Skip to content

Commit

Permalink
Improve quoting and empty/multiline expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Jul 27, 2022
1 parent 593035f commit 3e0c438
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions decoder/expression_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ func isEmptyExpr(expr hclsyntax.Expression) bool {
return true
}

func isMultilineTemplateExpr(expr hclsyntax.Expression) bool {
t, ok := expr.(*hclsyntax.TemplateExpr)
if !ok {
return false
}
// TODO fix/test
return isMultilineStringLiteral(t)
}

func (d *PathDecoder) candidatesFromHooks(ctx context.Context, attr *hclsyntax.Attribute, schema *schema.AttributeSchema, outerBodyRng hcl.Range, pos hcl.Pos) []lang.Candidate {
candidates := make([]lang.Candidate, 0)
expr := ExprConstraints(schema.Expr)
Expand All @@ -209,7 +218,11 @@ func (d *PathDecoder) candidatesFromHooks(ctx context.Context, attr *hclsyntax.A
}

editRng := attr.Expr.Range()
if isEmptyExpr(attr.Expr) { // TODO improve quoting and range
if isEmptyExpr(attr.Expr) || isMultilineTemplateExpr(attr.Expr) {
// An empty expression or a string without a closing quote will lead to
// an attribute expression spanning multiple lines.
// Since text edits only support a single line, we're resetting the End
// position here.
editRng.End = pos
}
prefixRng := attr.Expr.Range()
Expand All @@ -227,14 +240,19 @@ func (d *PathDecoder) candidatesFromHooks(ctx context.Context, attr *hclsyntax.A
res, _ := completionFunc(ctx, cty.StringVal(prefix))

for _, c := range res {
// We're adding quotes to the string here, as we're always
// replacing the whole edit range
text := fmt.Sprintf("%q", c.RawInsertText)

candidates = append(candidates, lang.Candidate{
Label: c.Label,
Detail: c.Detail,
Description: c.Description,
Kind: c.Kind,
Label: text,
Detail: c.Detail,
Description: c.Description,
Kind: c.Kind,
IsDeprecated: c.IsDeprecated,
TextEdit: lang.TextEdit{
NewText: fmt.Sprintf("%q", c.RawInsertText),
Snippet: fmt.Sprintf("%q", c.RawInsertText),
NewText: text,
Snippet: text,
Range: editRng,
},
ResolveHook: c.ResolveHook,
Expand Down

0 comments on commit 3e0c438

Please sign in to comment.