| 14 | } |
| 15 | |
| 16 | func Definition(ctx context.Context, definitionLinkSupport bool, doc document.ComposeDocument, params *protocol.DefinitionParams) (any, error) { |
| 17 | name, dependency := DocumentHighlights(doc, params.Position) |
| 18 | if len(dependency.documentHighlights) == 0 { |
| 19 | return nil, nil |
| 20 | } |
| 21 | |
| 22 | targetURI := params.TextDocument.URI |
| 23 | var sourceRange *protocol.Range |
| 24 | var definitionRange *protocol.Range |
| 25 | for _, highlight := range dependency.documentHighlights { |
| 26 | if *highlight.Kind == protocol.DocumentHighlightKindWrite { |
| 27 | definitionRange = &highlight.Range |
| 28 | if insideRange(highlight.Range, params.Position.Line, params.Position.Character) { |
| 29 | sourceRange = &highlight.Range |
| 30 | break |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | if definitionRange == nil { |
| 36 | node, u := dependencyLookup(doc, dependency.dependencyType, name) |
| 37 | if node != nil { |
| 38 | r := createRange(node.Key.GetToken(), len(node.Key.GetToken().Value)) |
| 39 | definitionRange = &r |
| 40 | targetURI = u |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | for _, highlight := range dependency.documentHighlights { |
| 45 | if *highlight.Kind == protocol.DocumentHighlightKindRead { |
| 46 | if insideRange(highlight.Range, params.Position.Line, params.Position.Character) { |
| 47 | sourceRange = &highlight.Range |
| 48 | break |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if definitionRange != nil { |
| 54 | return types.CreateDefinitionResult( |
| 55 | definitionLinkSupport, |
| 56 | *definitionRange, |
| 57 | sourceRange, |
| 58 | targetURI, |
| 59 | ), nil |
| 60 | } |
| 61 | return nil, nil |
| 62 | } |
| 63 | |
| 64 | func dependencyLookup(doc document.ComposeDocument, dependencyType, name string) (*ast.MappingValueNode, string) { |
| 65 | files, _ := doc.IncludedFiles() |