Skip to content

Commit

Permalink
decoder/schema: Add support for Keyword as Constraint (#184)
Browse files Browse the repository at this point in the history
* schema: Implement EmptyCompletionData for Keyword

* decoder: Implement completion for Keyword

* decoder: Implement hover for Keyword

* decoder: Implement semantic tokens for Keyword
  • Loading branch information
radeksimko authored Jan 27, 2023
1 parent 31ff628 commit 053e555
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 20 deletions.
18 changes: 0 additions & 18 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,18 +9,3 @@ type Keyword struct {
expr hcl.Expression
cons schema.Keyword
}

func (kw Keyword) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
// TODO
return nil
}

func (kw Keyword) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
// TODO
return nil
}

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

import (
"context"
"strings"

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

func (kw Keyword) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
if isEmptyExpression(kw.expr) {
return []lang.Candidate{
{
Label: kw.cons.Keyword,
Detail: kw.cons.FriendlyName(),
Description: kw.cons.Description,
Kind: lang.KeywordCandidateKind,
TextEdit: lang.TextEdit{
NewText: kw.cons.Keyword,
Snippet: kw.cons.Keyword,
Range: hcl.Range{
Filename: kw.expr.Range().Filename,
Start: pos,
End: pos,
},
},
},
}
}

eType, ok := kw.expr.(*hclsyntax.ScopeTraversalExpr)
if !ok {
return []lang.Candidate{}
}

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

prefixLen := pos.Byte - eType.Traversal.SourceRange().Start.Byte
prefix := eType.Traversal.RootName()[0:prefixLen]

if strings.HasPrefix(kw.cons.Keyword, prefix) {
return []lang.Candidate{
{
Label: kw.cons.Keyword,
Detail: kw.cons.FriendlyName(),
Description: kw.cons.Description,
Kind: lang.KeywordCandidateKind,
TextEdit: lang.TextEdit{
NewText: kw.cons.Keyword,
Snippet: kw.cons.Keyword,
Range: eType.Range(),
},
},
}
}

return []lang.Candidate{}
}
138 changes: 138 additions & 0 deletions decoder/expr_keyword_completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
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 TestCompletionAtPos_exprKeyword(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
pos hcl.Pos
expectedCandidates lang.Candidates
}{
{
"no expression",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{Keyword: "foobar"},
},
},
`attr = `,
hcl.Pos{Line: 1, Column: 8, Byte: 7},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "foobar",
Detail: "keyword",
Kind: lang.KeywordCandidateKind,
TextEdit: lang.TextEdit{
NewText: "foobar",
Snippet: "foobar",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 8, Byte: 7},
},
},
},
}),
},
{
"matching prefix",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{Keyword: "foobar"},
},
},
`attr = f`,
hcl.Pos{Line: 1, Column: 9, Byte: 8},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "foobar",
Detail: "keyword",
Kind: lang.KeywordCandidateKind,
TextEdit: lang.TextEdit{
NewText: "foobar",
Snippet: "foobar",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 9, Byte: 8},
},
},
},
}),
},
{
"matching prefix in the middle",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{Keyword: "foobar"},
},
},
`attr = foo`,
hcl.Pos{Line: 1, Column: 9, Byte: 8},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "foobar",
Detail: "keyword",
Kind: lang.KeywordCandidateKind,
TextEdit: lang.TextEdit{
NewText: "foobar",
Snippet: "foobar",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 11, Byte: 10},
},
},
},
}),
},
{
"mismatching prefix",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{Keyword: "foobar"},
},
},
`attr = x`,
hcl.Pos{Line: 1, Column: 9, Byte: 8},
lang.CompleteCandidates([]lang.Candidate{}),
},
}
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()
candidates, err := d.CandidatesAtPos(ctx, "test.tf", tc.pos)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedCandidates, candidates); diff != "" {
t.Fatalf("unexpected candidates: %s", diff)
}
})
}
}
35 changes: 35 additions & 0 deletions decoder/expr_keyword_hover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package decoder

import (
"context"
"fmt"

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

func (kw Keyword) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
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
}
117 changes: 117 additions & 0 deletions decoder/expr_keyword_hover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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 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)
}
})
}
}
Loading

0 comments on commit 053e555

Please sign in to comment.