Skip to content

Commit 2577707

Browse files
authored
Merge pull request #169 from docker/compose-inlay-hints-bug-fixes
Fix object attribute and recursion bugs in inlay hints
2 parents d1506c0 + f179a77 commit 2577707

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-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: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"slices"
78
"strings"
89
"testing"
910

@@ -52,15 +53,15 @@ services:
5253
},
5354
},
5455
{
55-
name: "attribute recurses upwards",
56+
name: "attribute recurses upwards for a match",
5657
content: `
5758
services:
5859
web:
5960
attach: true
6061
web2:
6162
extends: web
6263
web3:
63-
extends: web
64+
extends: web2
6465
attach: false`,
6566
inlayHints: []protocol.InlayHint{
6667
{
@@ -70,6 +71,60 @@ services:
7071
},
7172
},
7273
},
74+
{
75+
name: "extension at different levels have the right value",
76+
content: `
77+
services:
78+
web:
79+
hostname: hostname1
80+
web2:
81+
extends: web
82+
hostname: hostname2
83+
web3:
84+
extends: web2
85+
hostname: hostname3`,
86+
inlayHints: []protocol.InlayHint{
87+
{
88+
Label: "(parent value: hostname1)",
89+
PaddingLeft: types.CreateBoolPointer(true),
90+
Position: protocol.Position{Line: 6, Character: 23},
91+
},
92+
{
93+
Label: "(parent value: hostname2)",
94+
PaddingLeft: types.CreateBoolPointer(true),
95+
Position: protocol.Position{Line: 9, Character: 23},
96+
},
97+
},
98+
},
99+
{
100+
name: "self recursion returns nothing",
101+
content: `
102+
services:
103+
web:
104+
hostname: hostname1
105+
extends: web2`,
106+
inlayHints: []protocol.InlayHint{},
107+
},
108+
{
109+
name: "self recursion does not affect other hints",
110+
content: `
111+
services:
112+
web:
113+
hostname: hostname1
114+
web2:
115+
extends: web
116+
hostname: hostname2
117+
web3:
118+
extends: web3
119+
hostname: hostname3`,
120+
inlayHints: []protocol.InlayHint{
121+
{
122+
Label: "(parent value: hostname1)",
123+
PaddingLeft: types.CreateBoolPointer(true),
124+
Position: protocol.Position{Line: 6, Character: 23},
125+
},
126+
},
127+
},
73128
{
74129
name: "extends as an object but without a file attribute",
75130
content: `
@@ -118,6 +173,19 @@ services:
118173
},
119174
},
120175
},
176+
{
177+
name: "unmatched tree structure should not render any hints",
178+
content: `
179+
services:
180+
web:
181+
build: .
182+
context: abc
183+
web2:
184+
extends: web
185+
build:
186+
context: def`,
187+
inlayHints: []protocol.InlayHint{},
188+
},
121189
{
122190
name: "sub-attributes unsupported",
123191
content: `
@@ -138,6 +206,9 @@ services:
138206
t.Run(tc.name, func(t *testing.T) {
139207
doc := document.NewComposeDocument(u, 1, []byte(tc.content))
140208
inlayHints, err := InlayHint(doc, protocol.Range{})
209+
slices.SortFunc(inlayHints, func(a protocol.InlayHint, b protocol.InlayHint) int {
210+
return int(a.Position.Line) - int(b.Position.Line)
211+
})
141212
require.NoError(t, err)
142213
require.Equal(t, tc.inlayHints, inlayHints)
143214
})

0 commit comments

Comments
 (0)