diff --git a/decoder/expr_type_declaration.go b/decoder/expr_type_declaration.go index f1ff92d8..f8783c82 100644 --- a/decoder/expr_type_declaration.go +++ b/decoder/expr_type_declaration.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" ) @@ -18,11 +15,6 @@ type TypeDeclaration struct { // TODO: optional attribute mode } -func (td TypeDeclaration) SemanticTokens(ctx context.Context) []lang.SemanticToken { - // TODO - return nil -} - func isSingleArgTypeName(name string) bool { return name == "list" || name == "set" || name == "map" } diff --git a/decoder/expr_type_declaration_semtok.go b/decoder/expr_type_declaration_semtok.go new file mode 100644 index 00000000..d005c42f --- /dev/null +++ b/decoder/expr_type_declaration_semtok.go @@ -0,0 +1,129 @@ +package decoder + +import ( + "context" + + "github.com/hashicorp/hcl-lang/lang" + "github.com/hashicorp/hcl/v2/hclsyntax" +) + +func (td TypeDeclaration) SemanticTokens(ctx context.Context) []lang.SemanticToken { + switch eType := td.expr.(type) { + case *hclsyntax.ScopeTraversalExpr: + if len(eType.Traversal) != 1 { + return []lang.SemanticToken{} + } + + if isPrimitiveTypeDeclaration(eType.Traversal.RootName()) { + return []lang.SemanticToken{ + { + Type: lang.TokenTypePrimitive, + Modifiers: []lang.SemanticTokenModifier{}, + Range: eType.Range(), + }, + } + } + case *hclsyntax.FunctionCallExpr: + if isSingleArgTypeName(eType.Name) { + tokens := make([]lang.SemanticToken, 0) + + tokens = append(tokens, lang.SemanticToken{ + Type: lang.TokenTypeCapsule, + Modifiers: []lang.SemanticTokenModifier{}, + Range: eType.NameRange, + }) + + if len(eType.Args) == 0 { + return tokens + } + + if len(eType.Args) == 1 { + cons := TypeDeclaration{ + expr: eType.Args[0], + pathCtx: td.pathCtx, + } + tokens = append(tokens, cons.SemanticTokens(ctx)...) + + return tokens + } + + return []lang.SemanticToken{} + } + + if eType.Name == "object" { + tokens := make([]lang.SemanticToken, 0) + tokens = append(tokens, lang.SemanticToken{ + Type: lang.TokenTypeCapsule, + Modifiers: []lang.SemanticTokenModifier{}, + Range: eType.NameRange, + }) + + if len(eType.Args) == 0 { + return tokens + } + + if len(eType.Args) == 1 { + objExpr, isObject := eType.Args[0].(*hclsyntax.ObjectConsExpr) + if !isObject { + return []lang.SemanticToken{} + } + + cons := TypeDeclaration{ + expr: objExpr, + pathCtx: td.pathCtx, + insideObject: true, + } + + tokens = append(tokens, cons.SemanticTokens(ctx)...) + + return tokens + } + + return []lang.SemanticToken{} + } + + // multi argument type (tuple) + if eType.Name == "tuple" { + tokens := make([]lang.SemanticToken, 0) + tokens = append(tokens, lang.SemanticToken{ + Type: lang.TokenTypeCapsule, + Modifiers: []lang.SemanticTokenModifier{}, + Range: eType.NameRange, + }) + + for _, expr := range eType.Args { + cons := TypeDeclaration{ + expr: expr, + pathCtx: td.pathCtx, + } + tokens = append(tokens, cons.SemanticTokens(ctx)...) + } + + return tokens + } + case *hclsyntax.ObjectConsExpr: + if !td.insideObject { + // no tokens in bare object notation w/out object() + return []lang.SemanticToken{} + } + + tokens := make([]lang.SemanticToken, 0) + + for _, item := range eType.Items { + tokens = append(tokens, lang.SemanticToken{ + Type: lang.TokenAttrName, + Modifiers: []lang.SemanticTokenModifier{}, + Range: item.KeyExpr.Range(), + }) + + cons := TypeDeclaration{ + expr: item.ValueExpr, + pathCtx: td.pathCtx, + } + tokens = append(tokens, cons.SemanticTokens(ctx)...) + } + + return tokens + } + return nil +} diff --git a/decoder/expr_type_declaration_semtok_test.go b/decoder/expr_type_declaration_semtok_test.go new file mode 100644 index 00000000..cecfc58a --- /dev/null +++ b/decoder/expr_type_declaration_semtok_test.go @@ -0,0 +1,393 @@ +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_exprTypeDeclaration(t *testing.T) { + testCases := []struct { + testName string + attrSchema map[string]*schema.AttributeSchema + cfg string + expectedTokens []lang.SemanticToken + }{ + { + "primitive type", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = string`, + []lang.SemanticToken{ + { + Type: lang.TokenAttrName, + 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: lang.TokenTypePrimitive, + 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}, + }, + }, + }, + }, + { + "invalid primitive type", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = foobar`, + []lang.SemanticToken{ + { + Type: lang.TokenAttrName, + 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}, + }, + }, + }, + }, + { + "single-argument complex type", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = list(string)`, + []lang.SemanticToken{ + { + Type: lang.TokenAttrName, + 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: lang.TokenTypeCapsule, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 13, Byte: 12}, + End: hcl.Pos{Line: 1, Column: 19, Byte: 18}, + }, + }, + }, + }, + { + "tuple", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = tuple(string, bool, number)`, + []lang.SemanticToken{ + { + Type: lang.TokenAttrName, + 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: lang.TokenTypeCapsule, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 1, Column: 13, Byte: 12}, + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 14, Byte: 13}, + End: hcl.Pos{Line: 1, Column: 20, Byte: 19}, + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 22, Byte: 21}, + End: hcl.Pos{Line: 1, Column: 26, Byte: 25}, + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 28, Byte: 27}, + End: hcl.Pos{Line: 1, Column: 34, Byte: 33}, + }, + }, + }, + }, + { + "object", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = object({ + foo = string + bar = number +})`, + []lang.SemanticToken{ + { + Type: lang.TokenAttrName, + 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: lang.TokenTypeCapsule, + 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}, + }, + }, + { + Type: lang.TokenAttrName, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 2, Column: 3, Byte: 18}, + End: hcl.Pos{Line: 2, Column: 6, Byte: 21}, + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 2, Column: 9, Byte: 24}, + End: hcl.Pos{Line: 2, Column: 15, Byte: 30}, + }, + }, + { + Type: lang.TokenAttrName, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 3, Column: 3, Byte: 33}, + End: hcl.Pos{Line: 3, Column: 6, Byte: 36}, + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 3, Column: 9, Byte: 39}, + End: hcl.Pos{Line: 3, Column: 15, Byte: 45}, + }, + }, + }, + }, + { + "object with complex types", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = object({ + foo = list(string) + bar = tuple(bool, string) + baz = object({ + paw = string + }) +})`, + []lang.SemanticToken{ + { + Type: lang.TokenAttrName, + 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: lang.TokenTypeCapsule, + 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}, + }, + }, + { + Type: lang.TokenAttrName, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 2, Column: 3, Byte: 18}, + End: hcl.Pos{Line: 2, Column: 6, Byte: 21}, + }, + }, + { + Type: lang.TokenTypeCapsule, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 2, Column: 9, Byte: 24}, + End: hcl.Pos{Line: 2, Column: 13, Byte: 28}, + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 14, Byte: 29}, + End: hcl.Pos{Line: 2, Column: 20, Byte: 35}, + Filename: "test.tf", + }, + }, + { + Type: lang.TokenAttrName, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Start: hcl.Pos{Line: 3, Column: 3, Byte: 39}, + End: hcl.Pos{Line: 3, Column: 6, Byte: 42}, + Filename: "test.tf", + }, + }, + { + Type: lang.TokenTypeCapsule, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Start: hcl.Pos{Line: 3, Column: 9, Byte: 45}, + End: hcl.Pos{Line: 3, Column: 14, Byte: 50}, + Filename: "test.tf", + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Start: hcl.Pos{Line: 3, Column: 15, Byte: 51}, + End: hcl.Pos{Line: 3, Column: 19, Byte: 55}, + Filename: "test.tf", + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 3, Column: 21, Byte: 57}, + End: hcl.Pos{Line: 3, Column: 27, Byte: 63}, + }, + }, + { + Type: lang.TokenAttrName, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 4, Column: 3, Byte: 67}, + End: hcl.Pos{Line: 4, Column: 6, Byte: 70}, + }, + }, + { + Type: lang.TokenTypeCapsule, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 4, Column: 9, Byte: 73}, + End: hcl.Pos{Line: 4, Column: 15, Byte: 79}, + }, + }, + { + Type: lang.TokenAttrName, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 5, Column: 5, Byte: 86}, + End: hcl.Pos{Line: 5, Column: 8, Byte: 89}, + }, + }, + { + Type: lang.TokenTypePrimitive, + Modifiers: lang.SemanticTokenModifiers{}, + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 5, Column: 11, Byte: 92}, + End: hcl.Pos{Line: 5, Column: 17, Byte: 98}, + }, + }, + }, + }, + } + 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() + hoverData, err := d.SemanticTokensInFile(ctx, "test.tf") + if err != nil { + t.Fatal(err) + } + + if diff := cmp.Diff(tc.expectedTokens, hoverData); diff != "" { + t.Fatalf("unexpected tokens: %s", diff) + } + }) + } +}