Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema/decoder: Make label completion more flexible #54

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions decoder/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func (d *Decoder) candidatesAtPos(body *hclsyntax.Body, outerBodyRng hcl.Range,
}
prefixRng.End = pos

labelSchema := bSchema.Labels[i]

if !labelSchema.Completable {
return lang.ZeroCandidates(), nil
}

return d.labelCandidatesFromDependentSchema(i, bSchema.DependentBody, prefixRng, rng)
}
}
Expand Down
65 changes: 57 additions & 8 deletions decoder/candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestDecoder_CandidatesAtPos_nilBodySchema(t *testing.T) {
Blocks: map[string]*schema.BlockSchema{
"resource": {
Labels: []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "type", IsDepKey: true, Completable: true},
{Name: "name"},
},
Body: nil,
Expand Down Expand Up @@ -495,7 +495,7 @@ func TestDecoder_CandidatesAtPos_rightHandSideInString(t *testing.T) {
func TestDecoder_CandidatesAtPos_endOfLabel(t *testing.T) {
blockSchema := &schema.BlockSchema{
Labels: []*schema.LabelSchema{
{Name: "type"},
{Name: "type", Completable: true},
},
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
schema.NewSchemaKey(schema.DependencyKeys{
Expand Down Expand Up @@ -595,9 +595,58 @@ func TestDecoder_CandidatesAtPos_endOfLabel(t *testing.T) {
}
}

func TestDecoder_CandidatesAtPos_nonCompletableLabel(t *testing.T) {
blockSchema := &schema.BlockSchema{
Labels: []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
},
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: "myfirst"},
},
}): {},
schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: "mysecond"},
},
}): {},
},
}
bodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"myblock": blockSchema,
},
}
testConfig := []byte(`myblock "" {
}
`)

d := NewDecoder()
d.SetSchema(bodySchema)
f, _ := hclsyntax.ParseConfig(testConfig, "test.tf", hcl.InitialPos)
err := d.LoadFile("test.tf", f)
if err != nil {
t.Fatal(err)
}

candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{
Line: 1,
Column: 10,
Byte: 9,
})
if err != nil {
t.Fatal(err)
}
expectedCandidates := lang.ZeroCandidates()
if diff := cmp.Diff(expectedCandidates, candidates); diff != "" {
t.Fatalf("unexpected candidates: %s", diff)
}
}

func TestDecoder_CandidatesAtPos_zeroByteContent(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "type", IsDepKey: true, Completable: true},
{Name: "name"},
}
resourceSchema := &schema.BlockSchema{
Expand Down Expand Up @@ -653,7 +702,7 @@ func TestDecoder_CandidatesAtPos_zeroByteContent(t *testing.T) {

func TestDecoder_CandidatesAtPos_endOfFilePos(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "type", IsDepKey: true, Completable: true},
{Name: "name"},
}
resourceSchema := &schema.BlockSchema{
Expand Down Expand Up @@ -717,7 +766,7 @@ func TestDecoder_CandidatesAtPos_endOfFilePos(t *testing.T) {

func TestDecoder_CandidatesAtPos_emptyLabel(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "type", IsDepKey: true, Completable: true},
{Name: "name"},
}
resourceSchema := &schema.BlockSchema{
Expand Down Expand Up @@ -811,7 +860,7 @@ func TestDecoder_CandidatesAtPos_emptyLabel(t *testing.T) {

func TestDecoder_CandidatesAtPos_emptyLabel_duplicateDepKeys(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "type", IsDepKey: true, Completable: true},
{Name: "name"},
}
resourceSchema := &schema.BlockSchema{
Expand Down Expand Up @@ -903,7 +952,7 @@ func TestDecoder_CandidatesAtPos_emptyLabel_duplicateDepKeys(t *testing.T) {

func TestDecoder_CandidatesAtPos_basic(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "type", IsDepKey: true, Completable: true},
{Name: "name"},
}

Expand Down Expand Up @@ -1287,7 +1336,7 @@ func TestDecoder_CandidatesAtPos_AnyAttribute(t *testing.T) {

func TestDecoder_CandidatesAtPos_multipleTypes(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "type", IsDepKey: true, Completable: true},
{Name: "name"},
}

Expand Down
6 changes: 6 additions & 0 deletions schema/label_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type LabelSchema struct {
// IsDepKey describes whether to use this label as key
// when looking up dependent schema
IsDepKey bool

// In cases where label's IsDepKey=true any DependentKey label values
// within Blocks's DependentBody can be used for completion
// This enables such behaviour.
Completable bool
}

func (*LabelSchema) isSchemaImpl() schemaImplSigil {
Expand All @@ -25,6 +30,7 @@ func (ls *LabelSchema) Copy() *LabelSchema {

return &LabelSchema{
Name: ls.Name,
Completable: ls.Completable,
Description: ls.Description,
IsDepKey: ls.IsDepKey,
}
Expand Down