-
Notifications
You must be signed in to change notification settings - Fork 26
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 LiteralValue
as Constraint
#187
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
type LiteralValue struct { | ||
expr hcl.Expression | ||
cons schema.LiteralValue | ||
expr hcl.Expression | ||
cons schema.LiteralValue | ||
pathCtx *PathContext | ||
} | ||
|
||
func (lv LiteralValue) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate { | ||
// TODO | ||
return nil | ||
} | ||
func formatNumberVal(val cty.Value) string { | ||
bf := val.AsBigFloat() | ||
|
||
func (lv LiteralValue) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData { | ||
// TODO | ||
return nil | ||
} | ||
if bf.IsInt() { | ||
intNum, _ := bf.Int64() | ||
return fmt.Sprintf("%d", intNum) | ||
} | ||
|
||
fNum, _ := bf.Float64() | ||
return strconv.FormatFloat(fNum, 'f', -1, 64) | ||
|
||
func (lv LiteralValue) SemanticTokens(ctx context.Context) []lang.SemanticToken { | ||
// TODO | ||
return nil | ||
} |
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,98 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func (lv LiteralValue) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate { | ||
typ := lv.cons.Value.Type() | ||
|
||
if isEmptyExpression(lv.expr) { | ||
editRange := hcl.Range{ | ||
Filename: lv.expr.Range().Filename, | ||
Start: pos, | ||
End: pos, | ||
} | ||
|
||
// We expect values to be always fully populated | ||
ctx = schema.WithPrefillRequiredFields(ctx, true) | ||
|
||
cd := lv.cons.EmptyCompletionData(ctx, 1, 0) | ||
|
||
return []lang.Candidate{ | ||
{ | ||
Label: labelForLiteralValue(lv.cons.Value, false), | ||
Detail: typ.FriendlyName(), | ||
Kind: candidateKindForType(typ), | ||
TextEdit: lang.TextEdit{ | ||
Range: editRange, | ||
NewText: cd.NewText, | ||
Snippet: cd.Snippet, | ||
}, | ||
TriggerSuggest: cd.TriggerSuggest, | ||
}, | ||
} | ||
} | ||
|
||
if typ == cty.Bool { | ||
return lv.completeBoolAtPos(ctx, pos) | ||
} | ||
|
||
editRange := lv.expr.Range() | ||
if editRange.End.Line != pos.Line { | ||
// account for quotes or brackets that are not closed | ||
editRange.End = pos | ||
} | ||
|
||
if !editRange.ContainsPos(pos) { | ||
// account for trailing character(s) which doesn't appear in AST | ||
// such as dot, opening bracket etc. | ||
editRange.End = pos | ||
} | ||
|
||
cd := lv.cons.EmptyCompletionData(ctx, 1, 0) | ||
return []lang.Candidate{ | ||
{ | ||
Label: labelForLiteralValue(lv.cons.Value, false), | ||
Detail: typ.FriendlyName(), | ||
Kind: candidateKindForType(typ), | ||
TextEdit: lang.TextEdit{ | ||
Range: editRange, | ||
NewText: cd.NewText, | ||
Snippet: cd.Snippet, | ||
}, | ||
TriggerSuggest: cd.TriggerSuggest, | ||
}, | ||
} | ||
|
||
// Avoid partial completion inside complex types for now | ||
} | ||
|
||
func (lt LiteralValue) completeBoolAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate { | ||
switch eType := lt.expr.(type) { | ||
|
||
case *hclsyntax.ScopeTraversalExpr: | ||
prefixLen := pos.Byte - eType.Range().Start.Byte | ||
prefix := eType.Traversal.RootName()[0:prefixLen] | ||
return boolLiteralCandidates(prefix, eType.Range()) | ||
|
||
case *hclsyntax.LiteralValueExpr: | ||
if eType.Val.Type() == cty.Bool { | ||
value := "false" | ||
if eType.Val.True() { | ||
value = "true" | ||
} | ||
prefixLen := pos.Byte - eType.Range().Start.Byte | ||
prefix := value[0:prefixLen] | ||
return boolLiteralCandidates(prefix, eType.Range()) | ||
} | ||
} | ||
|
||
return []lang.Candidate{} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: This would not account for completion inside a (valid) multi-line expression, which is one of the reasons we use slightly more sophisticated solution for such an edge case, e.g. in Map
hcl-lang/decoder/expr_map_completion.go
Lines 143 to 148 in d469d16
Given that we only get to deal with primitive types here (which are mostly single-line), it should be fine to ignore. Multiline strings do exist (HEREDOC syntax) but we don't really use them in Terraform as constraint anywhere.
Implementing a perfect solution would be unnecessarily costly as we'd have to be looking for different terminating character or even a sequence of characters, depending on what type we can "guess".
TL;DR it's fine as is.
Side note - I am surprised that config such as this
is parsed by HCL at all and returns expression with range that we can take advantage of here, because a lot of other invalid configuration variations end up missing from the AST entirely (including the attribute), which is also what motivated that recovery logic linked above. 😅