Skip to content

Commit 10dd471

Browse files
committed
Auto merge of rust-lang#12175 - Veykril:completion-rev, r=Veykril
fix: Fix snippets triggering where they shouldn't Fixes rust-lang/rust-analyzer#12169
2 parents 57c5447 + 0ce6206 commit 10dd471

File tree

10 files changed

+133
-101
lines changed

10 files changed

+133
-101
lines changed

crates/hir/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,11 +1370,12 @@ impl Function {
13701370
None
13711371
}
13721372

1373+
pub fn has_self_param(self, db: &dyn HirDatabase) -> bool {
1374+
db.function_data(self.id).has_self_param()
1375+
}
1376+
13731377
pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
1374-
if !db.function_data(self.id).has_self_param() {
1375-
return None;
1376-
}
1377-
Some(SelfParam { func: self.id })
1378+
self.has_self_param(db).then(|| SelfParam { func: self.id })
13781379
}
13791380

13801381
pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> {

crates/ide-completion/src/completions/dot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
4242
Some(PathCompletionCtx {
4343
is_absolute_path: false,
4444
qualifier: None,
45-
kind: PathKind::Expr,
45+
kind: PathKind::Expr { .. },
4646
..
4747
}) if !ctx.is_path_disallowed() => {}
4848
_ => return,

crates/ide-completion/src/completions/expr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
1515
}
1616

1717
let (&is_absolute_path, qualifier) = match &ctx.path_context {
18-
Some(PathCompletionCtx { kind: PathKind::Expr, is_absolute_path, qualifier, .. }) => {
19-
(is_absolute_path, qualifier)
20-
}
18+
Some(PathCompletionCtx {
19+
kind: PathKind::Expr { .. },
20+
is_absolute_path,
21+
qualifier,
22+
..
23+
}) => (is_absolute_path, qualifier),
2124
_ => return,
2225
};
2326

crates/ide-completion/src/completions/flyimport.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
161161
(_, ItemInNs::Types(hir::ModuleDef::Module(_))) => true,
162162
// and so are macros(except for attributes)
163163
(
164-
PathKind::Expr | PathKind::Type | PathKind::Item | PathKind::Pat,
164+
PathKind::Expr { .. } | PathKind::Type | PathKind::Item { .. } | PathKind::Pat,
165165
ItemInNs::Macros(mac),
166166
) => mac.is_fn_like(ctx.db),
167-
(PathKind::Item, _) => true,
167+
(PathKind::Item { .. }, _) => true,
168168

169-
(PathKind::Expr, ItemInNs::Types(_) | ItemInNs::Values(_)) => true,
169+
(PathKind::Expr { .. }, ItemInNs::Types(_) | ItemInNs::Values(_)) => true,
170170

171171
(PathKind::Pat, ItemInNs::Types(_)) => true,
172172
(PathKind::Pat, ItemInNs::Values(def)) => matches!(def, hir::ModuleDef::Const(_)),

crates/ide-completion/src/completions/item_list.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
1313
}
1414

1515
let (&is_absolute_path, qualifier) = match &ctx.path_context {
16-
Some(PathCompletionCtx { kind: PathKind::Item, is_absolute_path, qualifier, .. }) => {
17-
(is_absolute_path, qualifier)
18-
}
16+
Some(PathCompletionCtx {
17+
kind: PathKind::Item { .. },
18+
is_absolute_path,
19+
qualifier,
20+
..
21+
}) => (is_absolute_path, qualifier),
1922
_ => return,
2023
};
2124

crates/ide-completion/src/completions/keyword.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
125125
}
126126

127127
let (can_be_stmt, in_loop_body) = match ctx.path_context {
128-
Some(PathCompletionCtx { is_absolute_path: false, can_be_stmt, in_loop_body, .. }) => {
129-
(can_be_stmt, in_loop_body)
130-
}
128+
Some(PathCompletionCtx {
129+
is_absolute_path: false,
130+
kind: PathKind::Expr { in_block_expr, in_loop_body, .. },
131+
..
132+
}) => (in_block_expr, in_loop_body),
131133
_ => return,
132134
};
133135

crates/ide-completion/src/completions/snippet.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use ide_db::{imports::insert_use::ImportScope, SnippetCap};
55
use syntax::T;
66

77
use crate::{
8-
context::PathCompletionCtx, item::Builder, CompletionContext, CompletionItem,
9-
CompletionItemKind, Completions, SnippetScope,
8+
context::{ItemListKind, PathCompletionCtx, PathKind},
9+
item::Builder,
10+
CompletionContext, CompletionItem, CompletionItemKind, Completions, SnippetScope,
1011
};
1112

1213
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
@@ -16,14 +17,13 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str)
1617
}
1718

1819
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
19-
if ctx.function_def.is_none() {
20-
return;
21-
}
22-
2320
let can_be_stmt = match ctx.path_context {
2421
Some(PathCompletionCtx {
25-
is_absolute_path: false, qualifier: None, can_be_stmt, ..
26-
}) => can_be_stmt,
22+
is_absolute_path: false,
23+
qualifier: None,
24+
kind: PathKind::Expr { in_block_expr, .. },
25+
..
26+
}) => in_block_expr,
2727
_ => return,
2828
};
2929

@@ -43,11 +43,16 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
4343
}
4444

4545
pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
46-
if !(ctx.expects_item() || ctx.has_block_expr_parent())
47-
|| ctx.previous_token_is(T![unsafe])
48-
|| ctx.path_qual().is_some()
49-
|| ctx.has_unfinished_impl_or_trait_prev_sibling()
50-
{
46+
let path_kind = match ctx.path_context {
47+
Some(PathCompletionCtx {
48+
is_absolute_path: false,
49+
qualifier: None,
50+
kind: kind @ (PathKind::Item { .. } | PathKind::Expr { in_block_expr: true, .. }),
51+
..
52+
}) => kind,
53+
_ => return,
54+
};
55+
if ctx.previous_token_is(T![unsafe]) || ctx.has_unfinished_impl_or_trait_prev_sibling() {
5156
return;
5257
}
5358
if ctx.has_visibility_prev_sibling() {
@@ -64,7 +69,8 @@ pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte
6469
}
6570

6671
// Test-related snippets shouldn't be shown in blocks.
67-
if !ctx.has_block_expr_parent() {
72+
if let PathKind::Item { kind: ItemListKind::SourceFile | ItemListKind::Module, .. } = path_kind
73+
{
6874
let mut item = snippet(
6975
ctx,
7076
cap,
@@ -96,19 +102,22 @@ fn ${1:feature}() {
96102
item.lookup_by("tfn");
97103
item.add_to(acc);
98104
}
99-
100-
let item = snippet(
101-
ctx,
102-
cap,
103-
"macro_rules",
104-
"\
105+
if let PathKind::Item { kind: ItemListKind::SourceFile | ItemListKind::Module, .. }
106+
| PathKind::Expr { .. } = path_kind
107+
{
108+
let item = snippet(
109+
ctx,
110+
cap,
111+
"macro_rules",
112+
"\
105113
macro_rules! $1 {
106114
($2) => {
107115
$0
108116
};
109117
}",
110-
);
111-
item.add_to(acc);
118+
);
119+
item.add_to(acc);
120+
}
112121
}
113122

114123
fn add_custom_completions(

crates/ide-completion/src/completions/trait_impl.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,25 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
5656
if let Some((kind, replacement_range, impl_def)) = completion_match(ctx) {
5757
if let Some(hir_impl) = ctx.sema.to_def(&impl_def) {
5858
get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| {
59+
use self::ImplCompletionKind::*;
5960
match (item, kind) {
60-
(
61-
hir::AssocItem::Function(fn_item),
62-
ImplCompletionKind::All | ImplCompletionKind::Fn,
63-
) => add_function_impl(acc, ctx, replacement_range, fn_item, hir_impl),
64-
(
65-
hir::AssocItem::TypeAlias(type_item),
66-
ImplCompletionKind::All | ImplCompletionKind::TypeAlias,
67-
) => add_type_alias_impl(acc, ctx, replacement_range, type_item),
68-
(
69-
hir::AssocItem::Const(const_item),
70-
ImplCompletionKind::All | ImplCompletionKind::Const,
71-
) => add_const_impl(acc, ctx, replacement_range, const_item, hir_impl),
61+
(hir::AssocItem::Function(func), All | Fn) => {
62+
add_function_impl(acc, ctx, replacement_range, func, hir_impl)
63+
}
64+
(hir::AssocItem::TypeAlias(type_alias), All | TypeAlias) => {
65+
add_type_alias_impl(acc, ctx, replacement_range, type_alias)
66+
}
67+
(hir::AssocItem::Const(const_), All | Const) => {
68+
add_const_impl(acc, ctx, replacement_range, const_, hir_impl)
69+
}
7270
_ => {}
7371
}
7472
});
7573
}
7674
}
7775
}
7876

77+
// FIXME: This should be lifted out so that we can do proper smart item keyword completions
7978
fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, TextRange, ast::Impl)> {
8079
let token = ctx.token.clone();
8180

@@ -152,15 +151,15 @@ fn add_function_impl(
152151
func: hir::Function,
153152
impl_def: hir::Impl,
154153
) {
155-
let fn_name = func.name(ctx.db).to_smol_str();
154+
let fn_name = func.name(ctx.db);
156155

157-
let label = if func.assoc_fn_params(ctx.db).is_empty() {
158-
format!("fn {}()", fn_name)
159-
} else {
160-
format!("fn {}(..)", fn_name)
161-
};
156+
let label = format!(
157+
"fn {}({})",
158+
fn_name,
159+
if func.assoc_fn_params(ctx.db).is_empty() { "" } else { ".." }
160+
);
162161

163-
let completion_kind = if func.self_param(ctx.db).is_some() {
162+
let completion_kind = if func.has_self_param(ctx.db) {
164163
CompletionItemKind::Method
165164
} else {
166165
CompletionItemKind::SymbolKind(SymbolKind::Function)

0 commit comments

Comments
 (0)