From 98e2d30aafceea4df4d96f8e568dbfaf9c4558f5 Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Wed, 7 Aug 2024 11:08:06 +0200 Subject: [PATCH 1/4] Fix identifier variable name typo --- Terraform.sublime-syntax | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Terraform.sublime-syntax b/Terraform.sublime-syntax index 02ca3ea..341f555 100644 --- a/Terraform.sublime-syntax +++ b/Terraform.sublime-syntax @@ -29,7 +29,7 @@ variables: # Identifiers: (UTF-8) (ID_Continue | '-')*; # # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#identifiers - identifer: (?:\b(?!null|false|true)[[:alpha:]][[:alnum:]_-]*\b) + identifier: (?:\b(?!null|false|true)[[:alpha:]][[:alnum:]_-]*\b) # Exponent: "e" or "E" followed by an optional sign # @@ -145,7 +145,7 @@ contexts: scope: punctuation.section.parens.end.terraform pop: true - include: expressions - - match: "{{identifer}}" + - match: "{{identifier}}" comment: Local Identifiers scope: variable.other.readwrite.terraform @@ -278,7 +278,7 @@ contexts: # # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#template-expressions heredoc: - - match: (\<\<\-?)\s*({{identifer}})\s*$ + - match: (\<\<\-?)\s*({{identifier}})\s*$ comment: String Heredoc's captures: 1: keyword.operator.heredoc.terraform @@ -349,8 +349,8 @@ contexts: - match: \" scope: punctuation.definition.string.end.terraform pop: true - - match: "{{identifer}}" - comment: Identifer label + - match: "{{identifier}}" + comment: Identifier label scope: entity.name.label.terraform - include: numeric_literals - include: attribute_access @@ -396,7 +396,7 @@ contexts: pop: true - include: object_for_expression - include: comments - - match: (?=({{identifer}}|\".*?\")\s*=) + - match: (?=({{identifier}}|\".*?\")\s*=) push: - object_value - assignment_operator @@ -414,7 +414,7 @@ contexts: - include: expressions object_key: - - match: '{{identifer}}' + - match: '{{identifier}}' scope: meta.mapping.key.terraform string.unquoted.terraform pop: true - match: (\").*?(\") @@ -456,7 +456,7 @@ contexts: - match: \. scope: punctuation.accessor.dot.terraform push: - - match: "{{identifer}}" + - match: "{{identifier}}" comment: Attribute access scope: variable.other.member.terraform pop: true @@ -476,13 +476,13 @@ contexts: # # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#attribute-definitions attribute_definition: - - match: (?=(\()?({{identifer}})(\))?\s*(\=(?![\=\>]))) + - match: (?=(\()?({{identifier}})(\))?\s*(\=(?![\=\>]))) push: - assignment_operator - attribute_key attribute_key: - - match: \((?={{identifer}}\)) + - match: \((?={{identifier}}\)) scope: punctuation.section.parens.begin.terraform set: - attribute_key_end @@ -495,7 +495,7 @@ contexts: - match: count\b scope: variable.declaration.terraform keyword.control.conditional.terraform pop: true - - match: '{{identifer}}' + - match: '{{identifier}}' scope: variable.declaration.terraform variable.other.readwrite.terraform pop: true @@ -509,7 +509,7 @@ contexts: # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#functions-and-function-calls # https://www.terraform.io/docs/language/expressions/function-calls.html functions: - - match: (?:({{predeclared_funcs}})|\b({{identifer}})\b)(\() + - match: (?:({{predeclared_funcs}})|\b({{identifier}})\b)(\() comment: Built-in function calls captures: 1: support.function.builtin.terraform @@ -573,7 +573,7 @@ contexts: - include: expressions - include: comments - include: comma - - match: "{{identifer}}" + - match: "{{identifier}}" comment: Local Identifiers scope: variable.other.readwrite.terraform @@ -581,7 +581,7 @@ contexts: # # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#structural-elements block: - - match: (?:\b({{terraform_known_blocks}})\b|({{identifer}}))(?=[\s\"\-[:word:]]*(\{)) + - match: (?:\b({{terraform_known_blocks}})\b|({{identifier}}))(?=[\s\"\-[:word:]]*(\{)) captures: 1: keyword.declaration.terraform 2: entity.name.type.terraform @@ -592,7 +592,7 @@ contexts: - match: \" scope: punctuation.definition.string.begin.terraform push: block_name_body - - match: "{{identifer}}" + - match: "{{identifier}}" scope: entity.name.label.terraform - match: \{ scope: punctuation.section.block.begin.terraform From 624943e3c58196ffe6f78f850978483da56220cd Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Wed, 7 Aug 2024 11:12:44 +0200 Subject: [PATCH 2/4] Properly match and scope identifiers They used to not be matched in some situations at all, which cause hyphens to not be recognized as part of the identifier as well. --- Terraform.sublime-syntax | 13 ++++++------- syntax_test_terraform.tf | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Terraform.sublime-syntax b/Terraform.sublime-syntax index 341f555..31bc1bb 100644 --- a/Terraform.sublime-syntax +++ b/Terraform.sublime-syntax @@ -123,13 +123,14 @@ contexts: # # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#expression-terms expressions: - - include: literal_values + - include: literals - include: operators - include: brackets - include: objects - include: attribute_access - include: functions - include: parens + - include: identifiers comma: - match: \, @@ -145,9 +146,6 @@ contexts: scope: punctuation.section.parens.end.terraform pop: true - include: expressions - - match: "{{identifier}}" - comment: Local Identifiers - scope: variable.other.readwrite.terraform # Literal Values: Numbers, Language Constants, and Strings # @@ -155,7 +153,7 @@ contexts: # but make the most sense to be part of this stack. # # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#literal-values - literal_values: + literals: - include: numeric_literals - include: language_constants - include: string_literals @@ -376,8 +374,8 @@ contexts: pop: true - include: comma - include: comments - - include: expressions - include: tuple_for_expression + - include: expressions # Objects: collection values # @@ -573,8 +571,9 @@ contexts: - include: expressions - include: comments - include: comma + + identifiers: - match: "{{identifier}}" - comment: Local Identifiers scope: variable.other.readwrite.terraform # Blocks: Identifier (StringLit|Identifier)* "{" Newline Body "}" Newline; diff --git a/syntax_test_terraform.tf b/syntax_test_terraform.tf index c3cad27..550d559 100644 --- a/syntax_test_terraform.tf +++ b/syntax_test_terraform.tf @@ -259,6 +259,21 @@ # ^^^^^^^^^^ constant.character.escape.terraform # ^ punctuation.definition.string.end.terraform +///////////////////////////////////////////////////////////////////// +// Identifiers +///////////////////////////////////////////////////////////////////// + + this_is_an_identifier +# ^^^^^^^^^^^^^^^^^^^^^ variable.other.readwrite.terraform + + identifier.member_access +# ^^^^^^^^^^ variable.other.readwrite.terraform +# ^ punctuation.accessor.dot.terraform +# ^^^^^^^^^^^^^ variable.other.member.terraform + + identifier-with-hyphens +# ^^^^^^^^^^^^^^^^^^^^^^^ variable.other.readwrite.terraform + ///////////////////////////////////////////////////////////////////// // STRING INTERPOLATION ///////////////////////////////////////////////////////////////////// @@ -1119,6 +1134,13 @@ # ^ punctuation.definition.string.begin.terraform # ^ punctuation.definition.string.end.terraform + provider = google-beta.impersonated +# ^^^^^^^^ variable.declaration.terraform variable.other.readwrite.terraform +# ^ keyword.operator.assignment.terraform +# ^^^^^^^^^^^ variable.other.readwrite.terraform +# ^ punctuation.accessor.dot.terraform +# ^^^^^^^^^^^^ variable.other.member.terraform + ///// // Meta-arguments From 1e93879f83bc6d6898528a950cbb60aaaa605a93 Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Wed, 7 Aug 2024 11:16:13 +0200 Subject: [PATCH 3/4] Fix comment matching after incomplete member access --- Terraform.sublime-syntax | 54 +++++++++++++++++++++++----------------- syntax_test_terraform.tf | 4 +++ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/Terraform.sublime-syntax b/Terraform.sublime-syntax index 31bc1bb..6e649c1 100644 --- a/Terraform.sublime-syntax +++ b/Terraform.sublime-syntax @@ -179,11 +179,13 @@ contexts: - match: '#|//' comment: Inline Comments scope: punctuation.definition.comment.terraform - push: - - meta_scope: comment.line.terraform - - match: $\n? - scope: punctuation.definition.comment.terraform - pop: true + push: inline_comment_body + + inline_comment_body: + - meta_scope: comment.line.terraform + - match: $\n? + scope: punctuation.definition.comment.terraform + pop: true # Block comments: start and end delimiters for multi-line comments. # @@ -193,11 +195,13 @@ contexts: - match: /\* comment: Block comments scope: punctuation.definition.comment.terraform - push: - - meta_scope: comment.block.terraform - - match: \*/ - scope: punctuation.definition.comment.terraform - pop: true + push: block_comments_body + + block_comments_body: + - meta_scope: comment.block.terraform + - match: \*/ + scope: punctuation.definition.comment.terraform + pop: true # Language Constants: booleans and `null`. # @@ -453,19 +457,23 @@ contexts: attribute_access: - match: \. scope: punctuation.accessor.dot.terraform - push: - - match: "{{identifier}}" - comment: Attribute access - scope: variable.other.member.terraform - pop: true - - match: \d+ - comment: Subscript - scope: constant.numeric.integer.terraform - pop: true - - match: \* - comment: Attribute-only splat - scope: keyword.operator.splat.terraform - pop: true + push: member + + member: + - include: comments + - match: "{{identifier}}" + comment: Attribute access + scope: variable.other.member.terraform + pop: true + - match: \d+ + comment: Subscript + scope: constant.numeric.integer.terraform + pop: true + - match: \* + comment: Attribute-only splat + scope: keyword.operator.splat.terraform + pop: true + - include: else_pop # Attribute Definition: Identifier "=" Expression Newline # diff --git a/syntax_test_terraform.tf b/syntax_test_terraform.tf index 550d559..b30789e 100644 --- a/syntax_test_terraform.tf +++ b/syntax_test_terraform.tf @@ -274,6 +274,10 @@ identifier-with-hyphens # ^^^^^^^^^^^^^^^^^^^^^^^ variable.other.readwrite.terraform + identifier. +// comment +# <- comment + ///////////////////////////////////////////////////////////////////// // STRING INTERPOLATION ///////////////////////////////////////////////////////////////////// From 06db3123974a7d09402fe06f746627a86de93c4d Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Wed, 7 Aug 2024 11:17:20 +0200 Subject: [PATCH 4/4] Declare hyphen as a sub-word separator --- Terraform.sublime-settings | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Terraform.sublime-settings b/Terraform.sublime-settings index 8bbee60..6f8a0d3 100644 --- a/Terraform.sublime-settings +++ b/Terraform.sublime-settings @@ -1,4 +1,7 @@ { "tab_size": 2, - "translate_tabs_to_spaces": true + "translate_tabs_to_spaces": true, + // Move '-' to sub-word separators + "word_separators": "./\\()\"':,.;<>~!@#$%^&*|+=[]{}`~?", + "sub_word_separators": "_-", }