diff options
| author | Ansgar Mertens <ansgar@hashicorp.com> | 2024-08-22 10:14:20 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-22 10:14:20 +0200 |
| commit | ae53b93a67f2cd4f2bedaaae6a64db6a1db8c6a1 (patch) | |
| tree | f8a334690cb164297ce4e759531f14b44aa461e9 /hclsyntax/parser.go | |
| parent | 360ae579460fab69e9939599af85c8f255a59007 (diff) | |
| parent | 117baa8bf53bc2050fe5e0f2ab85c4ef9341d20e (diff) | |
Merge pull request #692 from hashicorp/support-incomplete-references-in-object-items
feat: return an `ExprSyntaxError` for invalid references that end in a dot
Diffstat (limited to 'hclsyntax/parser.go')
| -rw-r--r-- | hclsyntax/parser.go | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/hclsyntax/parser.go b/hclsyntax/parser.go index ce96ae3..fec7861 100644 --- a/hclsyntax/parser.go +++ b/hclsyntax/parser.go @@ -811,9 +811,16 @@ Traversal: // will probably be misparsed until we hit something that // allows us to re-sync. // - // We will probably need to do something better here eventually - // in order to support autocomplete triggered by typing a - // period. + // Returning an ExprSyntaxError allows us to pass more information + // about the invalid expression to the caller, which can then + // use this for example for completions that happen after typing + // a dot in an editor. + ret = &ExprSyntaxError{ + Placeholder: cty.DynamicVal, + ParseDiags: diags, + SrcRange: hcl.RangeBetween(from.Range(), dot.Range), + } + p.setRecovery() } @@ -1516,6 +1523,16 @@ func (p *parser) parseObjectCons() (Expression, hcl.Diagnostics) { diags = append(diags, valueDiags...) if p.recovery && valueDiags.HasErrors() { + // If the value is an ExprSyntaxError, we can add an item with it, even though we will recover afterwards + // This allows downstream consumers to still retrieve this first invalid item, even though following items + // won't be parsed. This is useful for supplying completions. + if exprSyntaxError, ok := value.(*ExprSyntaxError); ok { + items = append(items, ObjectConsItem{ + KeyExpr: key, + ValueExpr: exprSyntaxError, + }) + } + // If expression parsing failed then we are probably in a strange // place in the token stream, so we'll bail out and try to reset // to after our closing brace to allow parsing to continue. |
