Skip to content

Commit

Permalink
Merge pull request #410 from hashicorp/fix-ref-completions-after-dot-…
Browse files Browse the repository at this point in the history
…in-objects

(TF-19544) fix: Support reference completions with a trailing dot in object constructors
  • Loading branch information
ansgarm authored Aug 26, 2024
2 parents c7b8b92 + 9cbeb16 commit daf20a7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion decoder/expr_literal_value_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func TestCompletionAtPos_exprLiteralValue(t *testing.T) {
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 10, Byte: 9},
End: hcl.Pos{Line: 1, Column: 11, Byte: 10},
},
},
},
Expand Down
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.0
require (
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/hcl/v2 v2.21.0
github.com/hashicorp/hcl/v2 v2.22.0
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/zclconf/go-cty v1.15.0
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/hcl/v2 v2.21.0 h1:lve4q/o/2rqwYOgUg3y3V2YPyD1/zkCLGjIV74Jit14=
github.com/hashicorp/hcl/v2 v2.21.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/hashicorp/hcl/v2 v2.22.0 h1:hkZ3nCtqeJsDhPRFz5EA9iwcG1hNWGePOTw6oyul12M=
github.com/hashicorp/hcl/v2 v2.22.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5 h1:shw+DWUaHIyW64Tv30ASCbC6QO6fLy+M5SJb5pJVEI4=
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5/go.mod h1:nHPoxaBUc5CDAMIv0MNmn5PBjWbTs9BI/eh30/n0U6g=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
Expand Down

0 comments on commit daf20a7

Please sign in to comment.