Skip to content

Support HCL objects natively - #107

Merged
FichteFoll merged 8 commits into
masterfrom
feature/native-hcl-objects
Nov 10, 2026
Merged

FichteFoll merged 8 commits into
masterfrom
feature/native-hcl-objects

Conversation

@FichteFoll

Copy link
Copy Markdown
Member

The previous object implementation (#71) was naïve and only supported the JSON notation but HCL objects are much more … terrible than that. Notably, they support arbitrary expressions in the key position and only when an identifier fails to be looked up it is treated as an unquoted string. We cannot resolve this from our syntax definition, of course, so we treat any single identifier as an unquoted string key, while treating the rest as a key expression (with the appropriate meta.mapping.key scope).

Additionally, = and : are equivalent separator characters and we are scoping both as punctuation.separator.key-value.terraform.

Closes #95.

PS: Turns out this wasn't so hard as I initially anticipated but Claude did all the work anyway.

FichteFoll and others added 3 commits November 5, 2026 13:40
`=` between an object key and value is now
`punctuation.separator.key-value.terraform`, matching `:` and the `=>`
of object `for` expressions.
Block attribute `=` keeps `keyword.operator.assignment.terraform`.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The scope was misspelled as `punctuation.section.baces.begin/end`,
so the basic brace folding rule never matched and object constructor
`{ ... }` expressions did not fold through it.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Object constructors only recognised `identifier =`, `"string" =` and
`(expr) =` elements and handed everything else to the JSON syntax's
`object-body`, so valid HCL such as `aws.secrets-manager = ...`,
`foo: 1` or `"a": var.x` was highlighted as `invalid.illegal.*.json`.

Parse elements following the HCL grammar instead: the key is an
identifier or any expression (`object-key`, `object-key-expression`),
followed by `=` or `:` (`object-key-separator`) and a full Terraform
expression. `:` is scoped `punctuation.separator.key-value.terraform`.
A `for` expression is only recognised as the first token of an object
(`object-begin`), as in hclsyntax `parseObjectCons`.

Fixes #95

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`JSON (Terraform)` gained Terraform string interpolation and line
comments to serve as the fallback for objects in
`Terraform.sublime-syntax`, which no longer uses it.
State files hold resolved values and no comments, so a literal `${...}`
in a value is not interpolation.

Keep the ACL and IP address highlighting of string values, and add a
syntax test for `.tfstate` files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@FichteFoll FichteFoll added this to the 2.1.0 milestone Nov 5, 2026
@FichteFoll
FichteFoll requested review from deathaxe and michaelblyons and removed request for michaelblyons November 9, 2026 10:37

@deathaxe deathaxe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separating HCL from JSON is a reasonable step forward. Changes look good in general. Some comments and suggestions, following.

Comment thread Terraform.sublime-syntax Resolved
Comment thread Terraform.sublime-syntax Resolved
Comment thread Terraform.sublime-syntax
pop: 1
- match: (?=\S)
push:
- object-value

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object-value is not scoped meta.mapping.value nor is the separator scoped meta.mapping.terraform.

With keys being scoped meta.mapping.key and separators punctuation.separator.key-value, it is probably worth adding full meta.mapping.[key|value] support in addition to or replacing more general meta.braces.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, but I'd prefer to do this in a follow-up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change wasn't that involved and it is still related to object matching, so I added it here after all.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With braces of blocks being scoped punctuation.section.block, does it make sense to change braces of objects to punctuation.section.mapping?

It would likely - in a follow up - also advice to move tuples to "sequence" scope.

Comment thread tests/syntax_test_scope.tf Resolved
Commas between object elements are now scoped
`punctuation.separator.sequence.terraform`, matching what the previous
JSON fallback produced for JSON-style objects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@FichteFoll
FichteFoll requested a review from deathaxe November 10, 2026 12:16
@FichteFoll
FichteFoll force-pushed the feature/native-hcl-objects branch 2 times, most recently from 655b059 to 5311d3b Compare November 10, 2026 13:34
@FichteFoll
FichteFoll added this pull request to stack #109 November 10, 2026 13:53
FichteFoll and others added 3 commits November 10, 2026 14:56
Use `meta.mapping.terraform` for object bodies in place of
`meta.braces.terraform`, following the scope naming conventions
and the built-in JSON syntax.

Existing `- meta.mapping` exclusions on separators and values become
`- meta.mapping.key`, which is what they are about; the exclusions
outside objects keep the full `- meta.mapping`.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Follow the built-in JSON syntax: keys and values clear the object's
`meta.mapping.terraform` with `clear_scopes: 1` and are scoped
`meta.mapping.key.terraform` and the new `meta.mapping.value.terraform`,
so they are siblings of the mapping instead of nested in it.

The value scope ends before trailing whitespace, a trailing line
comment and a trailing single-line block comment. Separators, commas,
and whitespace and comments between elements keep
`meta.mapping.terraform`. Object `for` expressions are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Commas in tuples, function arguments and for-expression headers now
use `punctuation.separator.sequence.terraform`, matching the commas
between object elements (PR #107 review).

Color schemes targeting `punctuation.separator` still match.
The `/` and `:` in IP addresses keep `punctuation.separator.terraform`.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@FichteFoll
FichteFoll merged commit fbe94ba into master Nov 10, 2026
2 checks passed
@FichteFoll
FichteFoll deleted the feature/native-hcl-objects branch November 10, 2026 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aliased Terraform providers highlighted as if they are typos

2 participants