Skip to content

Commit b20d628

Browse files
authored
Fix bug with DependentBody merging of Extensions (#147)
1 parent b8c8114 commit b20d628

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

decoder/body_extensions_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,104 @@ func TestCompletionAtPos_BodySchema_Extensions(t *testing.T) {
8686
},
8787
}),
8888
},
89+
{
90+
"count attribute completion with DependentBody",
91+
&schema.BodySchema{
92+
Blocks: map[string]*schema.BlockSchema{
93+
"resource": {
94+
Labels: []*schema.LabelSchema{
95+
{
96+
Name: "type",
97+
IsDepKey: true,
98+
},
99+
{
100+
Name: "name",
101+
},
102+
},
103+
Body: &schema.BodySchema{
104+
Extensions: &schema.BodyExtensions{
105+
Count: true,
106+
},
107+
},
108+
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
109+
schema.NewSchemaKey(schema.DependencyKeys{
110+
Labels: []schema.LabelDependent{
111+
{Index: 0, Value: "aws_instance"},
112+
},
113+
}): {
114+
Attributes: map[string]*schema.AttributeSchema{
115+
"instance_size": {
116+
IsOptional: true,
117+
Expr: schema.LiteralTypeOnly(cty.String),
118+
},
119+
},
120+
},
121+
},
122+
},
123+
},
124+
},
125+
reference.Targets{},
126+
`resource "aws_instance" "foo" {
127+
128+
}
129+
`,
130+
hcl.Pos{
131+
Line: 2,
132+
Column: 1,
133+
Byte: 32,
134+
},
135+
lang.CompleteCandidates([]lang.Candidate{
136+
{
137+
Label: "count",
138+
Description: lang.MarkupContent{
139+
Value: "The distinct index number (starting with 0) corresponding to the instance",
140+
Kind: lang.MarkdownKind,
141+
},
142+
Detail: "optional, number",
143+
TextEdit: lang.TextEdit{
144+
Range: hcl.Range{
145+
Filename: "test.tf",
146+
Start: hcl.Pos{
147+
Line: 2,
148+
Column: 1,
149+
Byte: 32,
150+
},
151+
End: hcl.Pos{
152+
Line: 2,
153+
Column: 1,
154+
Byte: 32,
155+
},
156+
},
157+
NewText: "count",
158+
Snippet: "count = ",
159+
},
160+
TriggerSuggest: true,
161+
Kind: lang.AttributeCandidateKind,
162+
},
163+
{
164+
Label: "instance_size",
165+
Detail: "optional, string",
166+
TextEdit: lang.TextEdit{
167+
Range: hcl.Range{
168+
Filename: "test.tf",
169+
Start: hcl.Pos{
170+
Line: 2,
171+
Column: 1,
172+
Byte: 32,
173+
},
174+
End: hcl.Pos{
175+
Line: 2,
176+
Column: 1,
177+
Byte: 32,
178+
},
179+
},
180+
NewText: "instance_size",
181+
Snippet: `instance_size = "${1:value}"`,
182+
},
183+
Kind: lang.AttributeCandidateKind,
184+
},
185+
}),
186+
},
89187
{
90188
"count attribute completion does not complete count when extensions not enabled",
91189
&schema.BodySchema{

decoder/decoder.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,18 @@ func mergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*
7474

7575
mergedSchema.TargetableAs = append(mergedSchema.TargetableAs, depSchema.TargetableAs...)
7676
mergedSchema.ImpliedOrigins = append(mergedSchema.ImpliedOrigins, depSchema.ImpliedOrigins...)
77+
78+
// TODO: avoid resetting?
7779
mergedSchema.Targets = depSchema.Targets.Copy()
80+
81+
// TODO: avoid resetting?
7882
mergedSchema.DocsLink = depSchema.DocsLink.Copy()
79-
mergedSchema.Extensions = depSchema.Extensions.Copy()
83+
84+
// use extensions of DependentBody if not nil
85+
// (to avoid resetting to nil)
86+
if depSchema.Extensions != nil {
87+
mergedSchema.Extensions = depSchema.Extensions.Copy()
88+
}
8089
}
8190

8291
return mergedSchema, nil

0 commit comments

Comments
 (0)