Skip to content

Commit 0b07900

Browse files
authored
Merge pull request #83 from docker/long-form-depends_on-definition
Support looking up services using the long form depends_on syntax
2 parents 40f12b4 + 25b5d6f commit 0b07900

File tree

3 files changed

+84
-23
lines changed

3 files changed

+84
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ All notable changes to the Docker Language Server will be documented in this fil
1010
- support code navigation when a single attribute of a target has been reused ([#78](https://github.com/docker/docker-language-server/issues/78))
1111
- Compose
1212
- textDocument/definition
13-
- support lookup of services referenced by the short form syntax of `depends_on` ([#67](https://github.com/docker/docker-language-server/issues/67))
13+
- support lookup of `services` referenced by the short form syntax of `depends_on` ([#67](https://github.com/docker/docker-language-server/issues/67))
14+
- support lookup of `services` referenced by the long form syntax of `depends_on` ([#68](https://github.com/docker/docker-language-server/issues/68))
1415

1516
### Fixed
1617
- ensure file validation is skipped if the file has since been closed by the editor ([#79](https://github.com/docker/docker-language-server/issues/79))

internal/compose/definition.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,16 @@ func Definition(ctx context.Context, definitionLinkSupport bool, doc document.Co
2121
if service.Content[j].Value == "depends_on" {
2222
if service.Content[j+1].Kind == yaml.SequenceNode {
2323
for _, dependency := range service.Content[j+1].Content {
24-
if dependency.Line == line && dependency.Column <= character && character <= dependency.Column+len(dependency.Value) {
25-
serviceRange := ServiceDefinitionRange(root, dependency.Value)
26-
if serviceRange == nil {
27-
return nil, nil
28-
}
29-
30-
return createDefinitionResult(
31-
definitionLinkSupport,
32-
*serviceRange,
33-
&protocol.Range{
34-
Start: protocol.Position{
35-
Line: params.Position.Line,
36-
Character: protocol.UInteger(dependency.Column - 1),
37-
},
38-
End: protocol.Position{
39-
Line: params.Position.Line,
40-
Character: protocol.UInteger(dependency.Column + len(dependency.Value) - 1),
41-
},
42-
},
43-
params.TextDocument.URI,
44-
), nil
24+
link := serviceDependencyLink(root, definitionLinkSupport, params, dependency, line, character)
25+
if link != nil {
26+
return link, nil
27+
}
28+
}
29+
} else if service.Content[j+1].Kind == yaml.MappingNode {
30+
for k := 0; k < len(service.Content[j+1].Content); k += 2 {
31+
link := serviceDependencyLink(root, definitionLinkSupport, params, service.Content[j+1].Content[k], line, character)
32+
if link != nil {
33+
return link, nil
4534
}
4635
}
4736
}
@@ -54,7 +43,33 @@ func Definition(ctx context.Context, definitionLinkSupport bool, doc document.Co
5443
return nil, nil
5544
}
5645

57-
func ServiceDefinitionRange(root yaml.Node, serviceName string) *protocol.Range {
46+
func serviceDependencyLink(root yaml.Node, definitionLinkSupport bool, params *protocol.DefinitionParams, dependency *yaml.Node, line, character int) any {
47+
if dependency.Line == line && dependency.Column <= character && character <= dependency.Column+len(dependency.Value) {
48+
serviceRange := serviceDefinitionRange(root, dependency.Value)
49+
if serviceRange == nil {
50+
return nil
51+
}
52+
53+
return createDefinitionResult(
54+
definitionLinkSupport,
55+
*serviceRange,
56+
&protocol.Range{
57+
Start: protocol.Position{
58+
Line: params.Position.Line,
59+
Character: protocol.UInteger(dependency.Column - 1),
60+
},
61+
End: protocol.Position{
62+
Line: params.Position.Line,
63+
Character: protocol.UInteger(dependency.Column + len(dependency.Value) - 1),
64+
},
65+
},
66+
params.TextDocument.URI,
67+
)
68+
}
69+
return nil
70+
}
71+
72+
func serviceDefinitionRange(root yaml.Node, serviceName string) *protocol.Range {
5873
for i := 0; i < len(root.Content[0].Content); i += 2 {
5974
switch root.Content[0].Content[i].Value {
6075
case "services":

internal/compose/definition_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,51 @@ services:
106106
},
107107
},
108108
},
109+
{
110+
name: "long syntax form of depends_on in services",
111+
content: `
112+
services:
113+
web:
114+
build: .
115+
depends_on:
116+
db:
117+
condition: service_healthy
118+
restart: true
119+
redis:
120+
condition: service_started
121+
db:
122+
image: postgres
123+
redis:
124+
image: redis`,
125+
line: 8,
126+
character: 9,
127+
locations: []protocol.Location{
128+
{
129+
URI: composeFileURI,
130+
Range: protocol.Range{
131+
Start: protocol.Position{Line: 12, Character: 2},
132+
End: protocol.Position{Line: 12, Character: 7},
133+
},
134+
},
135+
},
136+
links: []protocol.LocationLink{
137+
{
138+
OriginSelectionRange: &protocol.Range{
139+
Start: protocol.Position{Line: 8, Character: 6},
140+
End: protocol.Position{Line: 8, Character: 11},
141+
},
142+
TargetURI: composeFileURI,
143+
TargetRange: protocol.Range{
144+
Start: protocol.Position{Line: 12, Character: 2},
145+
End: protocol.Position{Line: 12, Character: 7},
146+
},
147+
TargetSelectionRange: protocol.Range{
148+
Start: protocol.Position{Line: 12, Character: 2},
149+
End: protocol.Position{Line: 12, Character: 7},
150+
},
151+
},
152+
},
153+
},
109154
}
110155

111156
for _, tc := range testCases {

0 commit comments

Comments
 (0)