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

Avoid completing duplicate label candidates #32

Merged
merged 1 commit into from
Apr 19, 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
92 changes: 92 additions & 0 deletions decoder/candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,98 @@ func TestDecoder_CandidatesAtPos_emptyLabel(t *testing.T) {
}
}

func TestDecoder_CandidatesAtPos_emptyLabel_duplicateDepKeys(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "name"},
}
resourceSchema := &schema.BlockSchema{
Labels: resourceLabelSchema,
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"count": {Expr: schema.LiteralTypeOnly(cty.Number)},
},
},
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: "azurerm_subnet"},
},
}): {
Attributes: map[string]*schema.AttributeSchema{
"one": {Expr: schema.LiteralTypeOnly(cty.String), IsRequired: true},
"two": {Expr: schema.LiteralTypeOnly(cty.Number)},
"three": {Expr: schema.LiteralTypeOnly(cty.Bool)},
},
},
schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: "azurerm_subnet"},
},
Attributes: []schema.AttributeDependent{
{
Name: "provider",
Expr: schema.ExpressionValue{
Address: lang.Address{
lang.RootStep{Name: "azurerm"},
},
},
},
},
}): {
Attributes: map[string]*schema.AttributeSchema{
"one": {Expr: schema.LiteralTypeOnly(cty.String), IsRequired: true},
"two": {Expr: schema.LiteralTypeOnly(cty.Number)},
"three": {Expr: schema.LiteralTypeOnly(cty.Bool)},
},
},
},
}
bodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"resource": resourceSchema,
},
}

cfg := []byte(`resource "" "" {
}
`)

d := NewDecoder()
d.SetSchema(bodySchema)
f, pDiags := hclsyntax.ParseConfig([]byte(cfg), "test.tf", hcl.InitialPos)
if len(pDiags) > 0 {
t.Fatal(pDiags)
}
err := d.LoadFile("test.tf", f)
if err != nil {
t.Fatal(err)
}

candidates, err := d.CandidatesAtPos("test.tf", hcl.Pos{Line: 1, Column: 11, Byte: 10})
if err != nil {
t.Fatal(err)
}
expectedCandidates := lang.CompleteCandidates([]lang.Candidate{
{
Label: "azurerm_subnet",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 11, Byte: 10},
End: hcl.Pos{Line: 1, Column: 11, Byte: 10},
},
NewText: "azurerm_subnet",
Snippet: "azurerm_subnet",
},
Kind: lang.LabelCandidateKind,
},
})
if diff := cmp.Diff(expectedCandidates, candidates); diff != "" {
t.Fatalf("unexpected candidates: %s", diff)
}
}

func TestDecoder_CandidatesAtPos_basic(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
Expand Down
23 changes: 22 additions & 1 deletion decoder/label_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func (d *Decoder) labelCandidatesFromDependentSchema(idx int, db map[schema.Sche
candidates := lang.NewCandidates()
count := 0

foundCandidateNames := make(map[string]bool, 0)

prefix, _ := d.bytesFromRange(prefixRng)

for schemaKey, bodySchema := range db {
Expand All @@ -32,6 +34,22 @@ func (d *Decoder) labelCandidatesFromDependentSchema(idx int, db map[schema.Sche
if len(prefix) > 0 && !strings.HasPrefix(label.Value, string(prefix)) {
continue
}

// Dependent keys may be duplicated where one
// key is labels-only and other one contains
// labels + attributes.
//
// Specifically in Terraform this applies to
// a resource type depending on 'provider' attribute.
//
// We do need such dependent keys elsewhere
// to know how to do completion within a block
// but this doesn't matter when completing the label itself
// unless/until we're also completing the dependent attributes.
if _, ok := foundCandidateNames[label.Value]; ok {
continue
}

candidates.List = append(candidates.List, lang.Candidate{
Label: label.Value,
Kind: lang.LabelCandidateKind,
Expand All @@ -41,10 +59,13 @@ func (d *Decoder) labelCandidatesFromDependentSchema(idx int, db map[schema.Sche
Snippet: label.Value,
Range: editRng,
},
// TODO: AdditionalTextEdits (required fields if body is empty)
// TODO: AdditionalTextEdits:
// - prefill required fields if body is empty
// - prefill dependent attribute(s)
Detail: bodySchema.Detail,
Description: bodySchema.Description,
})
foundCandidateNames[label.Value] = true
}
}
}
Expand Down