-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add infer_function_return_type assist #6477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a14df19
Add infer_function_return_type assist
Veykril 0a7c851
Support closures in infer_function_return_type assist
Veykril 2e6e5d8
Ignore unit expressions in infer_function_return_type assist
Veykril c665884
Wrap non-block expressions in closures with a block
Veykril 4f0d02c
Enable infer_function_return_type in return-type position
Veykril 186431e
Adjust test-texts in infer_function_return_type
Veykril File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
337 changes: 337 additions & 0 deletions
337
crates/assists/src/handlers/infer_function_return_type.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,337 @@ | ||
| use hir::HirDisplay; | ||
| use syntax::{ast, AstNode, TextRange, TextSize}; | ||
| use test_utils::mark; | ||
|
|
||
| use crate::{AssistContext, AssistId, AssistKind, Assists}; | ||
|
|
||
| // Assist: infer_function_return_type | ||
| // | ||
| // Adds the return type to a function or closure inferred from its tail expression if it doesn't have a return | ||
| // type specified. This assists is useable in a functions or closures tail expression or return type position. | ||
| // | ||
| // ``` | ||
| // fn foo() { 4<|>2i32 } | ||
| // ``` | ||
| // -> | ||
| // ``` | ||
| // fn foo() -> i32 { 42i32 } | ||
| // ``` | ||
| pub(crate) fn infer_function_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
| let (tail_expr, builder_edit_pos, wrap_expr) = extract_tail(ctx)?; | ||
| let module = ctx.sema.scope(tail_expr.syntax()).module()?; | ||
| let ty = ctx.sema.type_of_expr(&tail_expr)?; | ||
| if ty.is_unit() { | ||
| return None; | ||
| } | ||
| let ty = ty.display_source_code(ctx.db(), module.into()).ok()?; | ||
|
|
||
| acc.add( | ||
| AssistId("infer_function_return_type", AssistKind::RefactorRewrite), | ||
| "Add this function's return type", | ||
| tail_expr.syntax().text_range(), | ||
| |builder| { | ||
| match builder_edit_pos { | ||
| InsertOrReplace::Insert(insert_pos) => { | ||
| builder.insert(insert_pos, &format!("-> {} ", ty)) | ||
| } | ||
| InsertOrReplace::Replace(text_range) => { | ||
| builder.replace(text_range, &format!("-> {}", ty)) | ||
| } | ||
| } | ||
| if wrap_expr { | ||
| mark::hit!(wrap_closure_non_block_expr); | ||
| // `|x| x` becomes `|x| -> T x` which is invalid, so wrap it in a block | ||
| builder.replace(tail_expr.syntax().text_range(), &format!("{{{}}}", tail_expr)); | ||
| } | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| enum InsertOrReplace { | ||
| Insert(TextSize), | ||
| Replace(TextRange), | ||
| } | ||
|
|
||
| /// Check the potentially already specified return type and reject it or turn it into a builder command | ||
| /// if allowed. | ||
| fn ret_ty_to_action(ret_ty: Option<ast::RetType>, insert_pos: TextSize) -> Option<InsertOrReplace> { | ||
| match ret_ty { | ||
| Some(ret_ty) => match ret_ty.ty() { | ||
| Some(ast::Type::InferType(_)) | None => { | ||
| mark::hit!(existing_infer_ret_type); | ||
| mark::hit!(existing_infer_ret_type_closure); | ||
| Some(InsertOrReplace::Replace(ret_ty.syntax().text_range())) | ||
| } | ||
| _ => { | ||
| mark::hit!(existing_ret_type); | ||
| mark::hit!(existing_ret_type_closure); | ||
| None | ||
| } | ||
| }, | ||
| None => Some(InsertOrReplace::Insert(insert_pos + TextSize::from(1))), | ||
| } | ||
| } | ||
|
|
||
| fn extract_tail(ctx: &AssistContext) -> Option<(ast::Expr, InsertOrReplace, bool)> { | ||
| let (tail_expr, return_type_range, action, wrap_expr) = | ||
| if let Some(closure) = ctx.find_node_at_offset::<ast::ClosureExpr>() { | ||
| let rpipe_pos = closure.param_list()?.syntax().last_token()?.text_range().end(); | ||
| let action = ret_ty_to_action(closure.ret_type(), rpipe_pos)?; | ||
|
|
||
| let body = closure.body()?; | ||
| let body_start = body.syntax().first_token()?.text_range().start(); | ||
| let (tail_expr, wrap_expr) = match body { | ||
| ast::Expr::BlockExpr(block) => (block.expr()?, false), | ||
| body => (body, true), | ||
| }; | ||
|
|
||
| let ret_range = TextRange::new(rpipe_pos, body_start); | ||
| (tail_expr, ret_range, action, wrap_expr) | ||
| } else { | ||
| let func = ctx.find_node_at_offset::<ast::Fn>()?; | ||
| let rparen_pos = func.param_list()?.r_paren_token()?.text_range().end(); | ||
| let action = ret_ty_to_action(func.ret_type(), rparen_pos)?; | ||
|
|
||
| let body = func.body()?; | ||
| let tail_expr = body.expr()?; | ||
|
|
||
| let ret_range_end = body.l_curly_token()?.text_range().start(); | ||
| let ret_range = TextRange::new(rparen_pos, ret_range_end); | ||
| (tail_expr, ret_range, action, false) | ||
| }; | ||
| let frange = ctx.frange.range; | ||
| if return_type_range.contains_range(frange) { | ||
| mark::hit!(cursor_in_ret_position); | ||
| mark::hit!(cursor_in_ret_position_closure); | ||
| } else if tail_expr.syntax().text_range().contains_range(frange) { | ||
| mark::hit!(cursor_on_tail); | ||
| mark::hit!(cursor_on_tail_closure); | ||
| } else { | ||
| return None; | ||
| } | ||
| Some((tail_expr, action, wrap_expr)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::tests::{check_assist, check_assist_not_applicable}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn infer_return_type_specified_inferred() { | ||
| mark::check!(existing_infer_ret_type); | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() -> <|>_ { | ||
| 45 | ||
| }"#, | ||
| r#"fn foo() -> i32 { | ||
| 45 | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type_specified_inferred_closure() { | ||
| mark::check!(existing_infer_ret_type_closure); | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| || -> _ {<|>45}; | ||
| }"#, | ||
| r#"fn foo() { | ||
| || -> i32 {45}; | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type_cursor_at_return_type_pos() { | ||
| mark::check!(cursor_in_ret_position); | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() <|>{ | ||
| 45 | ||
| }"#, | ||
| r#"fn foo() -> i32 { | ||
| 45 | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type_cursor_at_return_type_pos_closure() { | ||
| mark::check!(cursor_in_ret_position_closure); | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| || <|>45 | ||
| }"#, | ||
| r#"fn foo() { | ||
| || -> i32 {45} | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type() { | ||
| mark::check!(cursor_on_tail); | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| 45<|> | ||
| }"#, | ||
| r#"fn foo() -> i32 { | ||
| 45 | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type_nested() { | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| if true { | ||
| 3<|> | ||
| } else { | ||
| 5 | ||
| } | ||
| }"#, | ||
| r#"fn foo() -> i32 { | ||
| if true { | ||
| 3 | ||
| } else { | ||
| 5 | ||
| } | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn not_applicable_ret_type_specified() { | ||
| mark::check!(existing_ret_type); | ||
| check_assist_not_applicable( | ||
| infer_function_return_type, | ||
| r#"fn foo() -> i32 { | ||
| ( 45<|> + 32 ) * 123 | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn not_applicable_non_tail_expr() { | ||
| check_assist_not_applicable( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| let x = <|>3; | ||
| ( 45 + 32 ) * 123 | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn not_applicable_unit_return_type() { | ||
| check_assist_not_applicable( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| (<|>) | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type_closure_block() { | ||
| mark::check!(cursor_on_tail_closure); | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| |x: i32| { | ||
| x<|> | ||
| }; | ||
| }"#, | ||
| r#"fn foo() { | ||
| |x: i32| -> i32 { | ||
| x | ||
| }; | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type_closure() { | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| |x: i32| { x<|> }; | ||
| }"#, | ||
| r#"fn foo() { | ||
| |x: i32| -> i32 { x }; | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type_closure_wrap() { | ||
| mark::check!(wrap_closure_non_block_expr); | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| |x: i32| x<|>; | ||
| }"#, | ||
| r#"fn foo() { | ||
| |x: i32| -> i32 {x}; | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn infer_return_type_nested_closure() { | ||
| check_assist( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| || { | ||
| if true { | ||
| 3<|> | ||
| } else { | ||
| 5 | ||
| } | ||
| } | ||
| }"#, | ||
| r#"fn foo() { | ||
| || -> i32 { | ||
| if true { | ||
| 3 | ||
| } else { | ||
| 5 | ||
| } | ||
| } | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn not_applicable_ret_type_specified_closure() { | ||
| mark::check!(existing_ret_type_closure); | ||
| check_assist_not_applicable( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| || -> i32 { 3<|> } | ||
| }"#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn not_applicable_non_tail_expr_closure() { | ||
| check_assist_not_applicable( | ||
| infer_function_return_type, | ||
| r#"fn foo() { | ||
| || -> i32 { | ||
| let x = 3<|>; | ||
| 6 | ||
| } | ||
| }"#, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.