Skip to content

Commit 01198da

Browse files
committed
Auto merge of rust-lang#12159 - Veykril:completions-rev, r=Veykril
internal: Lift out macro bang property from completion PathKind enum
2 parents e789d73 + 8b092ec commit 01198da

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ 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::Mac | PathKind::Pat,
164+
PathKind::Expr | PathKind::Type | PathKind::Item | PathKind::Pat,
165165
ItemInNs::Macros(mac),
166166
) => mac.is_fn_like(ctx.db),
167-
(PathKind::Mac, _) => true,
167+
(PathKind::Item, _) => true,
168168

169169
(PathKind::Expr, ItemInNs::Types(_) | ItemInNs::Values(_)) => true,
170170

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
1919
Some(PathCompletionCtx {
2020
is_absolute_path: false,
2121
qualifier: None,
22-
kind: None | Some(PathKind::Expr | PathKind::Type | PathKind::Mac),
22+
kind: None | Some(PathKind::Expr | PathKind::Type | PathKind::Item),
2323
..
2424
}) => (),
2525
_ => return,

crates/ide-completion/src/context.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,26 @@ pub(crate) enum Visible {
4747
pub(super) enum PathKind {
4848
Expr,
4949
Type,
50-
Attr { kind: AttrKind, annotated_item_kind: Option<SyntaxKind> },
50+
Attr {
51+
kind: AttrKind,
52+
annotated_item_kind: Option<SyntaxKind>,
53+
},
5154
Derive,
52-
// This should be removed in favor of `has_macro_bang` in PathCompletionContext
53-
Mac,
55+
/// Path in item position, that is inside an (Assoc)ItemList
56+
Item,
5457
Pat,
55-
Vis { has_in_token: bool },
58+
Vis {
59+
has_in_token: bool,
60+
},
5661
Use,
5762
}
5863

5964
#[derive(Debug)]
6065
pub(crate) struct PathCompletionCtx {
6166
/// If this is a call with () already there (or {} in case of record patterns)
6267
pub(super) has_call_parens: bool,
68+
/// If this has a macro call bang !
69+
pub(super) has_macro_bang: bool,
6370
/// Whether this path stars with a `::`.
6471
pub(super) is_absolute_path: bool,
6572
/// The qualifier of the current path if it exists.
@@ -942,6 +949,7 @@ impl<'a> CompletionContext<'a> {
942949
has_type_args: false,
943950
can_be_stmt: false,
944951
in_loop_body: false,
952+
has_macro_bang: false,
945953
kind: None,
946954
};
947955
let mut pat_ctx = None;
@@ -970,7 +978,21 @@ impl<'a> CompletionContext<'a> {
970978
pat_ctx = Some(pattern_context_for(original_file, it.into()));
971979
Some(PathKind::Pat)
972980
},
973-
ast::MacroCall(it) => it.excl_token().and(Some(PathKind::Mac)),
981+
ast::MacroCall(it) => {
982+
path_ctx.has_macro_bang = it.excl_token().is_some();
983+
match it.syntax().parent().map(|it| it.kind()) {
984+
Some(SyntaxKind::MACRO_PAT) => Some(PathKind::Pat),
985+
Some(SyntaxKind::MACRO_TYPE) => Some(PathKind::Type),
986+
Some(SyntaxKind::MACRO_EXPR) => Some(PathKind::Expr),
987+
Some(
988+
SyntaxKind::ITEM_LIST
989+
| SyntaxKind::ASSOC_ITEM_LIST
990+
| SyntaxKind::EXTERN_ITEM_LIST
991+
| SyntaxKind::SOURCE_FILE
992+
) => Some(PathKind::Item),
993+
_ => return Some(None),
994+
}
995+
},
974996
ast::Meta(meta) => (|| {
975997
let attr = meta.parent_attr()?;
976998
let kind = attr.kind();
@@ -989,6 +1011,10 @@ impl<'a> CompletionContext<'a> {
9891011
})(),
9901012
ast::Visibility(it) => Some(PathKind::Vis { has_in_token: it.in_token().is_some() }),
9911013
ast::UseTree(_) => Some(PathKind::Use),
1014+
ast::ItemList(_) => Some(PathKind::Item),
1015+
ast::AssocItemList(_) => Some(PathKind::Item),
1016+
ast::ExternItemList(_) => Some(PathKind::Item),
1017+
ast::SourceFile(_) => Some(PathKind::Item),
9921018
_ => return None,
9931019
}
9941020
};

crates/ide-completion/src/render/macro_.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ide_db::SymbolKind;
55
use syntax::SmolStr;
66

77
use crate::{
8-
context::PathKind,
8+
context::{PathCompletionCtx, PathKind},
99
item::{Builder, CompletionItem},
1010
render::RenderContext,
1111
};
@@ -33,8 +33,12 @@ fn render(
3333
let is_fn_like = macro_.is_fn_like(completion.db);
3434
let (bra, ket) = if is_fn_like { guess_macro_braces(&name, docs_str) } else { ("", "") };
3535

36-
let needs_bang =
37-
is_fn_like && !matches!(completion.path_kind(), Some(PathKind::Mac | PathKind::Use));
36+
let needs_bang = match completion.path_context {
37+
Some(PathCompletionCtx { kind, has_macro_bang, .. }) => {
38+
is_fn_like && kind != Some(PathKind::Use) && !has_macro_bang
39+
}
40+
_ => is_fn_like,
41+
};
3842

3943
let mut item = CompletionItem::new(
4044
SymbolKind::from(macro_.kind(completion.db)),

0 commit comments

Comments
 (0)