Skip to content

Commit 7ca61d1

Browse files
authored
Merge pull request #191 from docker/navigate-other-compose-file
Support navigating to the definition in another Compose file
2 parents ff32320 + a19ad54 commit 7ca61d1

21 files changed

+687
-82
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ All notable changes to the Docker Language Server will be documented in this fil
1212
- set schema documentation to the completion items ([#176](https://github.com/docker/docker-language-server/issues/176))
1313
- automatically suggest boolean values for simple boolean attributes ([#179](https://github.com/docker/docker-language-server/issues/179))
1414
- suggest service names for a service's `extends` or `extends.service` attribute ([#184](https://github.com/docker/docker-language-server/issues/184))
15+
- textDocument/definition
16+
- support navigating to a dependency that is defined in another file ([#190](https://github.com/docker/docker-language-server/issues/190))
1517
- textDocument/hover
1618
- render a referenced service's YAML content as a hover ([#157](https://github.com/docker/docker-language-server/issues/157))
1719

internal/compose/completion_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,7 @@ services:
23832383
for _, tc := range testCases {
23842384
t.Run(tc.name, func(t *testing.T) {
23852385
manager := document.NewDocumentManager()
2386-
doc := document.NewComposeDocument(uri.URI(composeFileURI), 1, []byte(tc.content))
2386+
doc := document.NewComposeDocument(manager, uri.URI(composeFileURI), 1, []byte(tc.content))
23872387
list, err := Completion(context.Background(), &protocol.CompletionParams{
23882388
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
23892389
TextDocument: protocol.TextDocumentIdentifier{URI: composeFileURI},
@@ -3124,7 +3124,8 @@ secrets:
31243124

31253125
for _, tc := range testCases {
31263126
t.Run(tc.name, func(t *testing.T) {
3127-
doc := document.NewComposeDocument(uri.URI(composeFileURI), 1, []byte(tc.content))
3127+
manager := document.NewDocumentManager()
3128+
doc := document.NewComposeDocument(manager, uri.URI(composeFileURI), 1, []byte(tc.content))
31283129
list, err := Completion(context.Background(), &protocol.CompletionParams{
31293130
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
31303131
TextDocument: protocol.TextDocumentIdentifier{URI: composeFileURI},
@@ -3443,7 +3444,7 @@ services:
34433444
require.NoError(t, err)
34443445
require.True(t, changed)
34453446
}
3446-
doc := document.NewComposeDocument(uri.URI(composeFileURI), 1, []byte(tc.content))
3447+
doc := document.NewComposeDocument(manager, uri.URI(composeFileURI), 1, []byte(tc.content))
34473448
list, err := Completion(context.Background(), &protocol.CompletionParams{
34483449
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
34493450
TextDocument: protocol.TextDocumentIdentifier{URI: composeFileURI},

internal/compose/definition.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ import (
66
"github.com/docker/docker-language-server/internal/pkg/document"
77
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
88
"github.com/docker/docker-language-server/internal/types"
9+
"github.com/goccy/go-yaml/ast"
910
)
1011

1112
func insideRange(rng protocol.Range, line, character protocol.UInteger) bool {
1213
return rng.Start.Line == line && rng.Start.Character <= character && character <= rng.End.Character
1314
}
1415

1516
func Definition(ctx context.Context, definitionLinkSupport bool, doc document.ComposeDocument, params *protocol.DefinitionParams) (any, error) {
16-
highlights, err := DocumentHighlight(doc, params.Position)
17-
if err != nil {
18-
return nil, err
17+
name, dependency := DocumentHighlights(doc, params.Position)
18+
if len(dependency.documentHighlights) == 0 {
19+
return nil, nil
1920
}
2021

22+
targetURI := params.TextDocument.URI
2123
var sourceRange *protocol.Range
2224
var definitionRange *protocol.Range
23-
for _, highlight := range highlights {
25+
for _, highlight := range dependency.documentHighlights {
2426
if *highlight.Kind == protocol.DocumentHighlightKindWrite {
2527
definitionRange = &highlight.Range
2628
if insideRange(highlight.Range, params.Position.Line, params.Position.Character) {
@@ -30,7 +32,29 @@ func Definition(ctx context.Context, definitionLinkSupport bool, doc document.Co
3032
}
3133
}
3234

33-
for _, highlight := range highlights {
35+
if definitionRange == nil {
36+
files, _ := doc.IncludedFiles()
37+
fileSearch:
38+
for u, file := range files {
39+
for _, doc := range file.Docs {
40+
if mappingNode, ok := doc.Body.(*ast.MappingNode); ok {
41+
for _, node := range mappingNode.Values {
42+
if s, ok := node.Key.(*ast.StringNode); ok && s.Value == dependency.dependencyType {
43+
for _, service := range node.Value.(*ast.MappingNode).Values {
44+
if s, ok := service.Key.(*ast.StringNode); ok && s.Value == name {
45+
definitionRange = rangeFromToken(s.Token)
46+
targetURI = u
47+
break fileSearch
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
for _, highlight := range dependency.documentHighlights {
3458
if *highlight.Kind == protocol.DocumentHighlightKindRead {
3559
if insideRange(highlight.Range, params.Position.Line, params.Position.Character) {
3660
sourceRange = &highlight.Range
@@ -44,7 +68,7 @@ func Definition(ctx context.Context, definitionLinkSupport bool, doc document.Co
4468
definitionLinkSupport,
4569
*definitionRange,
4670
sourceRange,
47-
params.TextDocument.URI,
71+
targetURI,
4872
), nil
4973
}
5074
return nil, nil

0 commit comments

Comments
 (0)