From 99eca7e9f7652bca20dd58ec180206dce128d293 Mon Sep 17 00:00:00 2001 From: James Pogran Date: Wed, 22 Mar 2023 12:37:40 -0400 Subject: [PATCH] moar --- decoder/expr_literal_value_completion.go | 31 ++++++- decoder/expr_literal_value_completion_test.go | 93 +++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/decoder/expr_literal_value_completion.go b/decoder/expr_literal_value_completion.go index d3cb40a8..6552b853 100644 --- a/decoder/expr_literal_value_completion.go +++ b/decoder/expr_literal_value_completion.go @@ -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" ) @@ -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 @@ -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{} +} diff --git a/decoder/expr_literal_value_completion_test.go b/decoder/expr_literal_value_completion_test.go index 611eca5c..72f9848b 100644 --- a/decoder/expr_literal_value_completion_test.go +++ b/decoder/expr_literal_value_completion_test.go @@ -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 {