Skip to content

Commit

Permalink
fix: Support reference completions after dot in object constructors
Browse files Browse the repository at this point in the history
Previously it was required to type a first character as the hclsyntax parser wouldn't return anything for these invalid references. This requires hashicorp/hcl#692 to work
  • Loading branch information
ansgarm committed Aug 22, 2024
1 parent bd8c5f1 commit 6ce4bb9
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions decoder/expr_reference_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6ce4bb9

Please # to comment.