From 7d5029378d15c89f541987298dc920702c57b592 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 17 Jan 2023 14:39:57 +0000 Subject: [PATCH] decoder: Implement hover for TypeDeclaration --- decoder/expr_type_declaration.go | 141 ++++++++++++++++++++++- decoder/expr_type_declaration_test.go | 154 ++++++++++++++++++++++++++ 2 files changed, 294 insertions(+), 1 deletion(-) diff --git a/decoder/expr_type_declaration.go b/decoder/expr_type_declaration.go index 7c71fc85..b1c974d9 100644 --- a/decoder/expr_type_declaration.go +++ b/decoder/expr_type_declaration.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/hcl-lang/lang" "github.com/hashicorp/hcl-lang/schema" "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/ext/typeexpr" "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/zclconf/go-cty/cty" ) @@ -331,7 +332,145 @@ func innerObjectTypeAsCompletionCandidates(editRange hcl.Range) []lang.Candidate } func (td TypeDeclaration) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData { - // TODO + switch eType := td.expr.(type) { + case *hclsyntax.ScopeTraversalExpr: + if len(eType.Traversal) != 1 { + return nil + } + + if eType.Range().ContainsPos(pos) { + typ, _ := typeexpr.TypeConstraint(eType) + content, err := hoverContentForType(typ, 0) + if err != nil { + return nil + } + return &lang.HoverData{ + Content: lang.Markdown(content), + Range: eType.Range(), + } + } + case *hclsyntax.FunctionCallExpr: + // position in complex type name + if eType.NameRange.ContainsPos(pos) { + typ, _ := typeexpr.TypeConstraint(eType) + content, err := hoverContentForType(typ, 0) + if err != nil { + return nil + } + return &lang.HoverData{ + Content: lang.Markdown(content), + Range: eType.Range(), + } + } + + // position inside paranthesis + if hcl.RangeBetween(eType.OpenParenRange, eType.CloseParenRange).ContainsPos(pos) { + if isSingleArgTypeName(eType.Name) { + if len(eType.Args) == 0 { + return nil + } + + if len(eType.Args) == 1 && eType.Args[0].Range().ContainsPos(pos) { + cons := TypeDeclaration{ + expr: eType.Args[0], + } + return cons.HoverAtPos(ctx, pos) + } + + return nil + } + + if eType.Name == "object" { + if len(eType.Args) == 0 { + return nil + } + + if len(eType.Args) == 1 { + objExpr, isObject := eType.Args[0].(*hclsyntax.ObjectConsExpr) + if !isObject { + return nil + } + if !eType.Args[0].Range().ContainsPos(pos) { + return nil + } + + closeRange := hcl.Range{ + Filename: objExpr.Range().Filename, + Start: hcl.Pos{ + Line: objExpr.Range().End.Line, + Column: objExpr.Range().End.Column - 1, + Byte: objExpr.Range().End.Byte - 1, + }, + End: objExpr.Range().End, + } + if objExpr.OpenRange.ContainsPos(pos) || closeRange.ContainsPos(pos) { + typ, _ := typeexpr.TypeConstraint(eType) + content, err := hoverContentForType(typ, 0) + if err != nil { + return nil + } + return &lang.HoverData{ + Content: lang.Markdown(content), + Range: eType.Range(), + } + } + + cons := TypeDeclaration{ + expr: objExpr, + insideObject: true, + } + return cons.HoverAtPos(ctx, pos) + } + + return nil + } + + // multi argument type (tuple) + if eType.Name == "tuple" { + if len(eType.Args) == 0 { + return nil + } + + for _, expr := range eType.Args { + if expr.Range().ContainsPos(pos) { + cons := TypeDeclaration{ + expr: expr, + } + return cons.HoverAtPos(ctx, pos) + } + } + + return nil + } + } + case *hclsyntax.ObjectConsExpr: + if !td.insideObject { + // reject hover in bare object notation w/out object() + return nil + } + + if len(eType.Items) == 0 { + return nil + } + + for _, item := range eType.Items { + if item.KeyExpr.Range().ContainsPos(pos) { + val, _ := item.KeyExpr.Value(nil) + typ, _ := typeexpr.TypeConstraint(item.ValueExpr) + return &lang.HoverData{ + Content: lang.Markdown(fmt.Sprintf("`%s` = _%s_", val.AsString(), typ.FriendlyNameForConstraint())), + Range: hcl.RangeBetween(item.KeyExpr.Range(), item.ValueExpr.Range()), + } + } + if item.ValueExpr.Range().ContainsPos(pos) { + cons := TypeDeclaration{ + expr: item.ValueExpr, + } + return cons.HoverAtPos(ctx, pos) + } + } + return nil + } return nil } diff --git a/decoder/expr_type_declaration_test.go b/decoder/expr_type_declaration_test.go index 598861a0..c26a9cc3 100644 --- a/decoder/expr_type_declaration_test.go +++ b/decoder/expr_type_declaration_test.go @@ -362,3 +362,157 @@ func TestCompletionAtPos_exprTypeDeclaration(t *testing.T) { }) } } + +func TestHoverAtPos_exprTypeDeclaration(t *testing.T) { + testCases := []struct { + testName string + attrSchema map[string]*schema.AttributeSchema + cfg string + pos hcl.Pos + expectedHoverData *lang.HoverData + }{ + { + "primitive type", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = string`, + hcl.Pos{Line: 1, Column: 11, Byte: 10}, + &lang.HoverData{ + Content: lang.Markdown(`_string_`), + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 1, Column: 14, Byte: 13}, + }, + }, + }, + { + "list type on list", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = list(string)`, + hcl.Pos{Line: 1, Column: 10, Byte: 9}, + &lang.HoverData{ + Content: lang.Markdown(`_list of string_`), + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 1, Column: 20, Byte: 19}, + }, + }, + }, + { + "list type on element type", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = list(string)`, + hcl.Pos{Line: 1, Column: 16, Byte: 15}, + &lang.HoverData{ + Content: lang.Markdown(`_string_`), + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 13, Byte: 12}, + End: hcl.Pos{Line: 1, Column: 19, Byte: 18}, + }, + }, + }, + { + "object type on object", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = object({ + foo = string +}) +`, + hcl.Pos{Line: 1, Column: 11, Byte: 10}, + &lang.HoverData{ + Content: lang.Markdown("```\n{\n foo = string\n}\n```\n_object_"), + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 3, Column: 3, Byte: 33}, + }, + }, + }, + { + "object type on attribute name", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = object({ + foo = string +}) +`, + hcl.Pos{Line: 2, Column: 5, Byte: 20}, + &lang.HoverData{ + Content: lang.Markdown("`foo` = _string_"), + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 2, Column: 3, Byte: 18}, + End: hcl.Pos{Line: 2, Column: 15, Byte: 30}, + }, + }, + }, + { + "object type on attribute value", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.TypeDeclaration{}, + }, + }, + `attr = object({ + foo = string +}) +`, + hcl.Pos{Line: 2, Column: 11, Byte: 26}, + &lang.HoverData{ + Content: lang.Markdown("_string_"), + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{Line: 2, Column: 9, Byte: 24}, + End: hcl.Pos{Line: 2, Column: 15, Byte: 30}, + }, + }, + }, + } + + 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.HoverAtPos(ctx, "test.tf", tc.pos) + if err != nil { + t.Fatal(err) + } + + if diff := cmp.Diff(tc.expectedHoverData, hoverData); diff != "" { + t.Fatalf("unexpected hover data: %s", diff) + } + }) + } +}