tree-sitter / tree-sitter-rust

Rust grammar for tree-sitter
MIT License
337 stars 96 forks source link

Nested format macro not parsed correctly. #200

Closed antogilbert closed 10 months ago

antogilbert commented 10 months ago

Calling a format macro within a format macro only the outermost macro gets highlighted correctly.

Code:

fn main() {
    let x = 3;
    let y = 42;
    let v1 = [1, 2, 3, 4, 5];
    let v2 = [1, 2, 3, 4, 5];

    let s = format!(
        "{}:{}{}",
        x,
        y,
        v1.iter()
            .zip(v2.iter())
            .map(|(a, b)| { format!("| {a}{b}") })
            .fold(String::new(), |acc, arg| acc + arg.as_str())
    );

    println!("{s}");
}

Output of :TSPlaygroudToggle (Notice the statement ERROR [7, 17] - [13, 13])

function_item [0, 0] - [17, 1]
  name: identifier [0, 3] - [0, 7]
  parameters: parameters [0, 7] - [0, 9]
  body: block [0, 10] - [17, 1]
    let_declaration [1, 4] - [1, 14]
      pattern: identifier [1, 8] - [1, 9]
      value: integer_literal [1, 12] - [1, 13]
    let_declaration [2, 4] - [2, 15]
      pattern: identifier [2, 8] - [2, 9]
      value: integer_literal [2, 12] - [2, 14]
    let_declaration [3, 4] - [3, 29]
      pattern: identifier [3, 8] - [3, 10]
      value: array_expression [3, 13] - [3, 28]
        integer_literal [3, 14] - [3, 15]
        integer_literal [3, 17] - [3, 18]
        integer_literal [3, 20] - [3, 21]
        integer_literal [3, 23] - [3, 24]
        integer_literal [3, 26] - [3, 27]
    let_declaration [4, 4] - [4, 29]
      pattern: identifier [4, 8] - [4, 10]
      value: array_expression [4, 13] - [4, 28]
        integer_literal [4, 14] - [4, 15]
        integer_literal [4, 17] - [4, 18]
        integer_literal [4, 20] - [4, 21]
        integer_literal [4, 23] - [4, 24]
        integer_literal [4, 26] - [4, 27]
    let_declaration [6, 4] - [14, 6]
      pattern: identifier [6, 8] - [6, 9]
      value: macro_invocation [6, 12] - [14, 5]
        macro: identifier [6, 12] - [6, 18]
        token_tree [6, 19] - [14, 5]
          expression_statement [6, 19] - [14, 5]
            unit_expression [6, 19] - [14, 5]
              ERROR [7, 17] - [13, 13]
          string_literal [7, 8] - [7, 17]
          identifier [8, 8] - [8, 9]
          identifier [9, 8] - [9, 9]
          identifier [10, 8] - [10, 10]
          identifier [10, 11] - [10, 15]
          token_tree [10, 15] - [10, 17]
          identifier [11, 13] - [11, 16]
          token_tree [11, 16] - [11, 27]
            identifier [11, 17] - [11, 19]
            identifier [11, 20] - [11, 24]
            token_tree [11, 24] - [11, 26]
          identifier [12, 13] - [12, 16]
          token_tree [12, 16] - [12, 50]
            token_tree [12, 18] - [12, 24]
              identifier [12, 19] - [12, 20]
              identifier [12, 22] - [12, 23]
            token_tree [12, 26] - [12, 49]
              identifier [12, 28] - [12, 34]
              token_tree [12, 35] - [12, 47]
                string_literal [12, 36] - [12, 46]
          identifier [13, 13] - [13, 17]
          token_tree [13, 17] - [13, 63]
            identifier [13, 18] - [13, 24]
            identifier [13, 26] - [13, 29]
            token_tree [13, 29] - [13, 31]
            identifier [13, 34] - [13, 37]
            identifier [13, 39] - [13, 42]
            identifier [13, 44] - [13, 47]
            identifier [13, 50] - [13, 53]
            identifier [13, 54] - [13, 60]
            token_tree [13, 60] - [13, 62]
    expression_statement [16, 4] - [16, 20]
      macro_invocation [16, 4] - [16, 19]
        macro: identifier [16, 4] - [16, 11]
        token_tree [16, 12] - [16, 19]
          unit_expression [16, 12] - [16, 19]
          string_literal [16, 13] - [16, 18]
amaanq commented 10 months ago

nope, cli parses it just fine, the issue is macros are self-injected with rust in neovim.

antogilbert commented 10 months ago

nope, cli parses it just fine, the issue is macros are self-injected with rust in neovim.

@amaanq thanks. Can you expand a bit more on this? Is there something else i can look at to figure out why i am seeing this issue?