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: Add support for conditional expression #326

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion decoder/expr_any_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lan

// 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
// TODO: Support complex index expressions https://github.com/hashicorp/terraform-ls/issues/531
// TODO: Support relative traversals https://github.com/hashicorp/terraform-ls/issues/532

Expand All @@ -116,6 +115,12 @@ func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lan
}
candidates = append(candidates, templateCandidates...)

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

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

return candidates, true
}

func (a Any) completeConditionalExprAtPos(ctx context.Context, pos hcl.Pos) ([]lang.Candidate, bool) {
radeksimko marked this conversation as resolved.
Show resolved Hide resolved
candidates := make([]lang.Candidate, 0)

switch eType := a.expr.(type) {
case *hclsyntax.ConditionalExpr:
if eType.Condition.Range().ContainsPos(pos) || eType.Condition.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: cty.Bool,
}
return newExpression(a.pathCtx, eType.Condition, cons).CompletionAtPos(ctx, pos), true
}
if eType.TrueResult.Range().ContainsPos(pos) || eType.TrueResult.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: cty.DynamicPseudoType,
}
return newExpression(a.pathCtx, eType.TrueResult, cons).CompletionAtPos(ctx, pos), true
}
if eType.FalseResult.Range().ContainsPos(pos) || eType.FalseResult.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: cty.DynamicPseudoType,
}
return newExpression(a.pathCtx, eType.FalseResult, cons).CompletionAtPos(ctx, pos), true
}

return candidates, false
}

return candidates, true
}
Loading