feat(hcl,terraform): split terraform from hcl, add specialized queries for tf

This enables us to have different queries for
terraform and general hcl. It solve the situation
where other dialects of hcl (nomad, packer, etc)
might use a terraform keyword and get highlighted
out of place.
This commit is contained in:
Michael Hoffmann 2022-12-02 23:47:19 +01:00 committed by Stephan Seitz
parent c06baac6a1
commit ee3e9f4dc0
17 changed files with 47 additions and 12 deletions

View file

@ -296,6 +296,7 @@ We are looking for maintainers to add more parsers and to write query files for
- [x] [swift](https://github.com/alex-pinkus/tree-sitter-swift) (maintained by @alex-pinkus)
- [x] [sxhkdrc](https://github.com/RaafatTurki/tree-sitter-sxhkdrc) (maintained by @RaafatTurki)
- [x] [teal](https://github.com/euclidianAce/tree-sitter-teal) (maintained by @euclidianAce)
- [x] [terraform](https://github.com/MichaHoffmann/tree-sitter-hcl) (maintained by @MichaHoffmann)
- [x] [tiger](https://github.com/ambroisie/tree-sitter-tiger) (maintained by @ambroisie)
- [x] [tlaplus](https://github.com/tlaplus-community/tree-sitter-tlaplus) (maintained by @ahelwer, @susliko)
- [x] [todotxt](https://github.com/arnarg/tree-sitter-todotxt.git) (experimental, maintained by @arnarg)

View file

@ -374,6 +374,9 @@
"teal": {
"revision": "1ae8c68e90523b26b93af56feb7868fe4214e2b2"
},
"terraform": {
"revision": "0ff887f2a60a147452d52db060de6b42f42f1441"
},
"tiger": {
"revision": "a233ebe360a73a92c50978e5c4e9e471bc59ff42"
},

View file

@ -8,7 +8,6 @@ local filetype_to_parsername = {
PKGBUILD = "bash",
html_tags = "html",
["typescript.tsx"] = "tsx",
terraform = "hcl",
["html.handlebars"] = "glimmer",
systemverilog = "verilog",
cls = "latex",
@ -618,6 +617,17 @@ list.hcl = {
filetype = "hcl",
}
list.terraform = {
install_info = {
url = "https://github.com/MichaHoffmann/tree-sitter-hcl",
files = { "src/parser.c", "src/scanner.cc" },
branch = "main",
location = "dialects/terraform",
},
maintainers = { "@MichaHoffmann" },
filetype = "terraform",
}
list.markdown = {
install_info = {
url = "https://github.com/MDeiml/tree-sitter-markdown",

View file

@ -81,7 +81,8 @@
(comment) @comment @spell
(identifier) @variable
(block (identifier) @type)
(body (block (identifier) @keyword))
(body (block (body (block (identifier) @type))))
(function_call (identifier) @function)
(attribute (identifier) @field)
@ -90,13 +91,9 @@
; highlight identifier keys as though they were block attributes
(object_elem key: (expression (variable_expr (identifier) @field)))
((identifier) @keyword (#any-of? @keyword "module" "root" "cwd" "resource" "variable" "data" "locals" "terraform" "provider" "output"))
((identifier) @type.builtin (#any-of? @type.builtin "bool" "string" "number" "object" "tuple" "list" "map" "set" "any"))
(variable_expr (identifier) @variable.builtin (#any-of? @variable.builtin "var" "local" "path"))
(get_attr (identifier) @variable.builtin (#any-of? @variable.builtin "root" "cwd" "module"))
(object_elem val: (expression
(variable_expr
(identifier) @type.builtin (#any-of? @type.builtin "bool" "string" "number" "object" "tuple" "list" "map" "set" "any"))))
; var.foo, data.bar
;
; first element in get_attr is a keyword or a reference to a keyword
(expression (variable_expr (identifier) @keyword) (get_attr (identifier) @field))
(ERROR) @error

View file

@ -0,0 +1 @@
; inherits: hcl

View file

@ -0,0 +1,21 @@
; inherits: hcl
; Terraform specific references
;
;
; local/module/data/var/output
(expression (variable_expr (identifier) @type.builtin (#any-of? @type.builtin "data" "var" "local" "module" "output")) (get_attr (identifier) @field))
; path.root/cwd/module
(expression (variable_expr (identifier) @type.builtin (#eq? @type.builtin "path")) (get_attr (identifier) @variable.builtin (#any-of? @variable.builtin "root" "cwd" "module")))
; terraform.workspace
(expression (variable_expr (identifier) @type.builtin (#eq? @type.builtin "terraform")) (get_attr (identifier) @variable.builtin (#any-of? @variable.builtin "workspace")))
; Terraform specific keywords
; FIXME: ideally only for identifiers under a `variable` block to minimize false positives
((identifier) @type.builtin (#any-of? @type.builtin "bool" "string" "number" "object" "tuple" "list" "map" "set" "any"))
(object_elem val: (expression
(variable_expr
(identifier) @type.builtin (#any-of? @type.builtin "bool" "string" "number" "object" "tuple" "list" "map" "set" "any"))))

View file

@ -0,0 +1 @@
; inherits: hcl

View file

@ -0,0 +1 @@
; inherits: hcl

View file

@ -1,13 +1,13 @@
local Runner = require("tests.indent.common").Runner
--local XFAIL = require("tests.indent.common").XFAIL
local run = Runner:new(it, "tests/indent/hcl", {
local run = Runner:new(it, "tests/indent/terraform", {
tabstop = 2,
shiftwidth = 2,
expandtab = true,
})
describe("indent HCL:", function()
describe("indent Terraform:", function()
describe("whole file:", function()
run:whole_file(".", {
expected_failures = {},