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: Extract & document isEmptyExpression() #188

Merged
merged 1 commit into from
Jan 17, 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
19 changes: 19 additions & 0 deletions decoder/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)

// Expression represents an expression capable of providing
Expand Down Expand Up @@ -49,3 +51,20 @@ func (d *PathDecoder) newExpression(expr hcl.Expression, cons schema.Constraint)

return unknownExpression{}
}

// isEmptyExpression returns true if given expression is suspected
// to be empty, e.g. newline after equal sign.
//
// Because upstream HCL parser doesn't always handle incomplete
// configuration gracefully, this may not cover all cases.
func isEmptyExpression(expr hcl.Expression) bool {
l, ok := expr.(*hclsyntax.LiteralValueExpr)
if !ok {
return false
}
if l.Val != cty.DynamicVal {
return false
}

return true
}
15 changes: 1 addition & 14 deletions decoder/expression_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,6 @@ func MaxCandidatesFromContext(ctx context.Context) (uint, bool) {
return mc, ok
}

func isEmptyExpr(expr hclsyntax.Expression) bool {
l, ok := expr.(*hclsyntax.LiteralValueExpr)
if !ok {
return false
}
if l.Val != cty.DynamicVal {
return false
}
// TODO? more checks

return true
}

func isMultilineTemplateExpr(expr hclsyntax.Expression) bool {
t, ok := expr.(*hclsyntax.TemplateExpr)
if !ok {
Expand All @@ -268,7 +255,7 @@ func (d *PathDecoder) candidatesFromHooks(ctx context.Context, attr *hclsyntax.A
}

editRng := attr.Expr.Range()
if isEmptyExpr(attr.Expr) || isMultilineTemplateExpr(attr.Expr) {
if isEmptyExpression(attr.Expr) || isMultilineTemplateExpr(attr.Expr) {
// An empty expression or a string without a closing quote will lead to
// an attribute expression spanning multiple lines.
// Since text edits only support a single line, we're resetting the End
Expand Down