From 293f6f869fc7bb3fdbf1edd8e1bfd311050ac5b8 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 22 Mar 2022 09:38:38 +0000 Subject: [PATCH] avoid auto-assigning hcl-dependent semantic modifier --- decoder/semantic_tokens.go | 20 +++----------- decoder/semantic_tokens_test.go | 47 ++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/decoder/semantic_tokens.go b/decoder/semantic_tokens.go index b535814f..c1364810 100644 --- a/decoder/semantic_tokens.go +++ b/decoder/semantic_tokens.go @@ -28,7 +28,7 @@ func (d *PathDecoder) SemanticTokensInFile(filename string) ([]lang.SemanticToke return []lang.SemanticToken{}, nil } - tokens := d.tokensForBody(body, d.pathCtx.Schema, false, []lang.SemanticTokenModifier{}) + tokens := d.tokensForBody(body, d.pathCtx.Schema, []lang.SemanticTokenModifier{}) sort.Slice(tokens, func(i, j int) bool { return tokens[i].Range.Start.Byte < tokens[j].Range.Start.Byte @@ -37,9 +37,7 @@ func (d *PathDecoder) SemanticTokensInFile(filename string) ([]lang.SemanticToke return tokens, nil } -func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.BodySchema, - isDependent bool, parentModifiers []lang.SemanticTokenModifier) []lang.SemanticToken { - +func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.BodySchema, parentModifiers []lang.SemanticTokenModifier) []lang.SemanticToken { tokens := make([]lang.SemanticToken, 0) if bodySchema == nil { @@ -58,9 +56,6 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod attrModifiers := make([]lang.SemanticTokenModifier, 0) attrModifiers = append(attrModifiers, parentModifiers...) - if isDependent { - attrModifiers = append(attrModifiers, lang.TokenModifierDependent) - } attrModifiers = append(attrModifiers, attrSchema.SemanticTokenModifiers...) tokens = append(tokens, lang.SemanticToken{ @@ -82,10 +77,6 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod blockModifiers := make([]lang.SemanticTokenModifier, 0) blockModifiers = append(blockModifiers, parentModifiers...) - - if isDependent { - blockModifiers = append(blockModifiers, lang.TokenModifierDependent) - } blockModifiers = append(blockModifiers, blockSchema.SemanticTokenModifiers...) tokens = append(tokens, lang.SemanticToken{ @@ -104,9 +95,6 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod labelModifiers := make([]lang.SemanticTokenModifier, 0) labelModifiers = append(labelModifiers, parentModifiers...) - if labelSchema.IsDepKey { - labelModifiers = append(labelModifiers, lang.TokenModifierDependent) - } labelModifiers = append(labelModifiers, blockSchema.SemanticTokenModifiers...) labelModifiers = append(labelModifiers, labelSchema.SemanticTokenModifiers...) @@ -118,12 +106,12 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod } if block.Body != nil { - tokens = append(tokens, d.tokensForBody(block.Body, blockSchema.Body, false, blockModifiers)...) + tokens = append(tokens, d.tokensForBody(block.Body, blockSchema.Body, blockModifiers)...) } depSchema, _, ok := NewBlockSchema(blockSchema).DependentBodySchema(block.AsHCLBlock()) if ok { - tokens = append(tokens, d.tokensForBody(block.Body, depSchema, true, []lang.SemanticTokenModifier{})...) + tokens = append(tokens, d.tokensForBody(block.Body, depSchema, []lang.SemanticTokenModifier{})...) } } diff --git a/decoder/semantic_tokens_test.go b/decoder/semantic_tokens_test.go index 03b14b73..aa308858 100644 --- a/decoder/semantic_tokens_test.go +++ b/decoder/semantic_tokens_test.go @@ -106,13 +106,22 @@ func TestDecoder_SemanticTokensInFile_basic(t *testing.T) { "source": { Expr: schema.LiteralTypeOnly(cty.String), IsDeprecated: true, + SemanticTokenModifiers: lang.SemanticTokenModifiers{ + lang.TokenModifierDependent, + }, }, }, }, }, "resource": { Labels: []*schema.LabelSchema{ - {Name: "type", IsDepKey: true}, + { + Name: "type", + IsDepKey: true, + SemanticTokenModifiers: lang.SemanticTokenModifiers{ + lang.TokenModifierDependent, + }, + }, {Name: "name"}, }, }, @@ -164,8 +173,10 @@ resource "vault_auth_backend" "blah" { }, }, { // source - Type: lang.TokenAttrName, - Modifiers: []lang.SemanticTokenModifier{}, + Type: lang.TokenAttrName, + Modifiers: []lang.SemanticTokenModifier{ + lang.TokenModifierDependent, + }, Range: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -297,7 +308,13 @@ func TestDecoder_SemanticTokensInFile_dependentSchema(t *testing.T) { Blocks: map[string]*schema.BlockSchema{ "resource": { Labels: []*schema.LabelSchema{ - {Name: "type", IsDepKey: true}, + { + Name: "type", + IsDepKey: true, + SemanticTokenModifiers: lang.SemanticTokenModifiers{ + lang.TokenModifierDependent, + }, + }, {Name: "name"}, }, DependentBody: map[schema.SchemaKey]*schema.BodySchema{ @@ -457,10 +474,8 @@ resource "aws_instance" "beta" { }, }, { // instance_type - Type: lang.TokenAttrName, - Modifiers: []lang.SemanticTokenModifier{ - lang.TokenModifierDependent, - }, + Type: lang.TokenAttrName, + Modifiers: []lang.SemanticTokenModifier{}, Range: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -493,10 +508,8 @@ resource "aws_instance" "beta" { }, }, { // deprecated - Type: lang.TokenAttrName, - Modifiers: []lang.SemanticTokenModifier{ - lang.TokenModifierDependent, - }, + Type: lang.TokenAttrName, + Modifiers: []lang.SemanticTokenModifier{}, Range: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -553,8 +566,9 @@ func TestDecoder_SemanticTokensInFile_customModifiers(t *testing.T) { Expr: schema.LiteralTypeOnly(cty.Number), }, "source": { - Expr: schema.LiteralTypeOnly(cty.String), - IsDeprecated: true, + Expr: schema.LiteralTypeOnly(cty.String), + IsDeprecated: true, + SemanticTokenModifiers: lang.SemanticTokenModifiers{lang.TokenModifierDependent}, }, }, }, @@ -565,7 +579,7 @@ func TestDecoder_SemanticTokensInFile_customModifiers(t *testing.T) { { Name: "type", IsDepKey: true, - SemanticTokenModifiers: lang.SemanticTokenModifiers{"type"}, + SemanticTokenModifiers: lang.SemanticTokenModifiers{"type", lang.TokenModifierDependent}, }, { Name: "name", @@ -661,6 +675,7 @@ resource "vault_auth_backend" "blah" { Type: lang.TokenAttrName, Modifiers: []lang.SemanticTokenModifier{ lang.SemanticTokenModifier("module"), + lang.TokenModifierDependent, }, Range: hcl.Range{ Filename: "test.tf", @@ -751,9 +766,9 @@ resource "vault_auth_backend" "blah" { { // vault_auth_backend Type: lang.TokenBlockLabel, Modifiers: []lang.SemanticTokenModifier{ - lang.TokenModifierDependent, lang.SemanticTokenModifier("resource"), lang.SemanticTokenModifier("type"), + lang.TokenModifierDependent, }, Range: hcl.Range{ Filename: "test.tf",