@@ -6,21 +6,23 @@ import (
6
6
"github.com/docker/docker-language-server/internal/pkg/document"
7
7
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
8
8
"github.com/docker/docker-language-server/internal/types"
9
+ "github.com/goccy/go-yaml/ast"
9
10
)
10
11
11
12
func insideRange (rng protocol.Range , line , character protocol.UInteger ) bool {
12
13
return rng .Start .Line == line && rng .Start .Character <= character && character <= rng .End .Character
13
14
}
14
15
15
16
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
19
20
}
20
21
22
+ targetURI := params .TextDocument .URI
21
23
var sourceRange * protocol.Range
22
24
var definitionRange * protocol.Range
23
- for _ , highlight := range highlights {
25
+ for _ , highlight := range dependency . documentHighlights {
24
26
if * highlight .Kind == protocol .DocumentHighlightKindWrite {
25
27
definitionRange = & highlight .Range
26
28
if insideRange (highlight .Range , params .Position .Line , params .Position .Character ) {
@@ -30,7 +32,29 @@ func Definition(ctx context.Context, definitionLinkSupport bool, doc document.Co
30
32
}
31
33
}
32
34
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 {
34
58
if * highlight .Kind == protocol .DocumentHighlightKindRead {
35
59
if insideRange (highlight .Range , params .Position .Line , params .Position .Character ) {
36
60
sourceRange = & highlight .Range
@@ -44,7 +68,7 @@ func Definition(ctx context.Context, definitionLinkSupport bool, doc document.Co
44
68
definitionLinkSupport ,
45
69
* definitionRange ,
46
70
sourceRange ,
47
- params . TextDocument . URI ,
71
+ targetURI ,
48
72
), nil
49
73
}
50
74
return nil , nil
0 commit comments