hcl-lang
hcl-lang copied to clipboard
Provide completion near unterminated binary operator
Context
While implementing support for operators in https://github.com/hashicorp/hcl-lang/pull/320 a few edge cases concerning completion near an unterminated binary operator were discovered.
Examples:
attr = 42 +attr = 42 + var.attr = 42 -attr = 42 - var.
In these cases we do not provide relevant completion because the upstream HCL parser does not report any of the above as BinaryOpExpr and nor does our recovery logic account for these edge cases
https://github.com/hashicorp/hcl-lang/blob/436a15d944d87a2a4abeaec8e2d7b399f54eab6f/decoder/candidates.go#L175-L187
I tried updating that recovery logic in some naive way but that broke other tests, so clearly there's more changes that need to be made to accommodate this edge case which cannot currently be isolated to just decoder.Any.CompletionAtPos(...).
Here is what I tried:
// edge case: end of incomplete expression with characters which parser ignores
gapRange := hcl.Range{
Filename: attr.Range().Filename,
Start: attr.Expr.Range().End,
End: attr.Range().End,
}
if gapRange.Start.Byte < gapRange.End.Byte && gapRange.ContainsPos(pos) {
return true
}
Proposal
Revisit recovery logic to ensure that it can recover from the above snippets and equivalent (with other binary operators) and then deal with the issue inside decoder.Any.CompletionAtPos(...).