-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Add dereferencing autocomplete #18917
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
Conversation
This comment has been minimized.
This comment has been minimized.
crates/ide-completion/src/item.rs
Outdated
| let (label, edit_text) = match mode { | ||
| CompletionItemRefMode::Reference(Mutability::Shared) => { | ||
| (format!("&{}", self.label.primary), "&") | ||
| } | ||
| CompletionItemRefMode::Reference(Mutability::Mut) => { | ||
| (format!("&mut {}", self.label.primary), "&mut ") | ||
| } | ||
| CompletionItemRefMode::Dereference => (format!("*{}", self.label.primary), "*"), | ||
| }; | ||
|
|
||
| (label, ide_db::text_edit::Indel::insert(offset, String::from(edit_text)), relevance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let (label, edit_text) = match mode { | |
| CompletionItemRefMode::Reference(Mutability::Shared) => { | |
| (format!("&{}", self.label.primary), "&") | |
| } | |
| CompletionItemRefMode::Reference(Mutability::Mut) => { | |
| (format!("&mut {}", self.label.primary), "&mut ") | |
| } | |
| CompletionItemRefMode::Dereference => (format!("*{}", self.label.primary), "*"), | |
| }; | |
| (label, ide_db::text_edit::Indel::insert(offset, String::from(edit_text)), relevance) | |
| let prefix= match mode { | |
| CompletionItemRefMode::Reference(Mutability::Shared) => "&", | |
| CompletionItemRefMode::Reference(Mutability::Mut) => "&mut ", | |
| CompletionItemRefMode::Dereference =>"*", | |
| }; | |
| let label = format!("{prefix}{}", self.label.primary), | |
| (label, ide_db::text_edit::Indel::insert(offset, String::from(prefix)), relevance) |
crates/ide-completion/src/render.rs
Outdated
| } | ||
| } | ||
|
|
||
| if let Some(completion_ty_without_ref) = completion_ty.remove_ref() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can move this to the start of the function as let expected_type_without_ref = expected_type.remove_ref()?; since the branch above makes use of it as well either way to deduplicate a bit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify - are you suggesting we move both remove_ref() calls to the start of the function, or just the expected_type.remove_ref() call? Its also possible that the expected_type.remove_ref() returns None and would early return so there is still a need for the following code in the first branch.
if let Some(expected_without_ref) = &expected_type_without_ref
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Veykril I pushed the update to move both of these methods to the top of the function. Let me know if this is not what you were looking for.
crates/rust-analyzer/src/lib.rs
Outdated
| match ref_mode { | ||
| CompletionItemRefMode::Reference(mutability) => match mutability { | ||
| Mutability::Shared => hasher.update("&"), | ||
| Mutability::Mut => hasher.update("&mut"), | ||
| }, | ||
| CompletionItemRefMode::Dereference => { | ||
| hasher.update("*"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to above, we can move just return the strings in the match, then call hasher.update on that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me
Veykril
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
This PR adds dereferencing suggestions for Copy types. When a function expects a non-reference type T and we have a &T where T: Copy, we now suggest the dereferenced value in completions.
#18873