Skip to content

Commit

Permalink
decoder: Use EmptyCompletionData for Set
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Mar 9, 2023
1 parent 7f4b92a commit ad2396d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
10 changes: 5 additions & 5 deletions decoder/expr_set_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ import (
func (set Set) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
if isEmptyExpression(set.expr) {
label := "[ ]"
triggerSuggest := false

if set.cons.Elem != nil {
label = fmt.Sprintf("[ %s ]", set.cons.Elem.FriendlyName())
triggerSuggest = true
}

d := set.cons.EmptyCompletionData(1, 0)

return []lang.Candidate{
{
Label: label,
Detail: set.cons.FriendlyName(),
Kind: lang.SetCandidateKind,
Description: set.cons.Description,
TextEdit: lang.TextEdit{
NewText: "[ ]",
Snippet: "[ ${0} ]",
NewText: d.NewText,
Snippet: d.Snippet,
Range: hcl.Range{
Filename: set.expr.Range().Filename,
Start: pos,
End: pos,
},
},
TriggerSuggest: triggerSuggest,
TriggerSuggest: d.TriggerSuggest,
},
}
}
Expand Down
17 changes: 9 additions & 8 deletions decoder/expr_set_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)

func TestCompletionAtPos_exprSet(t *testing.T) {
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestCompletionAtPos_exprSet(t *testing.T) {
End: hcl.Pos{Line: 1, Column: 8, Byte: 7},
},
NewText: "[ ]",
Snippet: "[ ${0} ]",
Snippet: "[ ${1} ]",
},
Kind: lang.SetCandidateKind,
},
Expand All @@ -52,8 +53,8 @@ func TestCompletionAtPos_exprSet(t *testing.T) {
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Set{
Elem: schema.Keyword{
Keyword: "keyword",
Elem: schema.LiteralType{
Type: cty.String,
},
},
},
Expand All @@ -63,19 +64,19 @@ func TestCompletionAtPos_exprSet(t *testing.T) {
hcl.Pos{Line: 1, Column: 8, Byte: 7},
lang.CompleteCandidates([]lang.Candidate{
{
Label: `[ keyword ]`,
Detail: "set of keyword",
Label: `[ string ]`,
Detail: "set of string",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 8, Byte: 7},
},
NewText: "[ keyword ]",
Snippet: "[ ${0:keyword} ]",
NewText: "[ \"value\" ]",
Snippet: "[ \"${1:value}\" ]",
},
Kind: lang.SetCandidateKind,
TriggerSuggest: true,
TriggerSuggest: false,
},
}),
},
Expand Down
4 changes: 2 additions & 2 deletions schema/constraint_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s Set) Copy() Constraint {
func (s Set) EmptyCompletionData(nextPlaceholder int, nestingLevel int) CompletionData {
if s.Elem == nil {
return CompletionData{
NewText: "[]",
NewText: "[ ]",
Snippet: fmt.Sprintf("[ ${%d} ]", nextPlaceholder),
NextPlaceholder: nextPlaceholder + 1,
}
Expand All @@ -60,7 +60,7 @@ func (s Set) EmptyCompletionData(nextPlaceholder int, nestingLevel int) Completi
elemData := s.Elem.EmptyCompletionData(nextPlaceholder, nestingLevel)
if elemData.NewText == "" || elemData.Snippet == "" {
return CompletionData{
NewText: "[]",
NewText: "[ ]",
Snippet: fmt.Sprintf("[ ${%d} ]", nextPlaceholder),
TriggerSuggest: elemData.TriggerSuggest,
NextPlaceholder: nextPlaceholder + 1,
Expand Down
2 changes: 1 addition & 1 deletion schema/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ STRING
},
},
CompletionData{
NewText: "[]",
NewText: "[ ]",
Snippet: "[ ${1} ]",
TriggerSuggest: true,
NextPlaceholder: 2,
Expand Down

0 comments on commit ad2396d

Please sign in to comment.