Skip to content

Commit d9bb86a

Browse files
committed
Collect locals in context
1 parent 04fc937 commit d9bb86a

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

crates/ide/src/completion/completion_context.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! FIXME: write short doc here
22
33
use base_db::SourceDatabase;
4-
use hir::{Semantics, SemanticsScope, Type};
4+
use hir::{Local, ScopeDef, Semantics, SemanticsScope, Type};
55
use ide_db::RootDatabase;
66
use syntax::{
77
algo::{find_covering_element, find_node_at_offset},
@@ -90,6 +90,7 @@ pub(crate) struct CompletionContext<'a> {
9090
pub(super) impl_as_prev_sibling: bool,
9191
pub(super) is_match_arm: bool,
9292
pub(super) has_item_list_or_source_file_parent: bool,
93+
pub(super) locals: Vec<(String, Local)>,
9394
}
9495

9596
impl<'a> CompletionContext<'a> {
@@ -118,6 +119,12 @@ impl<'a> CompletionContext<'a> {
118119
original_file.syntax().token_at_offset(position.offset).left_biased()?;
119120
let token = sema.descend_into_macros(original_token.clone());
120121
let scope = sema.scope_at_offset(&token.parent(), position.offset);
122+
let mut locals = vec![];
123+
scope.process_all_names(&mut |name, scope| {
124+
if let ScopeDef::Local(local) = scope {
125+
locals.push((name.to_string(), local));
126+
}
127+
});
121128
let mut ctx = CompletionContext {
122129
sema,
123130
scope,
@@ -165,6 +172,7 @@ impl<'a> CompletionContext<'a> {
165172
if_is_prev: false,
166173
is_match_arm: false,
167174
has_item_list_or_source_file_parent: false,
175+
locals,
168176
};
169177

170178
let mut original_file = original_file.syntax().clone();

crates/ide/src/completion/presentation.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,15 @@ impl Completions {
192192
local_name: Option<String>,
193193
) {
194194
fn add_arg(arg: &str, ty: &Type, ctx: &CompletionContext) -> String {
195-
let mut prefix = "";
196195
if let Some(derefed_ty) = ty.remove_ref() {
197-
ctx.scope.process_all_names(&mut |name, scope| {
198-
if prefix != "" {
199-
return;
196+
for (name, local) in ctx.locals.iter() {
197+
if name == arg && local.can_unify(derefed_ty.clone(), ctx.db) {
198+
return (if ty.is_mutable_reference() { "&mut " } else { "&" }).to_string()
199+
+ &arg.to_string();
200200
}
201-
if let ScopeDef::Local(local) = scope {
202-
if name.to_string() == arg && local.can_unify(derefed_ty.clone(), ctx.db) {
203-
prefix = if ty.is_mutable_reference() { "&mut " } else { "&" };
204-
}
205-
}
206-
});
201+
}
207202
}
208-
prefix.to_string() + arg
203+
arg.to_string()
209204
};
210205
let name = local_name.unwrap_or_else(|| func.name(ctx.db).to_string());
211206
let ast_node = func.source(ctx.db).value;

0 commit comments

Comments
 (0)