Skip to content

Commit

Permalink
Auto merge of rust-lang#14455 - jplatte:convert-nested-function-to-cl…
Browse files Browse the repository at this point in the history
…osure, r=Veykril

Convert nested function to closure assist

Continuation of / closes rust-lang#13467.
Resolves rust-lang#13230.

r? `@Veykril`
  • Loading branch information
bors committed Apr 5, 2023
2 parents da9c0bd + bc704e1 commit e8bad53
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 1 deletion.
209 changes: 209 additions & 0 deletions crates/ide-assists/src/handlers/convert_nested_function_to_closure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
use ide_db::assists::{AssistId, AssistKind};
use syntax::ast::{self, HasGenericParams, HasName};
use syntax::{AstNode, SyntaxKind};

use crate::assist_context::{AssistContext, Assists};

// Assist: convert_nested_function_to_closure
//
// Converts a function that is defined within the body of another function into a closure.
//
// ```
// fn main() {
// fn fo$0o(label: &str, number: u64) {
// println!("{}: {}", label, number);
// }
//
// foo("Bar", 100);
// }
// ```
// ->
// ```
// fn main() {
// let foo = |label: &str, number: u64| {
// println!("{}: {}", label, number);
// };
//
// foo("Bar", 100);
// }
// ```
pub(crate) fn convert_nested_function_to_closure(
acc: &mut Assists,
ctx: &AssistContext<'_>,
) -> Option<()> {
let name = ctx.find_node_at_offset::<ast::Name>()?;
let function = name.syntax().parent().and_then(ast::Fn::cast)?;

if !is_nested_function(&function) || is_generic(&function) || has_modifiers(&function) {
return None;
}

let target = function.syntax().text_range();
let body = function.body()?;
let name = function.name()?;
let param_list = function.param_list()?;

acc.add(
AssistId("convert_nested_function_to_closure", AssistKind::RefactorRewrite),
"Convert nested function to closure",
target,
|edit| {
let params = &param_list.syntax().text().to_string();
let params = params.strip_prefix("(").unwrap_or(params);
let params = params.strip_suffix(")").unwrap_or(params);

let mut body = body.to_string();
if !has_semicolon(&function) {
body.push(';');
}
edit.replace(target, format!("let {name} = |{params}| {body}"));
},
)
}

/// Returns whether the given function is nested within the body of another function.
fn is_nested_function(function: &ast::Fn) -> bool {
function.syntax().ancestors().skip(1).find_map(ast::Item::cast).map_or(false, |it| {
matches!(it, ast::Item::Fn(_) | ast::Item::Static(_) | ast::Item::Const(_))
})
}

/// Returns whether the given nested function has generic parameters.
fn is_generic(function: &ast::Fn) -> bool {
function.generic_param_list().is_some()
}

/// Returns whether the given nested function has any modifiers:
///
/// - `async`,
/// - `const` or
/// - `unsafe`
fn has_modifiers(function: &ast::Fn) -> bool {
function.async_token().is_some()
|| function.const_token().is_some()
|| function.unsafe_token().is_some()
}

/// Returns whether the given nested function has a trailing semicolon.
fn has_semicolon(function: &ast::Fn) -> bool {
function
.syntax()
.next_sibling_or_token()
.map(|t| t.kind() == SyntaxKind::SEMICOLON)
.unwrap_or(false)
}

#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};

use super::convert_nested_function_to_closure;

#[test]
fn convert_nested_function_to_closure_works() {
check_assist(
convert_nested_function_to_closure,
r#"
fn main() {
fn $0foo(a: u64, b: u64) -> u64 {
2 * (a + b)
}
_ = foo(3, 4);
}
"#,
r#"
fn main() {
let foo = |a: u64, b: u64| {
2 * (a + b)
};
_ = foo(3, 4);
}
"#,
);
}

#[test]
fn convert_nested_function_to_closure_works_with_existing_semicolon() {
check_assist(
convert_nested_function_to_closure,
r#"
fn main() {
fn foo$0(a: u64, b: u64) -> u64 {
2 * (a + b)
};
_ = foo(3, 4);
}
"#,
r#"
fn main() {
let foo = |a: u64, b: u64| {
2 * (a + b)
};
_ = foo(3, 4);
}
"#,
);
}

#[test]
fn convert_nested_function_to_closure_is_not_suggested_on_top_level_function() {
check_assist_not_applicable(
convert_nested_function_to_closure,
r#"
fn ma$0in() {}
"#,
);
}

#[test]
fn convert_nested_function_to_closure_is_not_suggested_when_cursor_off_name() {
check_assist_not_applicable(
convert_nested_function_to_closure,
r#"
fn main() {
fn foo(a: u64, $0b: u64) -> u64 {
2 * (a + b)
}
_ = foo(3, 4);
}
"#,
);
}

#[test]
fn convert_nested_function_to_closure_is_not_suggested_if_function_has_generic_params() {
check_assist_not_applicable(
convert_nested_function_to_closure,
r#"
fn main() {
fn fo$0o<S: Into<String>>(s: S) -> String {
s.into()
}
_ = foo("hello");
}
"#,
);
}

#[test]
fn convert_nested_function_to_closure_is_not_suggested_if_function_has_modifier() {
check_assist_not_applicable(
convert_nested_function_to_closure,
r#"
fn main() {
const fn fo$0o(s: String) -> String {
s
}
_ = foo("hello");
}
"#,
);
}
}
4 changes: 3 additions & 1 deletion crates/ide-assists/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ mod handlers {
mod convert_iter_for_each_to_for;
mod convert_let_else_to_match;
mod convert_match_to_let_else;
mod convert_nested_function_to_closure;
mod convert_tuple_struct_to_named_struct;
mod convert_named_struct_to_tuple_struct;
mod convert_to_guarded_return;
Expand Down Expand Up @@ -228,8 +229,9 @@ mod handlers {
convert_iter_for_each_to_for::convert_iter_for_each_to_for,
convert_iter_for_each_to_for::convert_for_loop_with_for_each,
convert_let_else_to_match::convert_let_else_to_match,
convert_named_struct_to_tuple_struct::convert_named_struct_to_tuple_struct,
convert_match_to_let_else::convert_match_to_let_else,
convert_named_struct_to_tuple_struct::convert_named_struct_to_tuple_struct,
convert_nested_function_to_closure::convert_nested_function_to_closure,
convert_to_guarded_return::convert_to_guarded_return,
convert_tuple_struct_to_named_struct::convert_tuple_struct_to_named_struct,
convert_two_arm_bool_match_to_matches_macro::convert_two_arm_bool_match_to_matches_macro,
Expand Down
25 changes: 25 additions & 0 deletions crates/ide-assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,31 @@ impl Point {
)
}

#[test]
fn doctest_convert_nested_function_to_closure() {
check_doc_test(
"convert_nested_function_to_closure",
r#####"
fn main() {
fn fo$0o(label: &str, number: u64) {
println!("{}: {}", label, number);
}
foo("Bar", 100);
}
"#####,
r#####"
fn main() {
let foo = |label: &str, number: u64| {
println!("{}: {}", label, number);
};
foo("Bar", 100);
}
"#####,
)
}

#[test]
fn doctest_convert_to_guarded_return() {
check_doc_test(
Expand Down

0 comments on commit e8bad53

Please sign in to comment.