Skip to content

Commit 971b083

Browse files
committed
Use Names instead of Strings in the completion rendering api
1 parent 3cb3f1d commit 971b083

File tree

10 files changed

+165
-142
lines changed

10 files changed

+165
-142
lines changed

crates/ide_completion/src/completions.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) mod unqualified_path;
1818

1919
use std::iter;
2020

21-
use hir::{known, ModPath, ScopeDef, Type};
21+
use hir::known;
2222
use ide_db::SymbolKind;
2323

2424
use crate::{
@@ -69,12 +69,17 @@ impl Completions {
6969
items.into_iter().for_each(|item| self.add(item.into()))
7070
}
7171

72-
pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
72+
pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &hir::Type) {
7373
let item = render_field(RenderContext::new(ctx), field, ty);
7474
self.add(item);
7575
}
7676

77-
pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
77+
pub(crate) fn add_tuple_field(
78+
&mut self,
79+
ctx: &CompletionContext,
80+
field: usize,
81+
ty: &hir::Type,
82+
) {
7883
let item = render_tuple_field(RenderContext::new(ctx), field, ty);
7984
self.add(item);
8085
}
@@ -89,8 +94,8 @@ impl Completions {
8994
pub(crate) fn add_resolution(
9095
&mut self,
9196
ctx: &CompletionContext,
92-
local_name: String,
93-
resolution: &ScopeDef,
97+
local_name: hir::Name,
98+
resolution: &hir::ScopeDef,
9499
) {
95100
if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
96101
self.add(item);
@@ -100,7 +105,7 @@ impl Completions {
100105
pub(crate) fn add_macro(
101106
&mut self,
102107
ctx: &CompletionContext,
103-
name: Option<String>,
108+
name: Option<hir::Name>,
104109
macro_: hir::MacroDef,
105110
) {
106111
let name = match name {
@@ -116,7 +121,7 @@ impl Completions {
116121
&mut self,
117122
ctx: &CompletionContext,
118123
func: hir::Function,
119-
local_name: Option<String>,
124+
local_name: Option<hir::Name>,
120125
) {
121126
if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) {
122127
self.add(item)
@@ -127,7 +132,7 @@ impl Completions {
127132
&mut self,
128133
ctx: &CompletionContext,
129134
func: hir::Function,
130-
local_name: Option<String>,
135+
local_name: Option<hir::Name>,
131136
) {
132137
if let Some(item) = render_method(RenderContext::new(ctx), None, local_name, func) {
133138
self.add(item)
@@ -149,7 +154,7 @@ impl Completions {
149154
&mut self,
150155
ctx: &CompletionContext,
151156
variant: hir::Variant,
152-
path: ModPath,
157+
path: hir::ModPath,
153158
) {
154159
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) {
155160
self.add(item);
@@ -183,7 +188,7 @@ impl Completions {
183188
&mut self,
184189
ctx: &CompletionContext,
185190
variant: hir::Variant,
186-
path: ModPath,
191+
path: hir::ModPath,
187192
) {
188193
let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
189194
self.add(item);
@@ -193,7 +198,7 @@ impl Completions {
193198
&mut self,
194199
ctx: &CompletionContext,
195200
variant: hir::Variant,
196-
local_name: Option<String>,
201+
local_name: Option<hir::Name>,
197202
) {
198203
let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
199204
self.add(item);

crates/ide_completion/src/completions/lifetime.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
1616
(Some(lt), Some(lp)) if lp == lt.clone() => return,
1717
(Some(_), Some(lp)) => {
1818
lp_string = lp.to_string();
19-
Some(&lp_string)
19+
Some(&*lp_string)
2020
}
2121
_ => None,
2222
};
2323

2424
ctx.scope.process_all_names(&mut |name, res| {
2525
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
26-
let name = name.to_string();
27-
if param_lifetime != Some(&name) {
26+
if param_lifetime != Some(&*name.to_string()) {
2827
acc.add_resolution(ctx, name, &res);
2928
}
3029
}
@@ -41,7 +40,7 @@ pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
4140
}
4241
ctx.scope.process_all_names(&mut |name, res| {
4342
if let ScopeDef::Label(_) = res {
44-
acc.add_resolution(ctx, name.to_string(), &res);
43+
acc.add_resolution(ctx, name, &res);
4544
}
4645
});
4746
}

crates/ide_completion/src/completions/macro_in_item_position.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl
1111

1212
ctx.scope.process_all_names(&mut |name, res| {
1313
if let hir::ScopeDef::MacroDef(mac) = res {
14-
acc.add_macro(ctx, Some(name.to_string()), mac);
14+
acc.add_macro(ctx, Some(name.clone()), mac);
1515
}
1616
// FIXME: This should be done in qualified_path/unqualified_path instead?
1717
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
18-
acc.add_resolution(ctx, name.to_string(), &res);
18+
acc.add_resolution(ctx, name, &res);
1919
}
2020
})
2121
}

crates/ide_completion/src/completions/pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
5151
_ => false,
5252
};
5353
if add_resolution {
54-
acc.add_resolution(ctx, name.to_string(), &res);
54+
acc.add_resolution(ctx, name, &res);
5555
}
5656
});
5757
}

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Completion of paths, i.e. `some::prefix::$0`.
22
3-
use hir::{Adt, HasVisibility, PathResolution, ScopeDef};
3+
use hir::HasVisibility;
44
use rustc_hash::FxHashSet;
55
use syntax::AstNode;
66

@@ -21,14 +21,14 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
2121
};
2222
let context_module = ctx.scope.module();
2323
if ctx.expects_assoc_item() {
24-
if let PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
24+
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
2525
let module_scope = module.scope(ctx.db, context_module);
2626
for (name, def) in module_scope {
27-
if let ScopeDef::MacroDef(macro_def) = def {
28-
acc.add_macro(ctx, Some(name.to_string()), macro_def);
27+
if let hir::ScopeDef::MacroDef(macro_def) = def {
28+
acc.add_macro(ctx, Some(name.clone()), macro_def);
2929
}
30-
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
31-
acc.add_resolution(ctx, name.to_string(), &def);
30+
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
31+
acc.add_resolution(ctx, name, &def);
3232
}
3333
}
3434
}
@@ -42,11 +42,11 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
4242
});
4343

4444
match resolution {
45-
PathResolution::Def(hir::ModuleDef::Module(module)) => {
45+
hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
4646
let module_scope = module.scope(ctx.db, context_module);
4747
for (name, def) in module_scope {
4848
if ctx.use_item_syntax.is_some() {
49-
if let ScopeDef::Unknown = def {
49+
if let hir::ScopeDef::Unknown = def {
5050
if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
5151
if name_ref.syntax().text() == name.to_string().as_str() {
5252
// for `use self::foo$0`, don't suggest `foo` as a completion
@@ -57,20 +57,20 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
5757
}
5858
}
5959

60-
acc.add_resolution(ctx, name.to_string(), &def);
60+
acc.add_resolution(ctx, name, &def);
6161
}
6262
}
63-
PathResolution::Def(def @ hir::ModuleDef::Adt(_))
64-
| PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
65-
| PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
66-
if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
63+
hir::PathResolution::Def(def @ hir::ModuleDef::Adt(_))
64+
| hir::PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
65+
| hir::PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
66+
if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def {
6767
add_enum_variants(ctx, acc, e);
6868
}
6969
let ty = match def {
7070
hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
7171
hir::ModuleDef::TypeAlias(a) => {
7272
let ty = a.ty(ctx.db);
73-
if let Some(Adt::Enum(e)) = ty.as_adt() {
73+
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
7474
cov_mark::hit!(completes_variant_through_alias);
7575
add_enum_variants(ctx, acc, e);
7676
}
@@ -117,7 +117,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
117117
});
118118
}
119119
}
120-
PathResolution::Def(hir::ModuleDef::Trait(t)) => {
120+
hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => {
121121
// Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
122122
for item in t.items(ctx.db) {
123123
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
@@ -130,15 +130,15 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
130130
}
131131
}
132132
}
133-
PathResolution::TypeParam(_) | PathResolution::SelfType(_) => {
133+
hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_) => {
134134
if let Some(krate) = ctx.krate {
135135
let ty = match resolution {
136-
PathResolution::TypeParam(param) => param.ty(ctx.db),
137-
PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
136+
hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
137+
hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
138138
_ => return,
139139
};
140140

141-
if let Some(Adt::Enum(e)) = ty.as_adt() {
141+
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
142142
add_enum_variants(ctx, acc, e);
143143
}
144144

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
1414
if ctx.expects_assoc_item() {
1515
ctx.scope.process_all_names(&mut |name, def| {
1616
if let ScopeDef::MacroDef(macro_def) = def {
17-
acc.add_macro(ctx, Some(name.to_string()), macro_def);
17+
acc.add_macro(ctx, Some(name.clone()), macro_def);
1818
}
1919
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
20-
acc.add_resolution(ctx, name.to_string(), &def);
20+
acc.add_resolution(ctx, name, &def);
2121
}
2222
});
2323
return;
@@ -27,7 +27,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
2727
cov_mark::hit!(only_completes_modules_in_import);
2828
ctx.scope.process_all_names(&mut |name, res| {
2929
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
30-
acc.add_resolution(ctx, name.to_string(), &res);
30+
acc.add_resolution(ctx, name, &res);
3131
}
3232
});
3333
return;
@@ -45,7 +45,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
4545
cov_mark::hit!(skip_lifetime_completion);
4646
return;
4747
}
48-
acc.add_resolution(ctx, name.to_string(), &res);
48+
acc.add_resolution(ctx, name, &res);
4949
});
5050
}
5151

0 commit comments

Comments
 (0)