Skip to content

Commit

Permalink
avoid auto-assigning hcl-dependent semantic modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 22, 2022
1 parent b316ea3 commit 293f6f8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
20 changes: 4 additions & 16 deletions decoder/semantic_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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...)

Expand All @@ -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{})...)
}
}

Expand Down
47 changes: 31 additions & 16 deletions decoder/semantic_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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},
},
},
},
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 293f6f8

Please sign in to comment.