-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Closure capture inlay hints #14742
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| //! Implementation of "closure return type" inlay hints. | ||
| //! | ||
| //! Tests live in [`bind_pat`][super::bind_pat] module. | ||
| use ide_db::{base_db::FileId, famous_defs::FamousDefs}; | ||
| use syntax::ast::{self, AstNode}; | ||
| use text_edit::{TextRange, TextSize}; | ||
|
|
||
| use crate::{InlayHint, InlayHintLabel, InlayHintsConfig, InlayKind}; | ||
|
|
||
| pub(super) fn hints( | ||
| acc: &mut Vec<InlayHint>, | ||
| FamousDefs(sema, _): &FamousDefs<'_, '_>, | ||
| config: &InlayHintsConfig, | ||
| _file_id: FileId, | ||
| closure: ast::ClosureExpr, | ||
| ) -> Option<()> { | ||
| if !config.closure_capture_hints { | ||
| return None; | ||
| } | ||
| let ty = &sema.type_of_expr(&closure.clone().into())?.original; | ||
| let c = ty.as_closure()?; | ||
| let captures = c.captured_items(sema.db); | ||
|
|
||
| if captures.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| let move_kw_range = match closure.move_token() { | ||
| Some(t) => t.text_range(), | ||
| None => { | ||
| let range = closure.syntax().first_token()?.prev_token()?.text_range(); | ||
| let range = TextRange::new(range.end() - TextSize::from(1), range.end()); | ||
| acc.push(InlayHint { | ||
| range, | ||
| kind: InlayKind::ClosureCapture, | ||
| label: InlayHintLabel::simple("move", None, None), | ||
| text_edit: None, | ||
| }); | ||
| range | ||
| } | ||
| }; | ||
| acc.push(InlayHint { | ||
| range: move_kw_range, | ||
| kind: InlayKind::ClosureCapture, | ||
| label: InlayHintLabel::from("("), | ||
| text_edit: None, | ||
| }); | ||
| let last = captures.len() - 1; | ||
| for (idx, capture) in captures.into_iter().enumerate() { | ||
| let local = capture.local(); | ||
Veykril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let source = local.primary_source(sema.db); | ||
|
|
||
| // force cache the source file, otherwise sema lookup will potentially panic | ||
| _ = sema.parse_or_expand(source.file()); | ||
|
|
||
| acc.push(InlayHint { | ||
| range: move_kw_range, | ||
| kind: InlayKind::ClosureCapture, | ||
| label: InlayHintLabel::simple( | ||
| format!( | ||
| "{}{}", | ||
| match capture.kind() { | ||
| hir::CaptureKind::SharedRef => "&", | ||
| hir::CaptureKind::UniqueSharedRef => "&unique ", | ||
| hir::CaptureKind::MutableRef => "&mut ", | ||
| hir::CaptureKind::Move => "", | ||
| }, | ||
| local.name(sema.db) | ||
| ), | ||
| None, | ||
| source.name().and_then(|name| sema.original_range_opt(name.syntax())), | ||
| ), | ||
| text_edit: None, | ||
| }); | ||
|
|
||
| if idx != last { | ||
| acc.push(InlayHint { | ||
| range: move_kw_range, | ||
| kind: InlayKind::ClosureCapture, | ||
| label: InlayHintLabel::simple(", ", None, None), | ||
| text_edit: None, | ||
| }); | ||
| } | ||
| } | ||
| acc.push(InlayHint { | ||
| range: move_kw_range, | ||
| kind: InlayKind::ClosureCapture, | ||
| label: InlayHintLabel::from(")"), | ||
| text_edit: None, | ||
| }); | ||
|
|
||
| Some(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::{ | ||
| inlay_hints::tests::{check_with_config, DISABLED_CONFIG}, | ||
| InlayHintsConfig, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn all_capture_kinds() { | ||
| check_with_config( | ||
| InlayHintsConfig { closure_capture_hints: true, ..DISABLED_CONFIG }, | ||
| r#" | ||
| //- minicore: copy, derive | ||
| #[derive(Copy, Clone)] | ||
| struct Copy; | ||
| struct NonCopy; | ||
| fn main() { | ||
| let foo = Copy; | ||
| let bar = NonCopy; | ||
| let mut baz = NonCopy; | ||
| let qux = &mut NonCopy; | ||
| || { | ||
| // ^ move | ||
| // ^ ( | ||
| // ^ &foo | ||
| // ^ , $ | ||
| // ^ bar | ||
| // ^ , $ | ||
| // ^ baz | ||
| // ^ , $ | ||
| // ^ qux | ||
| // ^ ) | ||
| foo; | ||
| bar; | ||
| baz; | ||
| qux; | ||
| }; | ||
| || { | ||
| // ^ move | ||
| // ^ ( | ||
| // ^ &foo | ||
| // ^ , $ | ||
| // ^ &bar | ||
| // ^ , $ | ||
| // ^ &baz | ||
| // ^ , $ | ||
| // ^ &qux | ||
| // ^ ) | ||
| &foo; | ||
| &bar; | ||
| &baz; | ||
| &qux; | ||
| }; | ||
| || { | ||
| // ^ move | ||
| // ^ ( | ||
| // ^ &mut baz | ||
| // ^ ) | ||
| &mut baz; | ||
| }; | ||
| // FIXME: &mut qux should be &unique qux | ||
| || { | ||
| // ^ move | ||
| // ^ ( | ||
| // ^ &mut baz | ||
| // ^ , $ | ||
| // ^ &mut qux | ||
| // ^ ) | ||
| baz = NonCopy; | ||
| *qux = NonCopy; | ||
| }; | ||
| } | ||
| "#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn move_token() { | ||
| check_with_config( | ||
| InlayHintsConfig { closure_capture_hints: true, ..DISABLED_CONFIG }, | ||
| r#" | ||
| //- minicore: copy, derive | ||
| fn main() { | ||
| let foo = u32; | ||
| move || { | ||
| // ^^^^ ( | ||
| // ^^^^ foo | ||
| // ^^^^ ) | ||
| foo; | ||
| }; | ||
| } | ||
| "#, | ||
| ); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.