Skip to content

Commit daf20a7

Browse files
authored
Merge pull request #410 from hashicorp/fix-ref-completions-after-dot-in-objects
(TF-19544) fix: Support reference completions with a trailing dot in object constructors
2 parents c7b8b92 + 9cbeb16 commit daf20a7

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

decoder/expr_literal_value_completion_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func TestCompletionAtPos_exprLiteralValue(t *testing.T) {
415415
Range: hcl.Range{
416416
Filename: "test.tf",
417417
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
418-
End: hcl.Pos{Line: 1, Column: 10, Byte: 9},
418+
End: hcl.Pos{Line: 1, Column: 11, Byte: 10},
419419
},
420420
},
421421
},

decoder/expr_reference_completion.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,36 @@ func (ref Reference) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Ca
6363
return candidates
6464
}
6565

66-
eType, ok := ref.expr.(*hclsyntax.ScopeTraversalExpr)
67-
if !ok {
66+
var editRng, prefixRng hcl.Range
67+
switch eType := ref.expr.(type) {
68+
case *hclsyntax.ScopeTraversalExpr:
69+
editRng = eType.Range()
70+
if !editRng.ContainsPos(pos) {
71+
// account for trailing character(s) which doesn't appear in AST
72+
// such as dot, opening bracket etc.
73+
editRng.End = pos
74+
}
75+
prefixRng = hcl.Range{
76+
Filename: eType.Range().Filename,
77+
Start: eType.Range().Start,
78+
End: pos,
79+
}
80+
case *hclsyntax.ExprSyntaxError:
81+
editRng = eType.Range()
82+
if !editRng.ContainsPos(pos) {
83+
// account for trailing character(s) which doesn't appear in AST
84+
// such as dot, opening bracket etc.
85+
editRng.End = pos
86+
}
87+
prefixRng = hcl.Range{
88+
Filename: eType.Range().Filename,
89+
Start: eType.Range().Start,
90+
End: pos,
91+
}
92+
default:
6893
return []lang.Candidate{}
6994
}
7095

71-
editRng := eType.Range()
72-
if !editRng.ContainsPos(pos) {
73-
// account for trailing character(s) which doesn't appear in AST
74-
// such as dot, opening bracket etc.
75-
editRng.End = pos
76-
}
77-
prefixRng := hcl.Range{
78-
Filename: eType.Range().Filename,
79-
Start: eType.Range().Start,
80-
End: pos,
81-
}
8296
prefix := string(prefixRng.SliceBytes(file.Bytes))
8397

8498
candidates := make([]lang.Candidate, 0)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.21.0
55
require (
66
github.com/google/go-cmp v0.6.0
77
github.com/hashicorp/go-multierror v1.1.1
8-
github.com/hashicorp/hcl/v2 v2.21.0
8+
github.com/hashicorp/hcl/v2 v2.22.0
99
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
1010
github.com/zclconf/go-cty v1.15.0
1111
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
1414
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
1515
github.com/hashicorp/hcl/v2 v2.21.0 h1:lve4q/o/2rqwYOgUg3y3V2YPyD1/zkCLGjIV74Jit14=
1616
github.com/hashicorp/hcl/v2 v2.21.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
17+
github.com/hashicorp/hcl/v2 v2.22.0 h1:hkZ3nCtqeJsDhPRFz5EA9iwcG1hNWGePOTw6oyul12M=
18+
github.com/hashicorp/hcl/v2 v2.22.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
1719
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5 h1:shw+DWUaHIyW64Tv30ASCbC6QO6fLy+M5SJb5pJVEI4=
1820
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5/go.mod h1:nHPoxaBUc5CDAMIv0MNmn5PBjWbTs9BI/eh30/n0U6g=
1921
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=

0 commit comments

Comments
 (0)