Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoder/schema: Add support for Keyword as Constraint #184

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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