|
| 1 | +package compose |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/docker/docker-language-server/internal/pkg/document" |
| 5 | + "github.com/docker/docker-language-server/internal/tliron/glsp/protocol" |
| 6 | + "github.com/goccy/go-yaml/ast" |
| 7 | + "github.com/goccy/go-yaml/token" |
| 8 | +) |
| 9 | + |
| 10 | +func serviceReferences(node *ast.MappingValueNode, dependencyAttributeName string) []*token.Token { |
| 11 | + if servicesNode, ok := node.Value.(*ast.MappingNode); ok { |
| 12 | + tokens := []*token.Token{} |
| 13 | + for _, serviceNode := range servicesNode.Values { |
| 14 | + if serviceAttributes, ok := serviceNode.Value.(*ast.MappingNode); ok { |
| 15 | + for _, attributeNode := range serviceAttributes.Values { |
| 16 | + if attributeNode.Key.GetToken().Value == dependencyAttributeName { |
| 17 | + if sequenceNode, ok := attributeNode.Value.(*ast.SequenceNode); ok { |
| 18 | + for _, service := range sequenceNode.Values { |
| 19 | + tokens = append(tokens, service.GetToken()) |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + return tokens |
| 27 | + } |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +func declarations(node *ast.MappingValueNode, dependencyType string) []*token.Token { |
| 32 | + if s, ok := node.Key.(*ast.StringNode); ok && s.Value == dependencyType { |
| 33 | + if servicesNode, ok := node.Value.(*ast.MappingNode); ok { |
| 34 | + tokens := []*token.Token{} |
| 35 | + for _, serviceNode := range servicesNode.Values { |
| 36 | + tokens = append(tokens, serviceNode.Key.GetToken()) |
| 37 | + } |
| 38 | + return tokens |
| 39 | + } |
| 40 | + } |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +func DocumentHighlight(doc document.ComposeDocument, position protocol.Position) ([]protocol.DocumentHighlight, error) { |
| 45 | + file := doc.File() |
| 46 | + if file == nil || len(file.Docs) == 0 { |
| 47 | + return nil, nil |
| 48 | + } |
| 49 | + |
| 50 | + line := int(position.Line) + 1 |
| 51 | + character := int(position.Character) + 1 |
| 52 | + if mappingNode, ok := file.Docs[0].Body.(*ast.MappingNode); ok { |
| 53 | + for _, node := range mappingNode.Values { |
| 54 | + if s, ok := node.Key.(*ast.StringNode); ok { |
| 55 | + switch s.Value { |
| 56 | + case "services": |
| 57 | + refs := serviceReferences(node, "depends_on") |
| 58 | + decls := declarations(node, "services") |
| 59 | + highlights := highlightServiceReferences(refs, decls, line, character) |
| 60 | + if len(highlights) > 0 { |
| 61 | + return highlights, nil |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return nil, nil |
| 68 | +} |
| 69 | + |
| 70 | +func highlightServiceReferences(refs, decls []*token.Token, line, character int) []protocol.DocumentHighlight { |
| 71 | + var match *token.Token |
| 72 | + for _, reference := range refs { |
| 73 | + if inToken(reference, line, character) { |
| 74 | + match = reference |
| 75 | + break |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if match == nil { |
| 80 | + for _, declaration := range decls { |
| 81 | + if inToken(declaration, line, character) { |
| 82 | + match = declaration |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if match != nil { |
| 89 | + highlights := []protocol.DocumentHighlight{} |
| 90 | + for _, reference := range refs { |
| 91 | + if reference.Value == match.Value { |
| 92 | + highlights = append(highlights, documentHighlightFromToken(reference, protocol.DocumentHighlightKindRead)) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + for _, declaration := range decls { |
| 97 | + if declaration.Value == match.Value { |
| 98 | + highlights = append(highlights, documentHighlightFromToken(declaration, protocol.DocumentHighlightKindWrite)) |
| 99 | + } |
| 100 | + } |
| 101 | + return highlights |
| 102 | + } |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func documentHighlightFromToken(t *token.Token, kind protocol.DocumentHighlightKind) protocol.DocumentHighlight { |
| 107 | + return documentHighlight( |
| 108 | + protocol.UInteger(t.Position.Line)-1, |
| 109 | + protocol.UInteger(t.Position.Column)-1, |
| 110 | + protocol.UInteger(t.Position.Line)-1, |
| 111 | + protocol.UInteger(t.Position.Column+len(t.Value))-1, |
| 112 | + kind, |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +func documentHighlight(startLine, startCharacter, endLine, endCharacter protocol.UInteger, kind protocol.DocumentHighlightKind) protocol.DocumentHighlight { |
| 117 | + return protocol.DocumentHighlight{ |
| 118 | + Kind: &kind, |
| 119 | + Range: protocol.Range{ |
| 120 | + Start: protocol.Position{ |
| 121 | + Line: startLine, |
| 122 | + Character: startCharacter, |
| 123 | + }, |
| 124 | + End: protocol.Position{ |
| 125 | + Line: endLine, |
| 126 | + Character: endCharacter, |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func inToken(t *token.Token, line, character int) bool { |
| 133 | + return t.Position.Line == line && t.Position.Column <= character && character <= t.Position.Column+len(t.Value) |
| 134 | +} |
0 commit comments