tree-sitter-grammars / tree-sitter-hcl

HCL grammar for tree-sitter
https://tree-sitter-grammars.github.io/tree-sitter-hcl/
Apache License 2.0
92 stars 20 forks source link

provider keyword is highlighted even as a property of a block #17

Closed radimsuckr closed 1 year ago

radimsuckr commented 2 years ago

Describe the bug The provider keyword is highlighted as a keyword even though it is part of a configuration block of a resource.

To Reproduce Steps to reproduce the behavior:

  1. Configure an action block nested in a stage block of a aws_codepipeline resource
  2. Define a provider argument in the action block
  3. The provider argument is highlighted as a keyword

Expected behavior provider should be a plain argument, not a keyword.

Screenshots image

Additional context I use the latest version provided by nvim-treesitter/nvim-treesitter.

mhoffm-aiven commented 2 years ago

Hey @radimsuckr,

thanks for flagging the issue! Parsing the text

stage {
  action {
    provider = "bar"
  }
}

results in

  (body [0, 0] - [4, 1]
    (block [0, 0] - [4, 1]
      (identifier [0, 0] - [0, 5])
      (block_start [0, 6] - [0, 7])
      (body [1, 2] - [3, 3]
        (block [1, 2] - [3, 3]
          (identifier [1, 2] - [1, 8])
          (block_start [1, 9] - [1, 10])
          (body [2, 4] - [2, 20]
            (attribute [2, 4] - [2, 20]
              (identifier [2, 4] - [2, 12])
              (expression [2, 15] - [2, 20]
                (literal_value [2, 15] - [2, 20]
                  (string_lit [2, 15] - [2, 20]
                    (quoted_template_start [2, 15] - [2, 16])
                    (template_literal [2, 16] - [2, 19])
                    (quoted_template_end [2, 19] - [2, 20]))))))
          (block_end [3, 2] - [3, 3])))
      (block_end [4, 0] - [4, 1]))))

so "provider" is an attribute of the "action" block and its value is a literal string. That seems parsed about right.

The queries in nvim-treesitter are very tailored to terraform, i suspect this line https://github.com/nvim-treesitter/nvim-treesitter/blob/6d0cee2f570ab848c0049be5a84a20acba3e8217/queries/hcl/highlights.scm#L93

Would you mind reraising this issue in nvim-treesitter ? I suspect the long-term solution would be something like https://github.com/tree-sitter/tree-sitter-typescript does with TSX and Typescript, just with terraform, packer, etc; so that nvim-treesitter can use a proper terraform parser.

MichaHoffmann commented 1 year ago

Hey, im currently rewriting the queries for nvim-treesitter terraform; ill make sure to write a test for this situation!

MichaHoffmann commented 1 year ago

Ok worked on highlights a bit on a branch, FPs should be a bit better; references also now have their leading part ( the resource to reference ) highlighted with same color as the resource block, which should help to identify them in templateas better. Ill submit a PR to nvim-treesitter once my PR introducing terraform as a distinct dialect is merged. before: image

after: image

MichaHoffmann commented 1 year ago

This should be solved with the latest version of nvim-treesitter