From 6ce4bb93e0b4b9e09da656d4f4d96c971174c4f3 Mon Sep 17 00:00:00 2001 From: Ansgar Mertens Date: Wed, 21 Aug 2024 16:02:09 +0200 Subject: [PATCH] fix: Support reference completions after dot in object constructors Previously it was required to type a first character as the hclsyntax parser wouldn't return anything for these invalid references. This requires https://github.com/hashicorp/hcl/pull/692 to work --- decoder/expr_reference_completion.go | 40 +++++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/decoder/expr_reference_completion.go b/decoder/expr_reference_completion.go index 83282eb9..132ff046 100644 --- a/decoder/expr_reference_completion.go +++ b/decoder/expr_reference_completion.go @@ -63,22 +63,36 @@ func (ref Reference) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Ca return candidates } - eType, ok := ref.expr.(*hclsyntax.ScopeTraversalExpr) - if !ok { + var editRng, prefixRng hcl.Range + switch eType := ref.expr.(type) { + case *hclsyntax.ScopeTraversalExpr: + editRng = eType.Range() + if !editRng.ContainsPos(pos) { + // account for trailing character(s) which doesn't appear in AST + // such as dot, opening bracket etc. + editRng.End = pos + } + prefixRng = hcl.Range{ + Filename: eType.Range().Filename, + Start: eType.Range().Start, + End: pos, + } + case *hclsyntax.ExprSyntaxError: + editRng = eType.Range() + if !editRng.ContainsPos(pos) { + // account for trailing character(s) which doesn't appear in AST + // such as dot, opening bracket etc. + editRng.End = pos + } + prefixRng = hcl.Range{ + Filename: eType.Range().Filename, + Start: eType.Range().Start, + End: pos, + } + default: return []lang.Candidate{} } - editRng := eType.Range() - if !editRng.ContainsPos(pos) { - // account for trailing character(s) which doesn't appear in AST - // such as dot, opening bracket etc. - editRng.End = pos - } - prefixRng := hcl.Range{ - Filename: eType.Range().Filename, - Start: eType.Range().Start, - End: pos, - } prefix := string(prefixRng.SliceBytes(file.Bytes)) candidates := make([]lang.Candidate, 0)