hashicorp / vscode-terraform

HashiCorp Terraform VSCode extension
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Mozilla Public License 2.0
929 stars 180 forks source link

Map keys are inconsistently highlighted #1138

Open alexjurkiewicz opened 2 years ago

alexjurkiewicz commented 2 years ago

Versions

This bug is reproducible in:

Extension

v2.22.0

Language Server

0.27.0

VS Code

1.67.1

Operating System

macOS 12.3.1

Terraform Version

Terraform v1.1.9
on darwin_amd64

Steps To Reproduce

locals {
  mymap = {
    key_one           = "foo"
    "key_two"         = "bar"
    (local.key_three) = "baz"
  }
}

Expected Behavior

I would expect key_one and key_two to use the same colour, since they are both strings.

Actual Behavior

Screen Shot 2022-05-18 at 1 32 34 pm

Additional context

radeksimko commented 2 years ago

Hi @alexjurkiewicz Thanks for the report.

Given how complex highlighting is I feel it's appropriate to share some context here.

I know you filed this under the vscode-terraform repository, so you probably already know this, but I just want to highlight that this is a Terraform-specific problem. Unquoted map keys are what HCL calls "traversals" (basically) and aside from the obvious edge cases with constants like true or false, these can have different interpretations depending on the language (e.g. Terraform). See https://go.dev/play/p/l4kYwVbHfZg

There are theoretically two ways we could address this.

  1. We could update the handling of map keys in the Terraform TM grammar (probably around here), but there's some costs associated with maintaining that level of complexity in a TextMate grammar full of regexes.
  2. We could update semantic highlighting rules in the Terraform language server, which in turn uses an HCL generic library hcl-lang - this means we'd also need to give the library some ability to treat map keys differently based on schema/product and then make use of it somewhere here.

Before jumping to solutions though:

alexjurkiewicz commented 2 years ago

Yes, that all makes sense. I hope you can make some progress 🙏