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

Support Color Syntax Highlighting for Templates (tmpl, etc) #496

Open
darkn3rd opened this issue Oct 11, 2020 · 7 comments
Open

Support Color Syntax Highlighting for Templates (tmpl, etc) #496

darkn3rd opened this issue Oct 11, 2020 · 7 comments
Labels

Comments

@darkn3rd
Copy link

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.

@radeksimko radeksimko transferred this issue from hashicorp/terraform-ls Oct 12, 2020
@AndrewGrachov

This comment has been minimized.

@Startouf
Copy link

Startouf commented Jun 4, 2021

I'm using JSON files templates with terraform .json.tpl, it would be nice to add syntax highlighting for those as well

@zorgzerg
Copy link

Guys, .yaml.tpl too, please

@jpogran jpogran added the enhancement New feature or request label Jan 31, 2022
@breathingdust breathingdust changed the title Support Color Syntanx Highlighting for Templates (tmpl, etc) Support Color Syntax Highlighting for Templates (tmpl, etc) Feb 4, 2022
@sparrowt
Copy link

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.

@sparrowt
Copy link

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.

@radeksimko
Copy link
Member

FYI We track support for various formats under #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.

@c4milo
Copy link

c4milo commented Feb 3, 2025

@radeksimko, I'm guessing this won't be done anytime soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants