Skip to content

Commit d7c451e

Browse files
bors[bot]matklad
andauthored
Merge #4455
4455: Prioritize locals with correct types r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents f1587ac + 90c62bc commit d7c451e

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

crates/ra_ide/src/completion/completion_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) struct CompletionContext<'a> {
3434
pub(super) record_pat_syntax: Option<ast::RecordPat>,
3535
pub(super) record_field_syntax: Option<ast::RecordField>,
3636
pub(super) impl_def: Option<ast::ImplDef>,
37-
/// FIXME: `ActiveParameter` is string-based, which is very wrong
37+
/// FIXME: `ActiveParameter` is string-based, which is very very wrong
3838
pub(super) active_parameter: Option<ActiveParameter>,
3939
pub(super) is_param: bool,
4040
/// If a name-binding or reference to a const in a pattern.

crates/ra_ide/src/completion/presentation.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ use crate::{
1717
impl Completions {
1818
pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
1919
let is_deprecated = is_deprecated(field, ctx.db);
20-
let ty = ty.display(ctx.db).to_string();
2120
let name = field.name(ctx.db);
2221
let mut completion_item =
2322
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string())
2423
.kind(CompletionItemKind::Field)
25-
.detail(ty.clone())
24+
.detail(ty.display(ctx.db).to_string())
2625
.set_documentation(field.docs(ctx.db))
2726
.set_deprecated(is_deprecated);
2827

@@ -107,6 +106,12 @@ impl Completions {
107106
}
108107
};
109108

109+
if let ScopeDef::Local(local) = resolution {
110+
if let Some(score) = compute_score(ctx, &local.ty(ctx.db), &local_name) {
111+
completion_item = completion_item.set_score(score);
112+
}
113+
}
114+
110115
// Add `<>` for generic types
111116
if ctx.is_path_type && !ctx.has_type_args && ctx.config.add_call_parenthesis {
112117
if let Some(cap) = ctx.config.snippet_cap {
@@ -319,10 +324,11 @@ impl Completions {
319324

320325
pub(crate) fn compute_score(
321326
ctx: &CompletionContext,
322-
// FIXME: this definitely should be a `Type`
323-
ty: &str,
327+
ty: &Type,
324328
name: &str,
325329
) -> Option<CompletionScore> {
330+
// FIXME: this should not fall back to string equality.
331+
let ty = &ty.display(ctx.db).to_string();
326332
let (active_name, active_type) = if let Some(record_field) = &ctx.record_field_syntax {
327333
tested_by!(test_struct_field_completion_in_record_lit);
328334
let (struct_field, _local) = ctx.sema.resolve_record_field(record_field)?;
@@ -1405,4 +1411,48 @@ mod tests {
14051411
"###
14061412
);
14071413
}
1414+
1415+
#[test]
1416+
fn prioritize_exact_ref_match() {
1417+
assert_debug_snapshot!(
1418+
do_reference_completion(
1419+
r"
1420+
struct WorldSnapshot { _f: () };
1421+
fn go(world: &WorldSnapshot) {
1422+
go(w<|>)
1423+
}
1424+
",
1425+
),
1426+
@r###"
1427+
[
1428+
CompletionItem {
1429+
label: "WorldSnapshot",
1430+
source_range: 132..133,
1431+
delete: 132..133,
1432+
insert: "WorldSnapshot",
1433+
kind: Struct,
1434+
},
1435+
CompletionItem {
1436+
label: "go(…)",
1437+
source_range: 132..133,
1438+
delete: 132..133,
1439+
insert: "go(${1:world})$0",
1440+
kind: Function,
1441+
lookup: "go",
1442+
detail: "fn go(world: &WorldSnapshot)",
1443+
trigger_call_info: true,
1444+
},
1445+
CompletionItem {
1446+
label: "world",
1447+
source_range: 132..133,
1448+
delete: 132..133,
1449+
insert: "world",
1450+
kind: Binding,
1451+
detail: "&WorldSnapshot",
1452+
score: TypeAndNameMatch,
1453+
},
1454+
]
1455+
"###
1456+
);
1457+
}
14081458
}

0 commit comments

Comments
 (0)