From ee3e9f4dc0e5ee9e2bfb1ee47638375840b8fe0f Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Fri, 2 Dec 2022 23:47:19 +0100 Subject: [PATCH] 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. --- README.md | 1 + lockfile.json | 3 +++ lua/nvim-treesitter/parsers.lua | 12 ++++++++++- queries/hcl/highlights.scm | 15 ++++++------- queries/terraform/folds.scm | 1 + queries/terraform/highlights.scm | 21 +++++++++++++++++++ queries/terraform/indents.scm | 1 + queries/terraform/injections.scm | 1 + .../{hcl => terraform}/function_call.tf | 0 .../indent-in-multiline-objects.tf | 0 .../indent-in-multiline-tuples.tf | 0 .../{hcl => terraform}/multiline-comments.tf | 0 .../{hcl => terraform}/multiple-attributes.tf | 0 .../{hcl => terraform}/multiple-blocks.tf | 0 .../{hcl => terraform}/nested_blocks.tf | 0 .../no-indent-after-brace.tf | 0 .../{hcl_spec.lua => terraform_spec.lua} | 4 ++-- 17 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 queries/terraform/folds.scm create mode 100644 queries/terraform/highlights.scm create mode 100644 queries/terraform/indents.scm create mode 100644 queries/terraform/injections.scm rename tests/indent/{hcl => terraform}/function_call.tf (100%) rename tests/indent/{hcl => terraform}/indent-in-multiline-objects.tf (100%) rename tests/indent/{hcl => terraform}/indent-in-multiline-tuples.tf (100%) rename tests/indent/{hcl => terraform}/multiline-comments.tf (100%) rename tests/indent/{hcl => terraform}/multiple-attributes.tf (100%) rename tests/indent/{hcl => terraform}/multiple-blocks.tf (100%) rename tests/indent/{hcl => terraform}/nested_blocks.tf (100%) rename tests/indent/{hcl => terraform}/no-indent-after-brace.tf (100%) rename tests/indent/{hcl_spec.lua => terraform_spec.lua} (95%) diff --git a/README.md b/README.md index 40aa11d6a..2f210f718 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lockfile.json b/lockfile.json index bb4e18ade..97aad4ec5 100644 --- a/lockfile.json +++ b/lockfile.json @@ -374,6 +374,9 @@ "teal": { "revision": "1ae8c68e90523b26b93af56feb7868fe4214e2b2" }, + "terraform": { + "revision": "0ff887f2a60a147452d52db060de6b42f42f1441" + }, "tiger": { "revision": "a233ebe360a73a92c50978e5c4e9e471bc59ff42" }, diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index f4e13974b..6bd94f93b 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -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", diff --git a/queries/hcl/highlights.scm b/queries/hcl/highlights.scm index 658369292..4f2eb2300 100644 --- a/queries/hcl/highlights.scm +++ b/queries/hcl/highlights.scm @@ -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 diff --git a/queries/terraform/folds.scm b/queries/terraform/folds.scm new file mode 100644 index 000000000..0e5ffc2db --- /dev/null +++ b/queries/terraform/folds.scm @@ -0,0 +1 @@ +; inherits: hcl diff --git a/queries/terraform/highlights.scm b/queries/terraform/highlights.scm new file mode 100644 index 000000000..d31b83c82 --- /dev/null +++ b/queries/terraform/highlights.scm @@ -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")))) diff --git a/queries/terraform/indents.scm b/queries/terraform/indents.scm new file mode 100644 index 000000000..0e5ffc2db --- /dev/null +++ b/queries/terraform/indents.scm @@ -0,0 +1 @@ +; inherits: hcl diff --git a/queries/terraform/injections.scm b/queries/terraform/injections.scm new file mode 100644 index 000000000..0e5ffc2db --- /dev/null +++ b/queries/terraform/injections.scm @@ -0,0 +1 @@ +; inherits: hcl diff --git a/tests/indent/hcl/function_call.tf b/tests/indent/terraform/function_call.tf similarity index 100% rename from tests/indent/hcl/function_call.tf rename to tests/indent/terraform/function_call.tf diff --git a/tests/indent/hcl/indent-in-multiline-objects.tf b/tests/indent/terraform/indent-in-multiline-objects.tf similarity index 100% rename from tests/indent/hcl/indent-in-multiline-objects.tf rename to tests/indent/terraform/indent-in-multiline-objects.tf diff --git a/tests/indent/hcl/indent-in-multiline-tuples.tf b/tests/indent/terraform/indent-in-multiline-tuples.tf similarity index 100% rename from tests/indent/hcl/indent-in-multiline-tuples.tf rename to tests/indent/terraform/indent-in-multiline-tuples.tf diff --git a/tests/indent/hcl/multiline-comments.tf b/tests/indent/terraform/multiline-comments.tf similarity index 100% rename from tests/indent/hcl/multiline-comments.tf rename to tests/indent/terraform/multiline-comments.tf diff --git a/tests/indent/hcl/multiple-attributes.tf b/tests/indent/terraform/multiple-attributes.tf similarity index 100% rename from tests/indent/hcl/multiple-attributes.tf rename to tests/indent/terraform/multiple-attributes.tf diff --git a/tests/indent/hcl/multiple-blocks.tf b/tests/indent/terraform/multiple-blocks.tf similarity index 100% rename from tests/indent/hcl/multiple-blocks.tf rename to tests/indent/terraform/multiple-blocks.tf diff --git a/tests/indent/hcl/nested_blocks.tf b/tests/indent/terraform/nested_blocks.tf similarity index 100% rename from tests/indent/hcl/nested_blocks.tf rename to tests/indent/terraform/nested_blocks.tf diff --git a/tests/indent/hcl/no-indent-after-brace.tf b/tests/indent/terraform/no-indent-after-brace.tf similarity index 100% rename from tests/indent/hcl/no-indent-after-brace.tf rename to tests/indent/terraform/no-indent-after-brace.tf diff --git a/tests/indent/hcl_spec.lua b/tests/indent/terraform_spec.lua similarity index 95% rename from tests/indent/hcl_spec.lua rename to tests/indent/terraform_spec.lua index ee53d89fc..49fa22c20 100644 --- a/tests/indent/hcl_spec.lua +++ b/tests/indent/terraform_spec.lua @@ -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 = {},