Skip to content

Commit

Permalink
schema/decoder: Make label completion more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jun 16, 2021
1 parent a5118c3 commit 249ce5a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
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
5 changes: 5 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 Down

0 comments on commit 249ce5a

Please sign in to comment.