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 List as Constraint #195

Merged
merged 10 commits into from
Mar 13, 2023
29 changes: 0 additions & 29 deletions decoder/expr_list.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
)
Expand All @@ -14,28 +10,3 @@ type List struct {
cons schema.List
pathCtx *PathContext
}

func (l List) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
// TODO
return nil
}

func (l List) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
// TODO
return nil
}

func (l List) SemanticTokens(ctx context.Context) []lang.SemanticToken {
// TODO
return nil
}

func (l List) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
// TODO
return nil
}

func (l List) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
// TODO
return nil
}
84 changes: 84 additions & 0 deletions decoder/expr_list_completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package decoder

import (
"context"
"fmt"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func (list List) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
if isEmptyExpression(list.expr) {
label := "[ ]"

if list.cons.Elem != nil {
label = fmt.Sprintf("[ %s ]", list.cons.Elem.FriendlyName())
}

d := list.cons.EmptyCompletionData(1, 0)

return []lang.Candidate{
{
Label: label,
Detail: list.cons.FriendlyName(),
Kind: lang.ListCandidateKind,
Description: list.cons.Description,
TextEdit: lang.TextEdit{
NewText: d.NewText,
Snippet: d.Snippet,
Range: hcl.Range{
Filename: list.expr.Range().Filename,
Start: pos,
End: pos,
},
},
TriggerSuggest: d.TriggerSuggest,
},
}
}

eType, ok := list.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return []lang.Candidate{}
}

if list.cons.Elem == nil {
return []lang.Candidate{}
}

betweenBraces := hcl.Range{
Filename: eType.Range().Filename,
Start: eType.OpenRange.End,
End: eType.Range().End,
}

if betweenBraces.ContainsPos(pos) {
if len(eType.Exprs) == 0 {
expr := newEmptyExpressionAtPos(eType.Range().Filename, pos)
return newExpression(list.pathCtx, expr, list.cons.Elem).CompletionAtPos(ctx, pos)
}

for _, elemExpr := range eType.Exprs {
// We cannot trust ranges of empty expressions, so we imply
// that invalid configuration follows and we stop here
// e.g. for completion between commas [keyword, ,keyword]
if isEmptyExpression(elemExpr) {
break
}
// We overshot the position and stop
if elemExpr.Range().Start.Byte > pos.Byte {
break
}
if elemExpr.Range().ContainsPos(pos) || elemExpr.Range().End.Byte == pos.Byte {
return newExpression(list.pathCtx, elemExpr, list.cons.Elem).CompletionAtPos(ctx, pos)
}
}

expr := newEmptyExpressionAtPos(eType.Range().Filename, pos)
return newExpression(list.pathCtx, expr, list.cons.Elem).CompletionAtPos(ctx, pos)
}

return []lang.Candidate{}
}
Loading