diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-10-22 21:46:24 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-10-22 21:46:24 +0100 |
commit | 8b3c851dd37f39f79e7e8807378f45fdde7f6411 (patch) | |
tree | d6c20cd94e6291d6e4c3b8f58a17817d61463d79 /crates/completion/src/completion_context.rs | |
parent | edf46a13a6a28093985d2d934ef97570947b9494 (diff) | |
parent | 3dbbcfca67ed09322227f2190b5364754a29a216 (diff) |
Merge #6098
6098: Insert ref for completions r=adamrk a=adamrk
Follow up to https://github.com/rust-analyzer/rust-analyzer/pull/5846. When we have a local in scope which needs a ref or mutable ref to match the name and type of the active in the completion context then a new completion item with `&` or `&mut ` is inserted. E.g.
```rust
fn foo(arg: &i32){};
fn main() {
let arg = 1_i32;
foo(a<|>)
}
```
now offers `&arg` as a completion option with the highest score.
Co-authored-by: adamrk <[email protected]>
Diffstat (limited to 'crates/completion/src/completion_context.rs')
-rw-r--r-- | crates/completion/src/completion_context.rs | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/crates/completion/src/completion_context.rs b/crates/completion/src/completion_context.rs index dc4e136c6..e4f86d0e0 100644 --- a/crates/completion/src/completion_context.rs +++ b/crates/completion/src/completion_context.rs | |||
@@ -246,6 +246,19 @@ impl<'a> CompletionContext<'a> { | |||
246 | } | 246 | } |
247 | } | 247 | } |
248 | 248 | ||
249 | pub(crate) fn active_name_and_type(&self) -> Option<(String, Type)> { | ||
250 | if let Some(record_field) = &self.record_field_syntax { | ||
251 | mark::hit!(record_field_type_match); | ||
252 | let (struct_field, _local) = self.sema.resolve_record_field(record_field)?; | ||
253 | Some((struct_field.name(self.db).to_string(), struct_field.signature_ty(self.db))) | ||
254 | } else if let Some(active_parameter) = &self.active_parameter { | ||
255 | mark::hit!(active_param_type_match); | ||
256 | Some((active_parameter.name.clone(), active_parameter.ty.clone())) | ||
257 | } else { | ||
258 | None | ||
259 | } | ||
260 | } | ||
261 | |||
249 | fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { | 262 | fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { |
250 | let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); | 263 | let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); |
251 | let syntax_element = NodeOrToken::Token(fake_ident_token); | 264 | let syntax_element = NodeOrToken::Token(fake_ident_token); |