Skip to content

Commit

Permalink
decoder: Implement semantic tokens for Object
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 21, 2023
1 parent 8d607e5 commit e28aef5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
6 changes: 0 additions & 6 deletions decoder/expr_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package decoder
import (
"context"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
Expand All @@ -17,11 +16,6 @@ type Object struct {
pathCtx *PathContext
}

func (obj Object) SemanticTokens(ctx context.Context) []lang.SemanticToken {
// TODO
return nil
}

func (obj Object) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
// TODO
return nil
Expand Down
46 changes: 46 additions & 0 deletions decoder/expr_object_semtok.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func (obj Object) SemanticTokens(ctx context.Context) []lang.SemanticToken {
eType, ok := obj.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return []lang.SemanticToken{}
}

if len(eType.Items) == 0 || len(obj.cons.Attributes) == 0 {
return []lang.SemanticToken{}
}

tokens := make([]lang.SemanticToken, 0)

for _, item := range eType.Items {
attrName, attrRange, ok := getRawObjectAttributeName(item.KeyExpr)
if !ok {
// invalid expression
continue
}

aSchema, ok := obj.cons.Attributes[attrName]
if !ok {
// unknown attribute
return nil
}

tokens = append(tokens, lang.SemanticToken{
Type: lang.TokenObjectKey,
Modifiers: lang.SemanticTokenModifiers{},
Range: *attrRange,
})

expr := newExpression(obj.pathCtx, item.ValueExpr, aSchema.Constraint)
tokens = append(tokens, expr.SemanticTokens(ctx)...)
}

return tokens
}
49 changes: 49 additions & 0 deletions decoder/expr_object_semtok_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package decoder

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func TestSemanticTokens_exprObject(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
expectedSemanticTokens []lang.SemanticToken
}{
// TODO
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
bodySchema := &schema.BodySchema{
Attributes: tc.attrSchema,
}

f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos)
d := testPathDecoder(t, &PathContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
})

ctx := context.Background()
tokens, err := d.SemanticTokensInFile(ctx, "test.tf")
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedSemanticTokens, tokens); diff != "" {
t.Fatalf("unexpected tokens: %s", diff)
}
})
}
}

0 comments on commit e28aef5

Please sign in to comment.