-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decoder: Implement completion for Set
- Loading branch information
1 parent
53a54c3
commit 6075158
Showing
2 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func (set Set) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate { | ||
if isEmptyExpression(set.expr) { | ||
label := "[ ]" | ||
triggerSuggest := false | ||
|
||
if set.cons.Elem != nil { | ||
label = fmt.Sprintf("[ %s ]", set.cons.Elem.FriendlyName()) | ||
triggerSuggest = true | ||
} | ||
|
||
return []lang.Candidate{ | ||
{ | ||
Label: label, | ||
Detail: set.cons.FriendlyName(), | ||
Kind: lang.SetCandidateKind, | ||
Description: set.cons.Description, | ||
TextEdit: lang.TextEdit{ | ||
NewText: "[ ]", | ||
Snippet: "[ ${0} ]", | ||
Range: hcl.Range{ | ||
Filename: set.expr.Range().Filename, | ||
Start: pos, | ||
End: pos, | ||
}, | ||
}, | ||
TriggerSuggest: triggerSuggest, | ||
}, | ||
} | ||
} | ||
|
||
eType, ok := set.expr.(*hclsyntax.TupleConsExpr) | ||
if !ok { | ||
return []lang.Candidate{} | ||
} | ||
|
||
if set.cons.Elem == nil { | ||
return []lang.Candidate{} | ||
} | ||
|
||
fileBytes := set.pathCtx.Files[set.expr.Range().Filename].Bytes | ||
|
||
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(set.pathCtx, expr, set.cons.Elem).CompletionAtPos(ctx, pos) | ||
} | ||
|
||
// TODO: depending on set.cons.Elem (Keyword, LiteralValue, Reference), | ||
// filter out declared elements to provide uniqueness as that is the nature of set | ||
|
||
var lastElemEndPos hcl.Pos | ||
for _, elemExpr := range eType.Exprs { | ||
if elemExpr.Range().ContainsPos(pos) || elemExpr.Range().End.Byte == pos.Byte { | ||
return newExpression(set.pathCtx, elemExpr, set.cons.Elem).CompletionAtPos(ctx, pos) | ||
} | ||
lastElemEndPos = elemExpr.Range().End | ||
} | ||
|
||
rng := hcl.Range{ | ||
Filename: eType.Range().Filename, | ||
Start: lastElemEndPos, | ||
End: pos, | ||
} | ||
|
||
// TODO: test with multi-line element expressions | ||
|
||
b := rng.SliceBytes(fileBytes) | ||
if strings.TrimSpace(string(b)) != "," { | ||
return []lang.Candidate{} | ||
} | ||
|
||
expr := newEmptyExpressionAtPos(eType.Range().Filename, pos) | ||
return newExpression(set.pathCtx, expr, set.cons.Elem).CompletionAtPos(ctx, pos) | ||
} | ||
|
||
return []lang.Candidate{} | ||
} |