diff --git a/decoder/expression.go b/decoder/expression.go index 7ef7cd1a..bd233f1b 100644 --- a/decoder/expression.go +++ b/decoder/expression.go @@ -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 @@ -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 +} diff --git a/decoder/expression_candidates.go b/decoder/expression_candidates.go index 7593928a..320a54b6 100644 --- a/decoder/expression_candidates.go +++ b/decoder/expression_candidates.go @@ -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 { @@ -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