Skip to content

Commit ab5d382

Browse files
committed
Fix object attribute and recursion bugs in inlay hints
Signed-off-by: Remy Suen <[email protected]>
1 parent d1506c0 commit ab5d382

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

internal/compose/inlayHint.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ func allServiceProperties(node ast.Node) map[string]map[string]ast.Node {
3131
func hierarchyProperties(service string, serviceProps map[string]map[string]ast.Node, chain []map[string]ast.Node) []map[string]ast.Node {
3232
if extends, ok := serviceProps[service]["extends"]; ok {
3333
if s, ok := extends.(*ast.StringNode); ok {
34-
chain = append(chain, hierarchyProperties(s.Value, serviceProps, chain)...)
34+
// block self-referencing recursions
35+
if s.Value != service {
36+
chain = append(chain, hierarchyProperties(s.Value, serviceProps, chain)...)
37+
}
3538
} else if mappingNode, ok := extends.(*ast.MappingNode); ok {
3639
external := false
3740
for _, value := range mappingNode.Values {
@@ -77,6 +80,10 @@ func InlayHint(doc document.ComposeDocument, rng protocol.Range) ([]protocol.Inl
7780
if name == "extends" {
7881
continue
7982
}
83+
// skip object attributes for now
84+
if _, ok := value.(*ast.MappingNode); ok {
85+
continue
86+
}
8087

8188
for _, parentProps := range chain {
8289
if parentProp, ok := parentProps[name]; ok {

internal/compose/inlayHint_test.go

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ services:
5252
},
5353
},
5454
{
55-
name: "attribute recurses upwards",
55+
name: "attribute recurses upwards for a match",
5656
content: `
5757
services:
5858
web:
5959
attach: true
6060
web2:
6161
extends: web
6262
web3:
63-
extends: web
63+
extends: web2
6464
attach: false`,
6565
inlayHints: []protocol.InlayHint{
6666
{
@@ -70,6 +70,60 @@ services:
7070
},
7171
},
7272
},
73+
{
74+
name: "extension at different levels have the right value",
75+
content: `
76+
services:
77+
web:
78+
hostname: hostname1
79+
web2:
80+
extends: web
81+
hostname: hostname2
82+
web3:
83+
extends: web2
84+
hostname: hostname3`,
85+
inlayHints: []protocol.InlayHint{
86+
{
87+
Label: "(parent value: hostname1)",
88+
PaddingLeft: types.CreateBoolPointer(true),
89+
Position: protocol.Position{Line: 6, Character: 23},
90+
},
91+
{
92+
Label: "(parent value: hostname2)",
93+
PaddingLeft: types.CreateBoolPointer(true),
94+
Position: protocol.Position{Line: 9, Character: 23},
95+
},
96+
},
97+
},
98+
{
99+
name: "self recursion returns nothing",
100+
content: `
101+
services:
102+
web:
103+
hostname: hostname1
104+
extends: web2`,
105+
inlayHints: []protocol.InlayHint{},
106+
},
107+
{
108+
name: "self recursion does not affect other hints",
109+
content: `
110+
services:
111+
web:
112+
hostname: hostname1
113+
web2:
114+
extends: web
115+
hostname: hostname2
116+
web3:
117+
extends: web3
118+
hostname: hostname3`,
119+
inlayHints: []protocol.InlayHint{
120+
{
121+
Label: "(parent value: hostname1)",
122+
PaddingLeft: types.CreateBoolPointer(true),
123+
Position: protocol.Position{Line: 6, Character: 23},
124+
},
125+
},
126+
},
73127
{
74128
name: "extends as an object but without a file attribute",
75129
content: `
@@ -118,6 +172,19 @@ services:
118172
},
119173
},
120174
},
175+
{
176+
name: "unmatched tree structure should not render any hints",
177+
content: `
178+
services:
179+
web:
180+
build: .
181+
context: abc
182+
web2:
183+
extends: web
184+
build:
185+
context: def`,
186+
inlayHints: []protocol.InlayHint{},
187+
},
121188
{
122189
name: "sub-attributes unsupported",
123190
content: `

0 commit comments

Comments
 (0)