Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes to identifiers (e.g. hyphens) #60

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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