Skip to content

Commit 1d74ef1

Browse files
committed
Don't complete values in type position
1 parent 5904726 commit 1d74ef1

File tree

8 files changed

+76
-55
lines changed

8 files changed

+76
-55
lines changed

crates/hir/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,18 @@ impl ScopeDef {
24962496

24972497
items
24982498
}
2499+
2500+
pub fn is_value_def(&self) -> bool {
2501+
matches!(
2502+
self,
2503+
ScopeDef::ModuleDef(ModuleDef::Function(_))
2504+
| ScopeDef::ModuleDef(ModuleDef::Variant(_))
2505+
| ScopeDef::ModuleDef(ModuleDef::Const(_))
2506+
| ScopeDef::ModuleDef(ModuleDef::Static(_))
2507+
| ScopeDef::GenericParam(GenericParam::ConstParam(_))
2508+
| ScopeDef::Local(_)
2509+
)
2510+
}
24992511
}
25002512

25012513
impl From<ItemInNs> for ScopeDef {

crates/hir/src/semantics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ pub enum PathResolution {
3535
Def(ModuleDef),
3636
/// A local binding (only value namespace)
3737
Local(Local),
38-
/// A generic parameter
38+
/// A type parameter
3939
TypeParam(TypeParam),
40+
/// A const parameter
4041
ConstParam(ConstParam),
4142
SelfType(Impl),
4243
Macro(MacroDef),

crates/ide_completion/src/completions.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ impl Builder {
5656
}
5757

5858
impl Completions {
59-
pub(crate) fn add(&mut self, item: CompletionItem) {
59+
fn add(&mut self, item: CompletionItem) {
6060
self.buf.push(item)
6161
}
6262

63+
fn add_opt(&mut self, item: Option<CompletionItem>) {
64+
if let Some(item) = item {
65+
self.buf.push(item)
66+
}
67+
}
68+
6369
pub(crate) fn add_all<I>(&mut self, items: I)
6470
where
6571
I: IntoIterator,
@@ -103,9 +109,10 @@ impl Completions {
103109
local_name: hir::Name,
104110
resolution: &hir::ScopeDef,
105111
) {
106-
if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
107-
self.add(item);
112+
if ctx.expects_type() && resolution.is_value_def() {
113+
return;
108114
}
115+
self.add_opt(render_resolution(RenderContext::new(ctx), local_name, resolution));
109116
}
110117

111118
pub(crate) fn add_macro(
@@ -118,9 +125,7 @@ impl Completions {
118125
Some(it) => it,
119126
None => return,
120127
};
121-
if let Some(item) = render_macro(RenderContext::new(ctx), None, name, macro_) {
122-
self.add(item);
123-
}
128+
self.add_opt(render_macro(RenderContext::new(ctx), None, name, macro_));
124129
}
125130

126131
pub(crate) fn add_function(
@@ -129,9 +134,10 @@ impl Completions {
129134
func: hir::Function,
130135
local_name: Option<hir::Name>,
131136
) {
132-
if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) {
133-
self.add(item)
137+
if ctx.expects_type() {
138+
return;
134139
}
140+
self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func));
135141
}
136142

137143
pub(crate) fn add_method(
@@ -141,10 +147,7 @@ impl Completions {
141147
receiver: Option<hir::Name>,
142148
local_name: Option<hir::Name>,
143149
) {
144-
if let Some(item) = render_method(RenderContext::new(ctx), None, receiver, local_name, func)
145-
{
146-
self.add(item)
147-
}
150+
self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
148151
}
149152

150153
pub(crate) fn add_variant_pat(
@@ -153,9 +156,7 @@ impl Completions {
153156
variant: hir::Variant,
154157
local_name: Option<hir::Name>,
155158
) {
156-
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, local_name, None) {
157-
self.add(item);
158-
}
159+
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
159160
}
160161

161162
pub(crate) fn add_qualified_variant_pat(
@@ -164,9 +165,7 @@ impl Completions {
164165
variant: hir::Variant,
165166
path: hir::ModPath,
166167
) {
167-
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) {
168-
self.add(item);
169-
}
168+
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
170169
}
171170

172171
pub(crate) fn add_struct_pat(
@@ -175,21 +174,18 @@ impl Completions {
175174
strukt: hir::Struct,
176175
local_name: Option<hir::Name>,
177176
) {
178-
if let Some(item) = render_struct_pat(RenderContext::new(ctx), strukt, local_name) {
179-
self.add(item);
180-
}
177+
self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
181178
}
182179

183180
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
184-
if let Some(item) = render_const(RenderContext::new(ctx), constant) {
185-
self.add(item);
181+
if ctx.expects_type() {
182+
return;
186183
}
184+
self.add_opt(render_const(RenderContext::new(ctx), constant));
187185
}
188186

189187
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
190-
if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) {
191-
self.add(item)
192-
}
188+
self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
193189
}
194190

195191
pub(crate) fn add_qualified_enum_variant(

crates/ide_completion/src/completions/attribute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attrib
6969
}
7070

7171
if is_inner || !attr_completion.prefer_inner {
72-
acc.add(item.build());
72+
item.add_to(acc);
7373
}
7474
};
7575

@@ -96,7 +96,7 @@ fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attrib
9696
if let Some(docs) = mac.docs(ctx.sema.db) {
9797
item.documentation(docs);
9898
}
99-
acc.add(item.build());
99+
item.add_to(acc);
100100
}
101101
}
102102
});

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding
9191
//! capability enabled.
9292
93-
use hir::ModPath;
9493
use ide_db::helpers::{
9594
import_assets::{ImportAssets, ImportCandidate},
9695
insert_use::ImportScope,
@@ -208,7 +207,7 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAs
208207
}
209208

210209
fn compute_fuzzy_completion_order_key(
211-
proposed_mod_path: &ModPath,
210+
proposed_mod_path: &hir::ModPath,
212211
user_input_lowercased: &str,
213212
) -> usize {
214213
cov_mark::hit!(certain_fuzzy_order_test);

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ fn x() -> $0
339339
"#,
340340
expect![[r#"
341341
st Foo
342-
fn x() fn()
343342
"#]],
344343
);
345344
}
@@ -391,7 +390,6 @@ pub mod prelude {
391390
}
392391
"#,
393392
expect![[r#"
394-
fn foo() fn()
395393
md std
396394
st Option
397395
"#]],
@@ -448,7 +446,6 @@ pub mod prelude {
448446
}
449447
"#,
450448
expect![[r#"
451-
fn foo() fn()
452449
md std
453450
md core
454451
st String
@@ -509,7 +506,6 @@ macro_rules! foo { () => {} }
509506
fn main() { let x: $0 }
510507
"#,
511508
expect![[r#"
512-
fn main() fn()
513509
ma foo!(…) macro_rules! foo
514510
"#]],
515511
);

crates/ide_completion/src/context.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@ pub(crate) enum PatternRefutability {
2929
Irrefutable,
3030
}
3131

32+
#[derive(Debug)]
33+
pub(super) enum PathKind {
34+
Expr,
35+
Type,
36+
}
37+
3238
#[derive(Debug)]
3339
pub(crate) struct PathCompletionContext {
3440
/// If this is a call with () already there
3541
call_kind: Option<CallKind>,
3642
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
3743
pub(super) is_trivial_path: bool,
3844
/// If not a trivial path, the prefix (qualifier).
39-
pub(super) path_qual: Option<ast::Path>,
40-
pub(super) is_path_type: bool,
45+
pub(super) qualifier: Option<ast::Path>,
46+
pub(super) kind: Option<PathKind>,
47+
/// Whether the path segment has type args or not.
4148
pub(super) has_type_args: bool,
4249
/// `true` if we are a statement or a last expr in the block.
4350
pub(super) can_be_stmt: bool,
44-
/// `true` if we expect an expression at the cursor position.
45-
pub(super) is_expr: bool,
4651
pub(super) in_loop_body: bool,
4752
}
4853

@@ -308,19 +313,23 @@ impl<'a> CompletionContext<'a> {
308313
}
309314

310315
pub(crate) fn expects_expression(&self) -> bool {
311-
self.path_context.as_ref().map_or(false, |it| it.is_expr)
316+
matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Expr), .. }))
317+
}
318+
319+
pub(crate) fn expects_type(&self) -> bool {
320+
matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Type), .. }))
312321
}
313322

314323
pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
315324
self.path_context.as_ref().and_then(|it| it.call_kind)
316325
}
317326

318327
pub(crate) fn is_trivial_path(&self) -> bool {
319-
self.path_context.as_ref().map_or(false, |it| it.is_trivial_path)
328+
matches!(self.path_context, Some(PathCompletionContext { is_trivial_path: true, .. }))
320329
}
321330

322331
pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
323-
self.path_context.as_ref().and_then(|it| it.path_qual.as_ref())
332+
self.path_context.as_ref().and_then(|it| it.qualifier.as_ref())
324333
}
325334

326335
fn fill_impl_def(&mut self) {
@@ -573,12 +582,11 @@ impl<'a> CompletionContext<'a> {
573582
let path_ctx = self.path_context.get_or_insert(PathCompletionContext {
574583
call_kind: None,
575584
is_trivial_path: false,
576-
path_qual: None,
585+
qualifier: None,
577586
has_type_args: false,
578-
is_path_type: false,
579587
can_be_stmt: false,
580-
is_expr: false,
581588
in_loop_body: false,
589+
kind: None,
582590
});
583591
path_ctx.in_loop_body = is_in_loop_body(name_ref.syntax());
584592
let path = segment.parent_path();
@@ -593,11 +601,20 @@ impl<'a> CompletionContext<'a> {
593601
}
594602
};
595603
}
596-
path_ctx.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
604+
605+
if let Some(parent) = path.syntax().parent() {
606+
path_ctx.kind = match_ast! {
607+
match parent {
608+
ast::PathType(_it) => Some(PathKind::Type),
609+
ast::PathExpr(_it) => Some(PathKind::Expr),
610+
_ => None,
611+
}
612+
};
613+
}
597614
path_ctx.has_type_args = segment.generic_arg_list().is_some();
598615

599616
if let Some(path) = path_or_use_tree_qualifier(&path) {
600-
path_ctx.path_qual = path
617+
path_ctx.qualifier = path
601618
.segment()
602619
.and_then(|it| {
603620
find_node_with_range::<ast::PathSegment>(
@@ -635,7 +652,6 @@ impl<'a> CompletionContext<'a> {
635652
None
636653
})
637654
.unwrap_or(false);
638-
path_ctx.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some();
639655
}
640656
}
641657
}

crates/ide_completion/src/render.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use ide_db::{
1818
use syntax::TextRange;
1919

2020
use crate::{
21+
context::{PathCompletionContext, PathKind},
2122
item::{CompletionRelevanceTypeMatch, ImportEdit},
2223
render::{enum_variant::render_variant, function::render_fn, macro_::render_macro},
2324
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance,
@@ -54,6 +55,9 @@ pub(crate) fn render_resolution_with_import<'a>(
5455
import_edit: ImportEdit,
5556
) -> Option<CompletionItem> {
5657
let resolution = hir::ScopeDef::from(import_edit.import.original_item);
58+
if ctx.completion.expects_type() && resolution.is_value_def() {
59+
return None;
60+
}
5761
let local_name = match resolution {
5862
hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db),
5963
hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?,
@@ -275,13 +279,10 @@ impl<'a> Render<'a> {
275279
};
276280

277281
// Add `<>` for generic types
278-
if self
279-
.ctx
280-
.completion
281-
.path_context
282-
.as_ref()
283-
.map_or(false, |it| it.is_path_type && !it.has_type_args)
284-
&& self.ctx.completion.config.add_call_parenthesis
282+
if matches!(
283+
self.ctx.completion.path_context,
284+
Some(PathCompletionContext { kind: Some(PathKind::Type), has_type_args: false, .. })
285+
) && self.ctx.completion.config.add_call_parenthesis
285286
{
286287
if let Some(cap) = self.ctx.snippet_cap() {
287288
let has_non_default_type_params = match resolution {

0 commit comments

Comments
 (0)