From 8845ee7f3f2b6963d0eb7d97921d63cf6a7b1845 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Sun, 27 Apr 2025 11:32:01 +0200 Subject: [PATCH] Improve ACL highlighting This commit... 1. adds ACL highlighting support to JSON values. 2. fixes string interpolation scopes within ACL variables. - remove string but not variable scope. --- JSON (Terraform).sublime-syntax | 1 + Terraform.sublime-syntax | 27 ++++++++++++++++++++------- tests/syntax_test_scope.tf | 32 +++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/JSON (Terraform).sublime-syntax b/JSON (Terraform).sublime-syntax index 7cf9831..78d3f2e 100644 --- a/JSON (Terraform).sublime-syntax +++ b/JSON (Terraform).sublime-syntax @@ -14,3 +14,4 @@ contexts: double-quoted-string-body: - meta_prepend: true - include: Packages/Terraform/Terraform.sublime-syntax#string_interpolation + - include: Packages/Terraform/Terraform.sublime-syntax#aws_acl diff --git a/Terraform.sublime-syntax b/Terraform.sublime-syntax index 12ba621..34e67ad 100644 --- a/Terraform.sublime-syntax +++ b/Terraform.sublime-syntax @@ -285,16 +285,24 @@ contexts: - match: '"' scope: punctuation.definition.string.end.terraform pop: true - - include: string_interpolation - match: '{{char_escapes}}' comment: Character Escapes scope: constant.character.escape.terraform - - match: (?=\barn:aws:) - push: aws_acl + - include: string_interpolation + - include: aws_acl aws_acl: + - match: (?=\barn:aws:) + push: aws_acl_body + + aws_acl_body: + - clear_scopes: 1 # Clear the string.* scope. - meta_scope: variable.language.acl.terraform - - include: string_interpolation + - match: ([$%]\{)(~)? + captures: + 1: punctuation.section.interpolation.begin.terraform + 2: keyword.operator.template.trim.left.terraform + push: acl_interpolation_body - match: :|/ scope: punctuation.separator.sequence.terraform - match: \* @@ -302,6 +310,11 @@ contexts: - match: (?![\w-]) pop: true + acl_interpolation_body: + - meta_scope: meta.interpolation.terraform + - meta_content_scope: source.terraform + - include: string_interpolation_body + # String Interpolation: ("${" | "${~") Expression ("}" | "~}" # # https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#templates @@ -313,9 +326,9 @@ contexts: push: string_interpolation_body string_interpolation_body: + - clear_scopes: 1 # Clear the string.* scope. - meta_scope: meta.interpolation.terraform - meta_content_scope: source.terraform - - clear_scopes: 1 # Clear the string.* scope. - match: (~)?(\}) captures: 1: keyword.operator.template.trim.right.terraform @@ -411,7 +424,7 @@ contexts: pop: true literal_label_body: - - meta_scope: string.quoted.double.terraform + - meta_scope: meta.string.terraform string.quoted.double.terraform - match: \" scope: punctuation.definition.string.end.terraform pop: true @@ -683,7 +696,7 @@ contexts: set: block_body block_name_body: - - meta_scope: string.quoted.double.terraform + - meta_scope: meta.string.terraform string.quoted.double.terraform - match: \" scope: punctuation.definition.string.end.terraform pop: true diff --git a/tests/syntax_test_scope.tf b/tests/syntax_test_scope.tf index 3fad3a2..7c7a134 100644 --- a/tests/syntax_test_scope.tf +++ b/tests/syntax_test_scope.tf @@ -3183,6 +3183,19 @@ resource "aws_iam_role_policy" "attach-inline-policy-1" { name = "xxx" role = aws_iam_role.execution-role.name + + res_arn = "arn:aws:lambda:*:*:function:${var.environment}-xxx" +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.terraform meta.string.terraform +# ^ string.quoted.double.terraform punctuation.definition.string.begin.terraform - variable +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ variable.language.acl.terraform +# ^^ punctuation.section.interpolation.begin.terraform +# ^^^^^^^^^^^^^^^ source.terraform source.terraform +# ^^^ variable.language.terraform +# ^ punctuation.accessor.dot.terraform +# ^^^^^^^^^^^ variable.other.member.terraform +# ^ punctuation.section.interpolation.end.terraform +# ^ string.quoted.double.terraform punctuation.definition.string.end.terraform - variable + policy = jsonencode({ Version = "2012-10-17" #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.terraform meta.function-call.terraform meta.braces.terraform @@ -3209,23 +3222,24 @@ resource "aws_iam_role_policy" "attach-inline-policy-1" { #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.terraform meta.function-call.terraform meta.mapping.value.json meta.sequence.json #^^^^^^^^^^^^^^^ meta.mapping.json # ^^^^^^^^^^ meta.mapping.key.json string.quoted.double.json +# ^^ meta.mapping.json +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.mapping.value.json meta.string.json - meta.interpolation +# ^^^^^^^^^^^^^^^^^^ meta.mapping.value.json meta.string.json meta.interpolation.terraform - string +# ^^^^^ meta.mapping.value.json meta.string.json - meta.interpolation +# ^ meta.mapping.json # ^ punctuation.definition.string.begin.json # ^ punctuation.definition.string.end.json -# ^^ meta.mapping.json # ^ punctuation.separator.key-value.json -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.mapping.value.json meta.string.json -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double.json -# ^ punctuation.definition.string.begin.json -# ^^^^^^^^^^^^^^^^^^ meta.interpolation.terraform +# ^ string.quoted.double.json punctuation.definition.string.begin.json - variable +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ variable.language.acl.terraform # ^^ punctuation.section.interpolation.begin.terraform -# ^^^^^^^^^^^^^^^ source.terraform +# ^^^^^^^^^^^^^^^ source.terraform source.terraform # ^^^ variable.language.terraform # ^ punctuation.accessor.dot.terraform # ^^^^^^^^^^^ variable.other.member.terraform # ^ punctuation.section.interpolation.end.terraform -# ^^^^^ string.quoted.double.json -# ^ punctuation.definition.string.end.json -# ^ meta.mapping.json punctuation.separator.sequence.json +# ^ string.quoted.double.json punctuation.definition.string.end.json - variable +# ^ punctuation.separator.sequence.json "Effect": "Allow" } ]