Skip to content

Commit

Permalink
Merge pull request #60 from SublimeText/bugfix/hypenated-identifiers
Browse files Browse the repository at this point in the history
Some fixes to identifiers (e.g. hyphens)
  • Loading branch information
FichteFoll authored Aug 7, 2024
2 parents a923e44 + 06db312 commit c1bcc4f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 44 deletions.
5 changes: 4 additions & 1 deletion Terraform.sublime-settings
Original file line number Diff line number Diff line change
@@ -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": "_-",
}
93 changes: 50 additions & 43 deletions Terraform.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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: \,
Expand All @@ -145,17 +146,14 @@ contexts:
scope: punctuation.section.parens.end.terraform
pop: true
- include: expressions
- match: "{{identifer}}"
comment: Local Identifiers
scope: variable.other.readwrite.terraform

# Literal Values: Numbers, Language Constants, and Strings
#
# Strings are _technically_ part of the "expression sub-language",
# 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
Expand All @@ -181,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.
#
Expand All @@ -195,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`.
#
Expand Down Expand Up @@ -278,7 +280,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
Expand Down Expand Up @@ -349,8 +351,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
Expand All @@ -376,8 +378,8 @@ contexts:
pop: true
- include: comma
- include: comments
- include: expressions
- include: tuple_for_expression
- include: expressions

# Objects: collection values
#
Expand All @@ -396,7 +398,7 @@ contexts:
pop: true
- include: object_for_expression
- include: comments
- match: (?=({{identifer}}|\".*?\")\s*=)
- match: (?=({{identifier}}|\".*?\")\s*=)
push:
- object_value
- assignment_operator
Expand All @@ -414,7 +416,7 @@ contexts:
- include: expressions

object_key:
- match: '{{identifer}}'
- match: '{{identifier}}'
scope: meta.mapping.key.terraform string.unquoted.terraform
pop: true
- match: (\").*?(\")
Expand Down Expand Up @@ -455,19 +457,23 @@ contexts:
attribute_access:
- match: \.
scope: punctuation.accessor.dot.terraform
push:
- match: "{{identifer}}"
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
#
Expand All @@ -476,13 +482,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
Expand All @@ -495,7 +501,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

Expand All @@ -509,7 +515,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
Expand Down Expand Up @@ -573,15 +579,16 @@ contexts:
- include: expressions
- include: comments
- include: comma
- match: "{{identifer}}"
comment: Local Identifiers

identifiers:
- match: "{{identifier}}"
scope: variable.other.readwrite.terraform

# Blocks: Identifier (StringLit|Identifier)* "{" Newline Body "}" Newline;
#
# 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
Expand All @@ -592,7 +599,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
Expand Down
26 changes: 26 additions & 0 deletions syntax_test_terraform.tf
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@
# ^^^^^^^^^^ 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

identifier.
// comment
# <- comment

/////////////////////////////////////////////////////////////////////
// STRING INTERPOLATION
/////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1119,6 +1138,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
Expand Down

0 comments on commit c1bcc4f

Please sign in to comment.