Skip to content

Support navigating to the definition in another Compose file #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ All notable changes to the Docker Language Server will be documented in this fil
- set schema documentation to the completion items ([#176](https://github.com/docker/docker-language-server/issues/176))
- automatically suggest boolean values for simple boolean attributes ([#179](https://github.com/docker/docker-language-server/issues/179))
- suggest service names for a service's `extends` or `extends.service` attribute ([#184](https://github.com/docker/docker-language-server/issues/184))
- textDocument/definition
- support navigating to a dependency that is defined in another file ([#190](https://github.com/docker/docker-language-server/issues/190))
- textDocument/hover
- render a referenced service's YAML content as a hover ([#157](https://github.com/docker/docker-language-server/issues/157))

Expand Down
7 changes: 4 additions & 3 deletions internal/compose/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,7 @@ services:
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
manager := document.NewDocumentManager()
doc := document.NewComposeDocument(uri.URI(composeFileURI), 1, []byte(tc.content))
doc := document.NewComposeDocument(manager, uri.URI(composeFileURI), 1, []byte(tc.content))
list, err := Completion(context.Background(), &protocol.CompletionParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{URI: composeFileURI},
Expand Down Expand Up @@ -3124,7 +3124,8 @@ secrets:

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
doc := document.NewComposeDocument(uri.URI(composeFileURI), 1, []byte(tc.content))
manager := document.NewDocumentManager()
doc := document.NewComposeDocument(manager, uri.URI(composeFileURI), 1, []byte(tc.content))
list, err := Completion(context.Background(), &protocol.CompletionParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{URI: composeFileURI},
Expand Down Expand Up @@ -3443,7 +3444,7 @@ services:
require.NoError(t, err)
require.True(t, changed)
}
doc := document.NewComposeDocument(uri.URI(composeFileURI), 1, []byte(tc.content))
doc := document.NewComposeDocument(manager, uri.URI(composeFileURI), 1, []byte(tc.content))
list, err := Completion(context.Background(), &protocol.CompletionParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{URI: composeFileURI},
Expand Down
36 changes: 30 additions & 6 deletions internal/compose/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import (
"github.com/docker/docker-language-server/internal/pkg/document"
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
"github.com/docker/docker-language-server/internal/types"
"github.com/goccy/go-yaml/ast"
)

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

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

targetURI := params.TextDocument.URI
var sourceRange *protocol.Range
var definitionRange *protocol.Range
for _, highlight := range highlights {
for _, highlight := range dependency.documentHighlights {
if *highlight.Kind == protocol.DocumentHighlightKindWrite {
definitionRange = &highlight.Range
if insideRange(highlight.Range, params.Position.Line, params.Position.Character) {
Expand All @@ -30,7 +32,29 @@ func Definition(ctx context.Context, definitionLinkSupport bool, doc document.Co
}
}

for _, highlight := range highlights {
if definitionRange == nil {
files, _ := doc.IncludedFiles()
fileSearch:
for u, file := range files {
for _, doc := range file.Docs {
if mappingNode, ok := doc.Body.(*ast.MappingNode); ok {
for _, node := range mappingNode.Values {
if s, ok := node.Key.(*ast.StringNode); ok && s.Value == dependency.dependencyType {
for _, service := range node.Value.(*ast.MappingNode).Values {
if s, ok := service.Key.(*ast.StringNode); ok && s.Value == name {
definitionRange = rangeFromToken(s.Token)
targetURI = u
break fileSearch
}
}
}
}
}
}
}
}

for _, highlight := range dependency.documentHighlights {
if *highlight.Kind == protocol.DocumentHighlightKindRead {
if insideRange(highlight.Range, params.Position.Line, params.Position.Character) {
sourceRange = &highlight.Range
Expand All @@ -44,7 +68,7 @@ func Definition(ctx context.Context, definitionLinkSupport bool, doc document.Co
definitionLinkSupport,
*definitionRange,
sourceRange,
params.TextDocument.URI,
targetURI,
), nil
}
return nil, nil
Expand Down
Loading
Loading