porcupine icon indicating copy to clipboard operation
porcupine copied to clipboard

tree-sitter highlighter: nested Rust macros

Open Akuli opened this issue 2 years ago • 3 comments

s

not_implemented! macro does not highlight when it is inside tokio::select! macro

Akuli avatar Aug 25 '22 17:08 Akuli

The correct name for the macro is actually unimplemented! but it highlights the same with the right name.

Akuli avatar Aug 25 '22 17:08 Akuli

This will be tricky to fix. foo!() parses as:

$ python3 scripts/tree-sitter-dump.py rust <(echo 'foo!()')
type=source_file text='foo!()\n'
  type=macro_invocation text='foo!()'
    field 'macro': type=identifier text='foo'
    type=! text='!'
    type=token_tree text='()'
      type=( text='('
      type=) text=')'

lol!{foo!()} parses as:

type=source_file text='lol!{foo!()}\n'
  type=macro_invocation text='lol!{foo!()}'
    field 'macro': type=identifier text='lol'
    type=! text='!'
    type=token_tree text='{foo!()}'
      type={ text='{'
      type=identifier text='foo'
      type=token_tree text='()'
        type=( text='('
        type=) text=')'
      type=} text='}'

The parse tree doesn't even have anything for the second !.

Presumably tree-sitter is trying to be "helpful" by not assuming much about macro bodies, as their specific syntax depends on which macro is being invoked. But it would be nice if it didn't just ignore ! magically.

Akuli avatar Aug 27 '22 09:08 Akuli

another example:

lazy_static! {
    static ref FILE_LOCK: tokio::sync::Mutex<&'static str> =
        tokio::sync::Mutex::new("catris_high_scores.txt");
}

Currently this highlights pretty differently if you comment out the lazy_static! { part, which makes the rest be inside a macro invocation.

Akuli avatar Aug 29 '22 21:08 Akuli