Skip to content

Commit 118ac45

Browse files
dbanckradeksimko
andauthored
Enable hooks for dynamic completion (#123)
* Add `CompletionHooks` to `AttributeSchema` * Add comment about `AnyAttribute` * Add optional reslove hook to Candidate * Extend decoder context with completion hooks * Execute hooks as part of attribue expr candidates * Add ResolveCandidate to decoder for resolve handler * Improve quoting and empty/multiline expressions * Pass context in all existing tests * Add tests for attr completion hooks Co-authored-by: Radek Simko <[email protected]>
1 parent 48e582c commit 118ac45

13 files changed

+546
-47
lines changed

decoder/body_candidates_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package decoder
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/google/go-cmp/cmp"
@@ -12,6 +13,7 @@ import (
1213
)
1314

1415
func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
16+
ctx := context.Background()
1517
bodySchema := &schema.BodySchema{
1618
Blocks: map[string]*schema.BlockSchema{
1719
"customblock": {
@@ -43,7 +45,7 @@ func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
4345
})
4446
d.maxCandidates = 1
4547

46-
candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
48+
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.Pos{
4749
Line: 2,
4850
Column: 7,
4951
Byte: 29,
@@ -84,6 +86,7 @@ func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
8486
}
8587

8688
func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
89+
ctx := context.Background()
8790
bodySchema := &schema.BodySchema{
8891
Blocks: map[string]*schema.BlockSchema{
8992
"customblock": {
@@ -113,7 +116,7 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
113116
},
114117
})
115118

116-
candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
119+
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.Pos{
117120
Line: 2,
118121
Column: 7,
119122
Byte: 29,
@@ -154,6 +157,7 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
154157
}
155158

156159
func TestDecoder_CandidateAtPos_incompleteBlocks(t *testing.T) {
160+
ctx := context.Background()
157161
bodySchema := &schema.BodySchema{
158162
Blocks: map[string]*schema.BlockSchema{
159163
"customblock": {
@@ -186,7 +190,7 @@ func TestDecoder_CandidateAtPos_incompleteBlocks(t *testing.T) {
186190
})
187191
d.maxCandidates = 1
188192

189-
candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
193+
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.Pos{
190194
Line: 3,
191195
Column: 8,
192196
Byte: 42,
@@ -227,6 +231,7 @@ func TestDecoder_CandidateAtPos_incompleteBlocks(t *testing.T) {
227231
}
228232

229233
func TestDecoder_CandidateAtPos_duplicateNames(t *testing.T) {
234+
ctx := context.Background()
230235
bodySchema := &schema.BodySchema{
231236
Attributes: map[string]*schema.AttributeSchema{
232237
"ingress": {
@@ -258,7 +263,7 @@ func TestDecoder_CandidateAtPos_duplicateNames(t *testing.T) {
258263
},
259264
})
260265

261-
candidates, err := d.CandidatesAtPos("test.tf", hcl.InitialPos)
266+
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.InitialPos)
262267
if err != nil {
263268
t.Fatal(err)
264269
}

decoder/candidates.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package decoder
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/hashicorp/hcl-lang/lang"
@@ -13,7 +14,7 @@ import (
1314
//
1415
// Schema is required in order to return any candidates and method will return
1516
// error if there isn't one.
16-
func (d *PathDecoder) CandidatesAtPos(filename string, pos hcl.Pos) (lang.Candidates, error) {
17+
func (d *PathDecoder) CandidatesAtPos(ctx context.Context, filename string, pos hcl.Pos) (lang.Candidates, error) {
1718
f, err := d.fileByName(filename)
1819
if err != nil {
1920
return lang.ZeroCandidates(), err
@@ -37,10 +38,10 @@ func (d *PathDecoder) CandidatesAtPos(filename string, pos hcl.Pos) (lang.Candid
3738
outerBodyRng = ob.Range()
3839
}
3940

40-
return d.candidatesAtPos(rootBody, outerBodyRng, d.pathCtx.Schema, pos)
41+
return d.candidatesAtPos(ctx, rootBody, outerBodyRng, d.pathCtx.Schema, pos)
4142
}
4243

43-
func (d *PathDecoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Range, bodySchema *schema.BodySchema, pos hcl.Pos) (lang.Candidates, error) {
44+
func (d *PathDecoder) candidatesAtPos(ctx context.Context, body *hclsyntax.Body, outerBodyRng hcl.Range, bodySchema *schema.BodySchema, pos hcl.Pos) (lang.Candidates, error) {
4445
if bodySchema == nil {
4546
return lang.ZeroCandidates(), nil
4647
}
@@ -50,10 +51,10 @@ func (d *PathDecoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Ran
5051
for _, attr := range body.Attributes {
5152
if d.isPosInsideAttrExpr(attr, pos) {
5253
if aSchema, ok := bodySchema.Attributes[attr.Name]; ok {
53-
return d.attrValueCandidatesAtPos(attr, aSchema, outerBodyRng, pos)
54+
return d.attrValueCandidatesAtPos(ctx, attr, aSchema, outerBodyRng, pos)
5455
}
5556
if bodySchema.AnyAttribute != nil {
56-
return d.attrValueCandidatesAtPos(attr, bodySchema.AnyAttribute, outerBodyRng, pos)
57+
return d.attrValueCandidatesAtPos(ctx, attr, bodySchema.AnyAttribute, outerBodyRng, pos)
5758
}
5859

5960
return lang.ZeroCandidates(), nil
@@ -132,7 +133,7 @@ func (d *PathDecoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Ran
132133
return lang.ZeroCandidates(), err
133134
}
134135

135-
return d.candidatesAtPos(block.Body, outerBodyRng, mergedSchema, pos)
136+
return d.candidatesAtPos(ctx, block.Body, outerBodyRng, mergedSchema, pos)
136137
}
137138
}
138139
}

0 commit comments

Comments
 (0)