Skip to content

Commit

Permalink
decoder: Extract & document isEmptyExpression (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jan 17, 2023
1 parent f9adfd8 commit d85c2db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
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 @@ -97,3 +99,20 @@ func newExpression(pathContext *PathContext, expr hcl.Expression, cons schema.Co

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

0 comments on commit d85c2db

Please sign in to comment.