Skip to content

Commit

Permalink
Enable hooks for dynamic completion (#123)
Browse files Browse the repository at this point in the history
* 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 <radek.simko@gmail.com>
  • Loading branch information
dbanck and radeksimko authored Aug 1, 2022
1 parent 48e582c commit 118ac45
Show file tree
Hide file tree
Showing 13 changed files with 546 additions and 47 deletions.
13 changes: 9 additions & 4 deletions decoder/body_candidates_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoder

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -12,6 +13,7 @@ import (
)

func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
ctx := context.Background()
bodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"customblock": {
Expand Down Expand Up @@ -43,7 +45,7 @@ func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
})
d.maxCandidates = 1

candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.Pos{
Line: 2,
Column: 7,
Byte: 29,
Expand Down Expand Up @@ -84,6 +86,7 @@ func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
}

func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
ctx := context.Background()
bodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"customblock": {
Expand Down Expand Up @@ -113,7 +116,7 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
},
})

candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.Pos{
Line: 2,
Column: 7,
Byte: 29,
Expand Down Expand Up @@ -154,6 +157,7 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
}

func TestDecoder_CandidateAtPos_incompleteBlocks(t *testing.T) {
ctx := context.Background()
bodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"customblock": {
Expand Down Expand Up @@ -186,7 +190,7 @@ func TestDecoder_CandidateAtPos_incompleteBlocks(t *testing.T) {
})
d.maxCandidates = 1

candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.Pos{
Line: 3,
Column: 8,
Byte: 42,
Expand Down Expand Up @@ -227,6 +231,7 @@ func TestDecoder_CandidateAtPos_incompleteBlocks(t *testing.T) {
}

func TestDecoder_CandidateAtPos_duplicateNames(t *testing.T) {
ctx := context.Background()
bodySchema := &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"ingress": {
Expand Down Expand Up @@ -258,7 +263,7 @@ func TestDecoder_CandidateAtPos_duplicateNames(t *testing.T) {
},
})

candidates, err := d.CandidatesAtPos("test.tf", hcl.InitialPos)
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.InitialPos)
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 7 additions & 6 deletions decoder/candidates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoder

import (
"context"
"fmt"

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

return d.candidatesAtPos(rootBody, outerBodyRng, d.pathCtx.Schema, pos)
return d.candidatesAtPos(ctx, rootBody, outerBodyRng, d.pathCtx.Schema, pos)
}

func (d *PathDecoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Range, bodySchema *schema.BodySchema, pos hcl.Pos) (lang.Candidates, error) {
func (d *PathDecoder) candidatesAtPos(ctx context.Context, body *hclsyntax.Body, outerBodyRng hcl.Range, bodySchema *schema.BodySchema, pos hcl.Pos) (lang.Candidates, error) {
if bodySchema == nil {
return lang.ZeroCandidates(), nil
}
Expand All @@ -50,10 +51,10 @@ func (d *PathDecoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Ran
for _, attr := range body.Attributes {
if d.isPosInsideAttrExpr(attr, pos) {
if aSchema, ok := bodySchema.Attributes[attr.Name]; ok {
return d.attrValueCandidatesAtPos(attr, aSchema, outerBodyRng, pos)
return d.attrValueCandidatesAtPos(ctx, attr, aSchema, outerBodyRng, pos)
}
if bodySchema.AnyAttribute != nil {
return d.attrValueCandidatesAtPos(attr, bodySchema.AnyAttribute, outerBodyRng, pos)
return d.attrValueCandidatesAtPos(ctx, attr, bodySchema.AnyAttribute, outerBodyRng, pos)
}

return lang.ZeroCandidates(), nil
Expand Down Expand Up @@ -132,7 +133,7 @@ func (d *PathDecoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Ran
return lang.ZeroCandidates(), err
}

return d.candidatesAtPos(block.Body, outerBodyRng, mergedSchema, pos)
return d.candidatesAtPos(ctx, block.Body, outerBodyRng, mergedSchema, pos)
}
}
}
Expand Down
Loading

0 comments on commit 118ac45

Please sign in to comment.