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: Support template expressions #322

Merged
merged 10 commits into from
Nov 22, 2023
62 changes: 61 additions & 1 deletion decoder/expr_any_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (a Any) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate
func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
candidates := make([]lang.Candidate, 0)

// TODO: Support TemplateExpr https://github.com/hashicorp/terraform-ls/issues/522
// TODO: Support splat expression https://github.com/hashicorp/terraform-ls/issues/526
// TODO: Support for-in-if expression https://github.com/hashicorp/terraform-ls/issues/527
// TODO: Support conditional expression https://github.com/hashicorp/terraform-ls/issues/528
Expand All @@ -111,6 +110,12 @@ func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lan
}
candidates = append(candidates, opCandidates...)

templateCandidates, ok := a.completeTemplateExprAtPos(ctx, pos)
if !ok {
return candidates
}
candidates = append(candidates, templateCandidates...)

ref := Reference{
expr: a.expr,
cons: schema.Reference{OfType: a.cons.OfType},
Expand Down Expand Up @@ -214,3 +219,58 @@ func (a Any) completeOperatorExprAtPos(ctx context.Context, pos hcl.Pos) ([]lang

return candidates, true
}

func (a Any) completeTemplateExprAtPos(ctx context.Context, pos hcl.Pos) ([]lang.Candidate, bool) {
candidates := make([]lang.Candidate, 0)

switch eType := a.expr.(type) {
case *hclsyntax.TemplateExpr:
if eType.IsStringLiteral() {
return candidates, false
}

for _, partExpr := range eType.Parts {
// We overshot the position and stop
if partExpr.Range().Start.Byte > pos.Byte {
break
}

// We're not checking the end byte position here, because we don't
// allow completion after the }
if partExpr.Range().ContainsPos(pos) || partExpr.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: cty.String,
}
return newExpression(a.pathCtx, partExpr, cons).CompletionAtPos(ctx, pos), true
}

// Trailing dot may be ignored by the parser so we attempt to recover it
if pos.Byte-partExpr.Range().End.Byte == 1 {
fileBytes := a.pathCtx.Files[partExpr.Range().Filename].Bytes
trailingRune := fileBytes[partExpr.Range().End.Byte:pos.Byte][0]

if trailingRune == '.' {
cons := schema.AnyExpression{
OfType: cty.String,
}
return newExpression(a.pathCtx, partExpr, cons).CompletionAtPos(ctx, pos), true
}
}
}

return candidates, false
case *hclsyntax.TemplateWrapExpr:
if eType.Wrapped.Range().ContainsPos(pos) {
cons := schema.AnyExpression{
OfType: cty.String,
}
return newExpression(a.pathCtx, eType.Wrapped, cons).CompletionAtPos(ctx, pos), true
}

return candidates, false
case *hclsyntax.TemplateJoinExpr:
// TODO: implement when support for expressions https://github.com/hashicorp/terraform-ls/issues/527 lands
}

return candidates, true
}
Loading