Skip to content

Commit e8bad53

Browse files
committed
Auto merge of #14455 - jplatte:convert-nested-function-to-closure, r=Veykril
Convert nested function to closure assist Continuation of / closes #13467. Resolves #13230. r? `@Veykril`
2 parents da9c0bd + bc704e1 commit e8bad53

File tree

3 files changed

+237
-1
lines changed

3 files changed

+237
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
use ide_db::assists::{AssistId, AssistKind};
2+
use syntax::ast::{self, HasGenericParams, HasName};
3+
use syntax::{AstNode, SyntaxKind};
4+
5+
use crate::assist_context::{AssistContext, Assists};
6+
7+
// Assist: convert_nested_function_to_closure
8+
//
9+
// Converts a function that is defined within the body of another function into a closure.
10+
//
11+
// ```
12+
// fn main() {
13+
// fn fo$0o(label: &str, number: u64) {
14+
// println!("{}: {}", label, number);
15+
// }
16+
//
17+
// foo("Bar", 100);
18+
// }
19+
// ```
20+
// ->
21+
// ```
22+
// fn main() {
23+
// let foo = |label: &str, number: u64| {
24+
// println!("{}: {}", label, number);
25+
// };
26+
//
27+
// foo("Bar", 100);
28+
// }
29+
// ```
30+
pub(crate) fn convert_nested_function_to_closure(
31+
acc: &mut Assists,
32+
ctx: &AssistContext<'_>,
33+
) -> Option<()> {
34+
let name = ctx.find_node_at_offset::<ast::Name>()?;
35+
let function = name.syntax().parent().and_then(ast::Fn::cast)?;
36+
37+
if !is_nested_function(&function) || is_generic(&function) || has_modifiers(&function) {
38+
return None;
39+
}
40+
41+
let target = function.syntax().text_range();
42+
let body = function.body()?;
43+
let name = function.name()?;
44+
let param_list = function.param_list()?;
45+
46+
acc.add(
47+
AssistId("convert_nested_function_to_closure", AssistKind::RefactorRewrite),
48+
"Convert nested function to closure",
49+
target,
50+
|edit| {
51+
let params = &param_list.syntax().text().to_string();
52+
let params = params.strip_prefix("(").unwrap_or(params);
53+
let params = params.strip_suffix(")").unwrap_or(params);
54+
55+
let mut body = body.to_string();
56+
if !has_semicolon(&function) {
57+
body.push(';');
58+
}
59+
edit.replace(target, format!("let {name} = |{params}| {body}"));
60+
},
61+
)
62+
}
63+
64+
/// Returns whether the given function is nested within the body of another function.
65+
fn is_nested_function(function: &ast::Fn) -> bool {
66+
function.syntax().ancestors().skip(1).find_map(ast::Item::cast).map_or(false, |it| {
67+
matches!(it, ast::Item::Fn(_) | ast::Item::Static(_) | ast::Item::Const(_))
68+
})
69+
}
70+
71+
/// Returns whether the given nested function has generic parameters.
72+
fn is_generic(function: &ast::Fn) -> bool {
73+
function.generic_param_list().is_some()
74+
}
75+
76+
/// Returns whether the given nested function has any modifiers:
77+
///
78+
/// - `async`,
79+
/// - `const` or
80+
/// - `unsafe`
81+
fn has_modifiers(function: &ast::Fn) -> bool {
82+
function.async_token().is_some()
83+
|| function.const_token().is_some()
84+
|| function.unsafe_token().is_some()
85+
}
86+
87+
/// Returns whether the given nested function has a trailing semicolon.
88+
fn has_semicolon(function: &ast::Fn) -> bool {
89+
function
90+
.syntax()
91+
.next_sibling_or_token()
92+
.map(|t| t.kind() == SyntaxKind::SEMICOLON)
93+
.unwrap_or(false)
94+
}
95+
96+
#[cfg(test)]
97+
mod tests {
98+
use crate::tests::{check_assist, check_assist_not_applicable};
99+
100+
use super::convert_nested_function_to_closure;
101+
102+
#[test]
103+
fn convert_nested_function_to_closure_works() {
104+
check_assist(
105+
convert_nested_function_to_closure,
106+
r#"
107+
fn main() {
108+
fn $0foo(a: u64, b: u64) -> u64 {
109+
2 * (a + b)
110+
}
111+
112+
_ = foo(3, 4);
113+
}
114+
"#,
115+
r#"
116+
fn main() {
117+
let foo = |a: u64, b: u64| {
118+
2 * (a + b)
119+
};
120+
121+
_ = foo(3, 4);
122+
}
123+
"#,
124+
);
125+
}
126+
127+
#[test]
128+
fn convert_nested_function_to_closure_works_with_existing_semicolon() {
129+
check_assist(
130+
convert_nested_function_to_closure,
131+
r#"
132+
fn main() {
133+
fn foo$0(a: u64, b: u64) -> u64 {
134+
2 * (a + b)
135+
};
136+
137+
_ = foo(3, 4);
138+
}
139+
"#,
140+
r#"
141+
fn main() {
142+
let foo = |a: u64, b: u64| {
143+
2 * (a + b)
144+
};
145+
146+
_ = foo(3, 4);
147+
}
148+
"#,
149+
);
150+
}
151+
152+
#[test]
153+
fn convert_nested_function_to_closure_is_not_suggested_on_top_level_function() {
154+
check_assist_not_applicable(
155+
convert_nested_function_to_closure,
156+
r#"
157+
fn ma$0in() {}
158+
"#,
159+
);
160+
}
161+
162+
#[test]
163+
fn convert_nested_function_to_closure_is_not_suggested_when_cursor_off_name() {
164+
check_assist_not_applicable(
165+
convert_nested_function_to_closure,
166+
r#"
167+
fn main() {
168+
fn foo(a: u64, $0b: u64) -> u64 {
169+
2 * (a + b)
170+
}
171+
172+
_ = foo(3, 4);
173+
}
174+
"#,
175+
);
176+
}
177+
178+
#[test]
179+
fn convert_nested_function_to_closure_is_not_suggested_if_function_has_generic_params() {
180+
check_assist_not_applicable(
181+
convert_nested_function_to_closure,
182+
r#"
183+
fn main() {
184+
fn fo$0o<S: Into<String>>(s: S) -> String {
185+
s.into()
186+
}
187+
188+
_ = foo("hello");
189+
}
190+
"#,
191+
);
192+
}
193+
194+
#[test]
195+
fn convert_nested_function_to_closure_is_not_suggested_if_function_has_modifier() {
196+
check_assist_not_applicable(
197+
convert_nested_function_to_closure,
198+
r#"
199+
fn main() {
200+
const fn fo$0o(s: String) -> String {
201+
s
202+
}
203+
204+
_ = foo("hello");
205+
}
206+
"#,
207+
);
208+
}
209+
}

crates/ide-assists/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ mod handlers {
122122
mod convert_iter_for_each_to_for;
123123
mod convert_let_else_to_match;
124124
mod convert_match_to_let_else;
125+
mod convert_nested_function_to_closure;
125126
mod convert_tuple_struct_to_named_struct;
126127
mod convert_named_struct_to_tuple_struct;
127128
mod convert_to_guarded_return;
@@ -228,8 +229,9 @@ mod handlers {
228229
convert_iter_for_each_to_for::convert_iter_for_each_to_for,
229230
convert_iter_for_each_to_for::convert_for_loop_with_for_each,
230231
convert_let_else_to_match::convert_let_else_to_match,
231-
convert_named_struct_to_tuple_struct::convert_named_struct_to_tuple_struct,
232232
convert_match_to_let_else::convert_match_to_let_else,
233+
convert_named_struct_to_tuple_struct::convert_named_struct_to_tuple_struct,
234+
convert_nested_function_to_closure::convert_nested_function_to_closure,
233235
convert_to_guarded_return::convert_to_guarded_return,
234236
convert_tuple_struct_to_named_struct::convert_tuple_struct_to_named_struct,
235237
convert_two_arm_bool_match_to_matches_macro::convert_two_arm_bool_match_to_matches_macro,

crates/ide-assists/src/tests/generated.rs

+25
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,31 @@ impl Point {
494494
)
495495
}
496496

497+
#[test]
498+
fn doctest_convert_nested_function_to_closure() {
499+
check_doc_test(
500+
"convert_nested_function_to_closure",
501+
r#####"
502+
fn main() {
503+
fn fo$0o(label: &str, number: u64) {
504+
println!("{}: {}", label, number);
505+
}
506+
507+
foo("Bar", 100);
508+
}
509+
"#####,
510+
r#####"
511+
fn main() {
512+
let foo = |label: &str, number: u64| {
513+
println!("{}: {}", label, number);
514+
};
515+
516+
foo("Bar", 100);
517+
}
518+
"#####,
519+
)
520+
}
521+
497522
#[test]
498523
fn doctest_convert_to_guarded_return() {
499524
check_doc_test(

0 commit comments

Comments
 (0)