tree-sitter-grammars / tree-sitter-hcl

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

Add terraform filetype #13

Closed wagensveld closed 2 years ago

wagensveld commented 2 years ago

Is your feature request related to a problem? Please describe. I've noticed recently neovim opens tf files as filetype=terraform, I'm not sure when this change was made. As a result tree-sitter doesn't pick up this parser unless I set syntax=tf.

Describe the solution you'd like Would it be possible to add terraform as a supported filetype in package.json?

MichaHoffmann commented 2 years ago

That would not be a problem! However for neovim I want to recommend https://github.com/nvim-treesitter/nvim-treesitter. It should work for filetype terraform ( https://github.com/nvim-treesitter/nvim-treesitter/blob/678a7857911f20cc9445aa13d28c454ae72483f5/lua/nvim-treesitter/parsers.lua#L432 )

Would using nvim-treesitter work for you instead? Otherwise we can also add terraform as supported filetype.

wagensveld commented 2 years ago

Thanks for that! I have nvim treesitter installed and the hcl parser installed, when I open a tf file it defaults syntax to terraform

Screen Shot 2021-11-30 at 11 37 30

Setting the syntax to tf works though

Screen Shot 2021-11-30 at 11 37 47

Do you know if there is any additional config required to get neovim to pick up the correct syntax? Thanks

MichaHoffmann commented 2 years ago

Ok it should work with nvim-treesitter, so thats a problem. I strongly suspect the nvim-treesitter setup, could you reopen this issue there (with a reproducer if possible, that would greatly help)? I'll have a look at the weekend.

One further observation: the highlighting looks kind of wrong when the filetype is tf the template expressions "${var.name_prefix}" etc should not be highlighted as string ( kind of depends on your colorscheme, but for most its not ), also the comments should be highlighted differently.

For reference: my local nvim-treesitter config

local ts = require'nvim-treesitter.configs'

ts.setup {
  highlight = {
    enable = true
  },
  indent = {
    enable = true
  },
  playground = {
    enable = true,
    disable = {},
    updatetime = 25,
    persist_queries = false,
    keybindings = {
      toggle_query_editor = 'o',
      toggle_hl_groups = 'i',
      toggle_injected_languages = 't',
      toggle_anonymous_nodes = 'a',
      toggle_language_display = 'I',
      focus_language = 'f',
      unfocus_language = 'F',
      update = 'R',
      goto_node = '<cr>',
      show_help = '?',
    },
  }
}

you can safely ignore the playground option, it just helps with debugging parsing issues once in a while.

wagensveld commented 2 years ago

Ahh that was it!

I didn't have highlight defined.

Thank you so much!