Skip to content

Commit

Permalink
decoder: Implement hover for Keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jan 23, 2023
1 parent 48291ed commit 5f88303
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
23 changes: 22 additions & 1 deletion decoder/expr_keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package decoder

import (
"context"
"fmt"
"strings"

"github.com/hashicorp/hcl-lang/lang"
Expand Down Expand Up @@ -69,7 +70,27 @@ func (kw Keyword) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candi
}

func (kw Keyword) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
// TODO
eType, ok := kw.expr.(*hclsyntax.ScopeTraversalExpr)
if !ok {
return nil
}

if len(eType.Traversal) != 1 {
return nil
}

if eType.Traversal.RootName() == kw.cons.Keyword {
content := fmt.Sprintf("`%s` _%s_", kw.cons.Keyword, kw.cons.FriendlyName())
if kw.cons.Description.Value != "" {
content += "\n\n" + kw.cons.Description.Value
}

return &lang.HoverData{
Content: lang.Markdown(content),
Range: eType.SrcRange,
}
}

return nil
}

Expand Down
104 changes: 104 additions & 0 deletions decoder/expr_keyword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,107 @@ func TestCompletionAtPos_exprKeyword(t *testing.T) {
})
}
}

func TestHoverAtPos_exprKeyword(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
pos hcl.Pos
expectedHoverData *lang.HoverData
}{
{
"mismatching expression type",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{
Keyword: "foobar",
},
},
},
`attr = "foobar"`,
hcl.Pos{Line: 1, Column: 12, Byte: 11},
nil,
},
{
"mismatching keyword",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{
Keyword: "foobar",
},
},
},
`attr = barfoo`,
hcl.Pos{Line: 1, Column: 12, Byte: 11},
nil,
},
{
"matching keyword",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{
Keyword: "foobar",
},
},
},
`attr = foobar`,
hcl.Pos{Line: 1, Column: 12, Byte: 11},
&lang.HoverData{
Content: lang.Markdown("`foobar` _keyword_"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 14, Byte: 13},
},
},
},
{
"matching keyword with all metadata",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{
Keyword: "foobar",
Name: "custom name",
Description: lang.Markdown("custom _description_"),
},
},
},
`attr = foobar`,
hcl.Pos{Line: 1, Column: 12, Byte: 11},
&lang.HoverData{
Content: lang.Markdown("`foobar` _custom name_\n\ncustom _description_"),
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()
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)
}
})
}
}

0 comments on commit 5f88303

Please sign in to comment.