Skip to content

Commit 2f87215

Browse files
committed
Auto merge of #16808 - ShoyuVanilla:proc-macro-sem-token, r=Veykril
feat: Add proc macro semantic token type Closes #11529
2 parents 40ee359 + fc11216 commit 2f87215

28 files changed

+56
-14
lines changed

crates/hir/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ use hir_def::{
5656
AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, CrateRootModuleId, DefWithBodyId,
5757
EnumId, EnumVariantId, ExternCrateId, FunctionId, GenericDefId, GenericParamId, HasModule,
5858
ImplId, InTypeConstId, ItemContainerId, LifetimeParamId, LocalFieldId, Lookup, MacroExpander,
59-
MacroId, ModuleId, StaticId, StructId, TraitAliasId, TraitId, TupleId, TypeAliasId,
60-
TypeOrConstParamId, TypeParamId, UnionId,
59+
ModuleId, StaticId, StructId, TraitAliasId, TraitId, TupleId, TypeAliasId, TypeOrConstParamId,
60+
TypeParamId, UnionId,
6161
};
6262
use hir_expand::{attrs::collect_attrs, name::name, proc_macro::ProcMacroKind, MacroCallKind};
6363
use hir_ty::{
@@ -122,7 +122,7 @@ pub use {
122122
visibility::Visibility,
123123
// FIXME: This is here since some queries take it as input that are used
124124
// outside of hir.
125-
{AdtId, ModuleDefId},
125+
{AdtId, MacroId, ModuleDefId},
126126
},
127127
hir_expand::{
128128
attrs::{Attr, AttrId},

crates/hir/src/semantics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,11 @@ impl<'db> SemanticsImpl<'db> {
12361236
sa.resolve_macro_call(self.db, macro_call)
12371237
}
12381238

1239+
pub fn is_proc_macro_call(&self, macro_call: &ast::MacroCall) -> bool {
1240+
self.resolve_macro_call(macro_call)
1241+
.map_or(false, |m| matches!(m.id, MacroId::ProcMacroId(..)))
1242+
}
1243+
12391244
pub fn is_unsafe_macro_call(&self, macro_call: &ast::MacroCall) -> bool {
12401245
let sa = match self.analyze(macro_call.syntax()) {
12411246
Some(it) => it,

crates/ide-completion/src/item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ impl CompletionItemKind {
369369
SymbolKind::LifetimeParam => "lt",
370370
SymbolKind::Local => "lc",
371371
SymbolKind::Macro => "ma",
372+
SymbolKind::ProcMacro => "pm",
372373
SymbolKind::Module => "md",
373374
SymbolKind::SelfParam => "sp",
374375
SymbolKind::SelfType => "sy",

crates/ide-db/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ pub enum SymbolKind {
348348
LifetimeParam,
349349
Local,
350350
Macro,
351+
ProcMacro,
351352
Module,
352353
SelfParam,
353354
SelfType,
@@ -366,9 +367,8 @@ pub enum SymbolKind {
366367
impl From<hir::MacroKind> for SymbolKind {
367368
fn from(it: hir::MacroKind) -> Self {
368369
match it {
369-
hir::MacroKind::Declarative | hir::MacroKind::BuiltIn | hir::MacroKind::ProcMacro => {
370-
SymbolKind::Macro
371-
}
370+
hir::MacroKind::Declarative | hir::MacroKind::BuiltIn => SymbolKind::Macro,
371+
hir::MacroKind::ProcMacro => SymbolKind::ProcMacro,
372372
hir::MacroKind::Derive => SymbolKind::Derive,
373373
hir::MacroKind::Attr => SymbolKind::Attribute,
374374
}
@@ -381,6 +381,7 @@ impl From<hir::ModuleDefId> for SymbolKind {
381381
hir::ModuleDefId::ConstId(..) => SymbolKind::Const,
382382
hir::ModuleDefId::EnumVariantId(..) => SymbolKind::Variant,
383383
hir::ModuleDefId::FunctionId(..) => SymbolKind::Function,
384+
hir::ModuleDefId::MacroId(hir::MacroId::ProcMacroId(..)) => SymbolKind::ProcMacro,
384385
hir::ModuleDefId::MacroId(..) => SymbolKind::Macro,
385386
hir::ModuleDefId::ModuleId(..) => SymbolKind::Module,
386387
hir::ModuleDefId::StaticId(..) => SymbolKind::Static,

crates/ide/src/references.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ use proc_macros::mirror;
17101710
mirror$0! {}
17111711
"#,
17121712
expect![[r#"
1713-
mirror Macro FileId(1) 1..77 22..28
1713+
mirror ProcMacro FileId(1) 1..77 22..28
17141714
17151715
FileId(0) 26..32
17161716
"#]],

crates/ide/src/syntax_highlighting.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ fn traverse(
248248
// an attribute nested in a macro call will not emit `inside_attribute`
249249
let mut inside_attribute = false;
250250
let mut inside_macro_call = false;
251+
let mut inside_proc_macro_call = false;
251252

252253
// Walk all nodes, keeping track of whether we are inside a macro or not.
253254
// If in macro, expand it first and highlight the expanded code.
@@ -298,8 +299,9 @@ fn traverse(
298299
ast::Item::Fn(_) | ast::Item::Const(_) | ast::Item::Static(_) => {
299300
bindings_shadow_count.clear()
300301
}
301-
ast::Item::MacroCall(_) => {
302+
ast::Item::MacroCall(ref macro_call) => {
302303
inside_macro_call = true;
304+
inside_proc_macro_call = sema.is_proc_macro_call(macro_call);
303305
}
304306
_ => (),
305307
}
@@ -344,6 +346,7 @@ fn traverse(
344346
}
345347
Some(ast::Item::MacroCall(_)) => {
346348
inside_macro_call = false;
349+
inside_proc_macro_call = false;
347350
}
348351
_ => (),
349352
}
@@ -519,6 +522,9 @@ fn traverse(
519522
highlight |= HlMod::Attribute
520523
}
521524
if inside_macro_call && tt_level > 0 {
525+
if inside_proc_macro_call {
526+
highlight |= HlMod::ProcMacro
527+
}
522528
highlight |= HlMod::Macro
523529
}
524530

crates/ide/src/syntax_highlighting/html.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
9898
.numeric_literal { color: #BFEBBF; }
9999
.bool_literal { color: #BFE6EB; }
100100
.macro { color: #94BFF3; }
101+
.proc_macro { color: #94BFF3; text-decoration: underline; }
101102
.derive { color: #94BFF3; font-style: italic; }
102103
.module { color: #AFD8AF; }
103104
.value_param { color: #DCDCCC; }

crates/ide/src/syntax_highlighting/tags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub enum HlMod {
7575
Library,
7676
/// Used to differentiate individual elements within macro calls.
7777
Macro,
78+
ProcMacro,
7879
/// Mutable binding.
7980
Mutable,
8081
/// Used for public items.
@@ -146,6 +147,7 @@ impl HlTag {
146147
SymbolKind::LifetimeParam => "lifetime",
147148
SymbolKind::Local => "variable",
148149
SymbolKind::Macro => "macro",
150+
SymbolKind::ProcMacro => "proc_macro",
149151
SymbolKind::Module => "module",
150152
SymbolKind::SelfParam => "self_keyword",
151153
SymbolKind::SelfType => "self_type_keyword",
@@ -219,6 +221,7 @@ impl HlMod {
219221
HlMod::IntraDocLink,
220222
HlMod::Library,
221223
HlMod::Macro,
224+
HlMod::ProcMacro,
222225
HlMod::Mutable,
223226
HlMod::Public,
224227
HlMod::Reference,
@@ -243,6 +246,7 @@ impl HlMod {
243246
HlMod::IntraDocLink => "intra_doc_link",
244247
HlMod::Library => "library",
245248
HlMod::Macro => "macro",
249+
HlMod::ProcMacro => "proc_macro",
246250
HlMod::Mutable => "mutable",
247251
HlMod::Public => "public",
248252
HlMod::Reference => "reference",

crates/ide/src/syntax_highlighting/test_data/highlight_assoc_functions.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
.numeric_literal { color: #BFEBBF; }
3030
.bool_literal { color: #BFE6EB; }
3131
.macro { color: #94BFF3; }
32+
.proc_macro { color: #94BFF3; text-decoration: underline; }
3233
.derive { color: #94BFF3; font-style: italic; }
3334
.module { color: #AFD8AF; }
3435
.value_param { color: #DCDCCC; }

crates/ide/src/syntax_highlighting/test_data/highlight_attributes.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
.numeric_literal { color: #BFEBBF; }
3030
.bool_literal { color: #BFE6EB; }
3131
.macro { color: #94BFF3; }
32+
.proc_macro { color: #94BFF3; text-decoration: underline; }
3233
.derive { color: #94BFF3; font-style: italic; }
3334
.module { color: #AFD8AF; }
3435
.value_param { color: #DCDCCC; }

0 commit comments

Comments
 (0)