From 1aaa58b151ff62c95a30a6bb871e17119f3bab1d Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 24 Jan 2023 13:47:14 +0200 Subject: [PATCH] decoder: Implement semantic tokens for Keyword --- decoder/expr_keyword.go | 8 -- decoder/expr_keyword_semtok.go | 31 +++++++ decoder/expr_keyword_semtok_test.go | 123 ++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 decoder/expr_keyword_semtok.go create mode 100644 decoder/expr_keyword_semtok_test.go diff --git a/decoder/expr_keyword.go b/decoder/expr_keyword.go index 8c66655d..aebd3888 100644 --- a/decoder/expr_keyword.go +++ b/decoder/expr_keyword.go @@ -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" ) @@ -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 -} diff --git a/decoder/expr_keyword_semtok.go b/decoder/expr_keyword_semtok.go new file mode 100644 index 00000000..cc065cb6 --- /dev/null +++ b/decoder/expr_keyword_semtok.go @@ -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{} +} diff --git a/decoder/expr_keyword_semtok_test.go b/decoder/expr_keyword_semtok_test.go new file mode 100644 index 00000000..4911ee98 --- /dev/null +++ b/decoder/expr_keyword_semtok_test.go @@ -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) + } + }) + } +}