Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to the Docker Language Server will be documented in this fil
- textDocument/documentLink
- add anchor resolution for all supported document links ([#348](https://github.com/docker/docker-language-server/issues/348))
- return document links for the `file` attribute of a service object's `extends` attribute object ([#172](https://github.com/docker/docker-language-server/issues/172))
- provide document links for models on Docker Hub and Hugging Face ([#356](https://github.com/docker/docker-language-server/issues/356))
- textDocument/hover
- support hovering over referenced models ([#343](https://github.com/docker/docker-language-server/issues/343))

Expand Down
58 changes: 58 additions & 0 deletions internal/compose/documentLink.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ func createObjectFileLink(u *url.URL, serviceNode *ast.MappingValueNode) *protoc
return nil
}

func createModelLink(serviceNode *ast.MappingValueNode) *protocol.DocumentLink {
if resolveAnchor(serviceNode.Key).GetToken().Value == "model" {
service := stringNode(serviceNode.Value)
if service != nil {
linkedText, link := extractModelLink(service.Value)
if linkedText != "" {
return &protocol.DocumentLink{
Range: createRange(service.GetToken(), len(linkedText)),
Target: types.CreateStringPointer(link),
Tooltip: types.CreateStringPointer(link),
}
}
}
}
return nil
}

func includedFiles(nodes []ast.Node) []*token.Token {
tokens := []*token.Token{}
for _, entry := range nodes {
Expand Down Expand Up @@ -201,6 +218,19 @@ func scanForLinks(u *url.URL, n *ast.MappingValueNode) []protocol.DocumentLink {
}
}
}
case "models":
if mappingNode, ok := resolveAnchor(n.Value).(*ast.MappingNode); ok {
for _, node := range mappingNode.Values {
if serviceAttributes, ok := resolveAnchor(node.Value).(*ast.MappingNode); ok {
for _, serviceAttribute := range serviceAttributes.Values {
link := createModelLink(serviceAttribute)
if link != nil {
links = append(links, *link)
}
}
}
}
}
}
return links
}
Expand Down Expand Up @@ -278,3 +308,31 @@ func extractImageLink(nodeValue string) (string, string) {
}
return nodeValue[0:idx], fmt.Sprintf("https://hub.docker.com/r/%v", nodeValue[0:idx])
}

func extractModelLink(nodeValue string) (string, string) {
if strings.HasPrefix(nodeValue, "hf.co") {
if len(nodeValue) <= 6 {
return "", ""
}
idx := strings.LastIndex(nodeValue, ":")
if idx == -1 {
return nodeValue, fmt.Sprintf("https://%v", nodeValue)
}
return nodeValue[0:idx], fmt.Sprintf("https://%v", nodeValue[0:idx])
}

idx := strings.LastIndex(nodeValue, ":")
if idx == -1 {
idx := strings.Index(nodeValue, "/")
if idx == -1 {
return nodeValue, fmt.Sprintf("https://hub.docker.com/_/%v", nodeValue)
}
return nodeValue, fmt.Sprintf("https://hub.docker.com/r/%v", nodeValue)
}

slashIndex := strings.Index(nodeValue, "/")
if slashIndex == -1 {
return nodeValue[0:idx], fmt.Sprintf("https://hub.docker.com/_/%v", nodeValue[0:idx])
}
return nodeValue[0:idx], fmt.Sprintf("https://hub.docker.com/r/%v", nodeValue[0:idx])
}
Loading
Loading