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 binary & unary operators #320

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions decoder/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ func (d *PathDecoder) isPosInsideAttrExpr(attr *hclsyntax.Attribute, pos hcl.Pos
return true
}

// edge case: end of incomplete traversal with '.' (which parser ignores)
// edge case: end of incomplete expression with trailing '.' (which parser ignores)
endByte := attr.Expr.Range().End.Byte
if _, ok := attr.Expr.(*hclsyntax.ScopeTraversalExpr); ok && pos.Byte-endByte == 1 {
if pos.Byte-endByte == 1 {
suspectedDotRng := hcl.Range{
Filename: attr.Expr.Range().Filename,
Start: attr.Expr.Range().End,
Expand Down
86 changes: 85 additions & 1 deletion decoder/expr_any_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
)

func (a Any) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
Expand Down Expand Up @@ -100,10 +102,15 @@ 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 operator expresssions https://github.com/hashicorp/terraform-ls/issues/529
// 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

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

ref := Reference{
expr: a.expr,
cons: schema.Reference{OfType: a.cons.OfType},
Expand All @@ -130,3 +137,80 @@ func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lan

return candidates
}

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

switch eType := a.expr.(type) {
case *hclsyntax.BinaryOpExpr:
opReturnType := eType.Op.Type

// Check if such an operation is even allowed within the constraint
if _, err := convert.Convert(cty.UnknownVal(opReturnType), a.cons.OfType); err != nil {
// This could illustrate a situation such as `list_attr = 42 +`
// which is invalid syntax as add (+) op will never produce a list
return candidates, false
}

opFuncParams := eType.Op.Impl.Params()
if len(opFuncParams) != 2 {
// This should never happen if HCL implementation is correct
return candidates, false
}

if eType.LHS.Range().ContainsPos(pos) {
cons := schema.AnyExpression{
OfType: opFuncParams[0].Type,
}
return newExpression(a.pathCtx, eType.LHS, cons).CompletionAtPos(ctx, pos), true
}
if eType.RHS.Range().ContainsPos(pos) || eType.RHS.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: opFuncParams[1].Type,
}
return newExpression(a.pathCtx, eType.RHS, cons).CompletionAtPos(ctx, pos), true
}

return candidates, false

case *hclsyntax.UnaryOpExpr:
opReturnType := eType.Op.Type

// Check if such an operation is even allowed within the constraint
if _, err := convert.Convert(cty.UnknownVal(opReturnType), a.cons.OfType); err != nil {
// This could illustrate a situation such as `list_attr = !`
// which is invalid syntax as negation (!) op will never produce a list
return candidates, false
}

opFuncParams := eType.Op.Impl.Params()
if len(opFuncParams) != 1 {
// This should never happen if HCL implementation is correct
return candidates, false
}

if eType.Val.Range().ContainsPos(pos) || eType.Val.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: opFuncParams[0].Type,
}
return newExpression(a.pathCtx, eType.Val, cons).CompletionAtPos(ctx, pos), true
}

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

if trailingRune == '.' {
cons := schema.AnyExpression{
OfType: opFuncParams[0].Type,
}
return newExpression(a.pathCtx, eType.Val, cons).CompletionAtPos(ctx, pos), true
}
}

return candidates, false
}

return candidates, true
}
Loading