Skip to content

Commit

Permalink
decoder: Implement semantic tokens for Keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jan 24, 2023
1 parent e9c1614 commit 1aaa58b
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 8 deletions.
8 changes: 0 additions & 8 deletions decoder/expr_keyword.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
)
Expand All @@ -12,8 +9,3 @@ type Keyword struct {
expr hcl.Expression
cons schema.Keyword
}

func (kw Keyword) SemanticTokens(ctx context.Context) []lang.SemanticToken {
// TODO
return nil
}
31 changes: 31 additions & 0 deletions decoder/expr_keyword_semtok.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package decoder

import (
"context"

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

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

if len(eType.Traversal) != 1 {
return []lang.SemanticToken{}
}

if eType.Traversal.RootName() == kw.cons.Keyword {
return []lang.SemanticToken{
{
Type: lang.TokenKeyword,
Modifiers: []lang.SemanticTokenModifier{},
Range: eType.Range(),
},
}
}

return []lang.SemanticToken{}
}
123 changes: 123 additions & 0 deletions decoder/expr_keyword_semtok_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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_exprKeyword(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
expectedSemanticTokens []lang.SemanticToken
}{
{
"mismatching expression type",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{
Keyword: "foobar",
},
},
},
`attr = "foobar"`,
[]lang.SemanticToken{
{
Type: "hcl-attrName",
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
},
},
{
"mismatching keyword",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{
Keyword: "foobar",
},
},
},
`attr = barfoo`,
[]lang.SemanticToken{
{
Type: "hcl-attrName",
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
},
},
{
"matching keyword",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{
Keyword: "foobar",
},
},
},
`attr = foobar`,
[]lang.SemanticToken{
{
Type: "hcl-attrName",
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 5, Byte: 4},
},
},
{
Type: "hcl-keyword",
Modifiers: lang.SemanticTokenModifiers{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 14, Byte: 13},
},
},
},
},
}
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 1aaa58b

Please sign in to comment.