@@ -166,6 +166,7 @@ pub struct HighlightConfig {
166166// injected:: Emitted for doc-string injected highlighting like rust source blocks in documentation.
167167// intraDocLink:: Emitted for intra doc links in doc-strings.
168168// library:: Emitted for items that are defined outside of the current crate.
169+ // macro:: Emitted for tokens inside macro calls.
169170// mutable:: Emitted for mutable locals and statics as well as functions taking `&mut self`.
170171// public:: Emitted for items that are from the current crate and are `pub`.
171172// reference:: Emitted for locals behind a reference and functions taking `self` by reference.
@@ -240,6 +241,7 @@ fn traverse(
240241 let mut current_macro: Option < ast:: Macro > = None ;
241242 let mut macro_highlighter = MacroHighlighter :: default ( ) ;
242243 let mut inside_attribute = false ;
244+ let mut inside_macro_call = false ;
243245
244246 // Walk all nodes, keeping track of whether we are inside a macro or not.
245247 // If in macro, expand it first and highlight the expanded code.
@@ -270,46 +272,50 @@ fn traverse(
270272 inside_attribute = false
271273 }
272274
273- Enter ( NodeOrToken :: Node ( node) ) if ast:: Item :: can_cast ( node. kind ( ) ) => {
274- match ast:: Item :: cast ( node. clone ( ) ) {
275- Some ( ast:: Item :: MacroRules ( mac) ) => {
276- macro_highlighter. init ( ) ;
277- current_macro = Some ( mac. into ( ) ) ;
278- continue ;
279- }
280- Some ( ast:: Item :: MacroDef ( mac) ) => {
281- macro_highlighter. init ( ) ;
282- current_macro = Some ( mac. into ( ) ) ;
283- continue ;
284- }
285- Some ( item) => {
286- if matches ! ( node. kind( ) , FN | CONST | STATIC ) {
287- bindings_shadow_count. clear ( ) ;
275+ Enter ( NodeOrToken :: Node ( node) ) => match ast:: Item :: cast ( node. clone ( ) ) {
276+ Some ( item) => {
277+ match item {
278+ ast:: Item :: MacroRules ( mac) => {
279+ macro_highlighter. init ( ) ;
280+ current_macro = Some ( mac. into ( ) ) ;
281+ continue ;
282+ }
283+ ast:: Item :: MacroDef ( mac) => {
284+ macro_highlighter. init ( ) ;
285+ current_macro = Some ( mac. into ( ) ) ;
286+ continue ;
288287 }
288+ ast:: Item :: Fn ( _) | ast:: Item :: Const ( _) | ast:: Item :: Static ( _) => {
289+ bindings_shadow_count. clear ( )
290+ }
291+ ast:: Item :: MacroCall ( _) => {
292+ inside_macro_call = true ;
293+ }
294+ _ => ( ) ,
295+ }
289296
290- if attr_or_derive_item. is_none ( ) {
291- if sema. is_attr_macro_call ( & item) {
292- attr_or_derive_item = Some ( AttrOrDerive :: Attr ( item) ) ;
293- } else {
294- let adt = match item {
295- ast:: Item :: Enum ( it) => Some ( ast:: Adt :: Enum ( it) ) ,
296- ast:: Item :: Struct ( it) => Some ( ast:: Adt :: Struct ( it) ) ,
297- ast:: Item :: Union ( it) => Some ( ast:: Adt :: Union ( it) ) ,
298- _ => None ,
299- } ;
300- match adt {
301- Some ( adt) if sema. is_derive_annotated ( & adt) => {
302- attr_or_derive_item =
303- Some ( AttrOrDerive :: Derive ( ast:: Item :: from ( adt) ) ) ;
304- }
305- _ => ( ) ,
297+ if attr_or_derive_item. is_none ( ) {
298+ if sema. is_attr_macro_call ( & item) {
299+ attr_or_derive_item = Some ( AttrOrDerive :: Attr ( item) ) ;
300+ } else {
301+ let adt = match item {
302+ ast:: Item :: Enum ( it) => Some ( ast:: Adt :: Enum ( it) ) ,
303+ ast:: Item :: Struct ( it) => Some ( ast:: Adt :: Struct ( it) ) ,
304+ ast:: Item :: Union ( it) => Some ( ast:: Adt :: Union ( it) ) ,
305+ _ => None ,
306+ } ;
307+ match adt {
308+ Some ( adt) if sema. is_derive_annotated ( & adt) => {
309+ attr_or_derive_item =
310+ Some ( AttrOrDerive :: Derive ( ast:: Item :: from ( adt) ) ) ;
306311 }
312+ _ => ( ) ,
307313 }
308314 }
309315 }
310- _ => ( ) ,
311316 }
312- }
317+ _ => ( ) ,
318+ } ,
313319 Leave ( NodeOrToken :: Node ( node) ) if ast:: Item :: can_cast ( node. kind ( ) ) => {
314320 match ast:: Item :: cast ( node. clone ( ) ) {
315321 Some ( ast:: Item :: MacroRules ( mac) ) => {
@@ -327,6 +333,9 @@ fn traverse(
327333 {
328334 attr_or_derive_item = None ;
329335 }
336+ Some ( ast:: Item :: MacroCall ( _) ) => {
337+ inside_macro_call = false ;
338+ }
330339 _ => ( ) ,
331340 }
332341 }
@@ -476,6 +485,9 @@ fn traverse(
476485 if inside_attribute {
477486 highlight |= HlMod :: Attribute
478487 }
488+ if inside_macro_call && tt_level > 0 {
489+ highlight |= HlMod :: Macro
490+ }
479491
480492 hl. add ( HlRange { range, highlight, binding_hash } ) ;
481493 }
0 commit comments