vscode-terraform
vscode-terraform copied to clipboard
Support Color Syntax Highlighting for Templates (tmpl, etc)
Current Version
$ ~/.vscode/extensions/hashicorp.terraform-2.2.3/lsp/terraform-ls --version
0.8.0
Use-cases
As a developer, I would like to use terraform templates that have terraform parts with color syntax highlighting. Furthermore, with different file formats, e.g. YAML, JSON, bash, etc., I would like to have the host language have highlighted, in addition to the terraform language used for templating.
The only way to solve this is to use an alternative color syntax highlighting solution that supports colorizing templates.
Attempted Solutions
Proposal
As an example, I would have a template that creates called cluster_config.yaml.tmpl that has this snippet
vpc:
subnets:
private:
%{ for availability_zone, subnet_id in subnet_private ~}
${availability_zone}: { id: ${subnet_id} }
%{ endfor ~}
public:
%{ for availability_zone, subnet_id in subnet_public ~}
${availability_zone}: { id: ${subnet_id} }
%{ endfor ~}
I would expect values like ${variable} or %{stuff} to be highlighted as appopriate, such as reserved words in, for, endfor
For terraform code to supply the values, I would do something like this:
local {
vars = {
subnet_private = {
for id in data.aws_subnet_ids.private_subnet_ids.ids:
data.aws_subnet.private_subnets[id].availability_zone => id
}
subnet_public = {
for id in data.aws_subnet_ids.public_subnet_ids.ids:
data.aws_subnet.public_subnets[id].availability_zone => id
}
}
rendered_content = templatefile("${path.module}/cluster_config.yaml.tmpl", local.vars)
Related LSP methods
Not sure I understand the content from this.
References
I searched and did not find anything.
Upvote
I'm using JSON files templates with terraform .json.tpl, it would be nice to add syntax highlighting for those as well
Guys, .yaml.tpl too, please
For searchability I'll also reference the file extension .tftpl here (which is now the recommended extension instead of .tmpl according to https://www.terraform.io/language/functions/templatefile) for which the same feature would be incredibly useful.
FWIW you can get limited support by opening a file of the extension in question, then CTRL+SHIFT+p to open the command bar > Change language mode > Configure file association for .tmpl then type "terraform" and apply this change.
This results equivalently in the following addition to your user settings JSON:
"files.associations": {
"*.tmpl": "terraform"
}
Obviously it doesn't specifically handle the surrounding language (e.g. json/yaml) interleaved with the templating syntax, but it's a lot better than having zero syntax highlighting.
FYI We track support for various formats under https://github.com/hashicorp/vscode-terraform/issues/636 and *.tftpl (Terraform templates) is amongst the formats we are considering supporting, but don't have any concrete plans yet on how exactly and when.
For the template formats which involve YAML, JSON or any other non-Terraform or non-HCL language - I will discuss that with the rest of the team, but I expect these formats to be generally more difficult to support.
The more dominant component which needs to be highlighted in that context is arguably YAML/JSON, i.e. even if there was a solution for highlighting e.g. *.tf.yaml, this would in some way have to integrate with a YAML extension and rely on that extension to do the YAML part. i.e. we won't be reimplementing YAML highlighting or IntelliSense as part of Terraform extension. 😅 As such, supporting similar "embedded languages" isn't trivial in VS Code.
The same problem affects Markdown, although there the division between the languages is a little clearer/cleaner. On that note - if we were to tackle this non-trivial problem, I imagine Markdown would be the first format to support, also because it's a format frequently used for documentation.