Skip to content

Commit

Permalink
moar
Browse files Browse the repository at this point in the history
  • Loading branch information
jpogran committed Mar 22, 2023
1 parent 573ef35 commit 99eca7e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
31 changes: 30 additions & 1 deletion decoder/expr_literal_value_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)

Expand Down Expand Up @@ -38,7 +39,7 @@ func (lv LiteralValue) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.

// TODO: partial completion for bool
if typ == cty.Bool {
return typ.completeBoolAtPos(ctx, pos)
return lv.completeBoolAtPos(ctx, pos)
}

// TODO: partial completion for string
Expand All @@ -63,3 +64,31 @@ func (lv LiteralValue) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.

return nil
}

func (lt LiteralValue) completeBoolAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
switch eType := lt.expr.(type) {

// case *hclsyntax.TupleConsExpr:
// prefixLen := pos.Byte - eType.Range().Start.Byte
// prefix := eType.Traversal.RootName()[0:prefixLen]
// return boolLiteralCandidates(prefix, eType.Range())

case *hclsyntax.ScopeTraversalExpr:
prefixLen := pos.Byte - eType.Range().Start.Byte
prefix := eType.Traversal.RootName()[0:prefixLen]
return boolLiteralCandidates(prefix, eType.Range())

case *hclsyntax.LiteralValueExpr:
if eType.Val.Type() == cty.Bool {
value := "false"
if eType.Val.True() {
value = "true"
}
prefixLen := pos.Byte - eType.Range().Start.Byte
prefix := value[0:prefixLen]
return boolLiteralCandidates(prefix, eType.Range())
}
}

return []lang.Candidate{}
}
93 changes: 93 additions & 0 deletions decoder/expr_literal_value_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,99 @@ func TestCompletionAtPos_exprLiteralValue(t *testing.T) {
},
}),
},
{
"list",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.LiteralValue{
Value: cty.ListVal([]cty.Value{
cty.BoolVal(true),
}),
},
},
},
`attr =
`,
hcl.Pos{Line: 1, Column: 8, Byte: 7},
lang.CompleteCandidates([]lang.Candidate{
{
Label: `[ true ]`,
Detail: "list of bool",
Kind: lang.ListCandidateKind,
TextEdit: lang.TextEdit{
NewText: `[true]`,
Snippet: `[true]`,
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 8, Byte: 7},
},
},
},
}),
},
{
"set",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.LiteralValue{
Value: cty.SetVal([]cty.Value{
cty.BoolVal(false),
}),
},
},
},
`attr =
`,
hcl.Pos{Line: 1, Column: 8, Byte: 7},
lang.CompleteCandidates([]lang.Candidate{
{
Label: `[ false ]`,
Detail: "set of bool",
Kind: lang.SetCandidateKind,
TextEdit: lang.TextEdit{
NewText: `[false]`,
Snippet: `[false]`,
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 8, Byte: 7},
},
},
},
}),
},
{
"tuple",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.LiteralValue{
Value: cty.TupleVal([]cty.Value{
cty.BoolVal(true),
}),
},
},
},
`attr =
`,
hcl.Pos{Line: 1, Column: 8, Byte: 7},
lang.CompleteCandidates([]lang.Candidate{
{
Label: `[ true ]`,
Detail: "tuple",
Kind: lang.TupleCandidateKind,
TextEdit: lang.TextEdit{
NewText: `[true]`,
Snippet: `[true]`,
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 8, Byte: 7},
},
},
},
}),
},
}

for i, tc := range testCases {
Expand Down

0 comments on commit 99eca7e

Please sign in to comment.