Skip to content

Suggest service names for the extends attribute #185

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 1 commit into from
May 8, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to the Docker Language Server will be documented in this fil
- support build stage names for the `target` attribute ([#173](https://github.com/docker/docker-language-server/issues/173))
- 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))

### Fixed

Expand Down
127 changes: 93 additions & 34 deletions internal/compose/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,67 @@ type completionItemText struct {
documentation string
}

type textEditModifier struct {
isInterested func(attributeName string, path []*ast.MappingValueNode) bool
modify func(file *ast.File, manager *document.Manager, u *url.URL, edit protocol.TextEdit, attributeName string, path []*ast.MappingValueNode) protocol.TextEdit
}

var buildTargetModifier = textEditModifier{
isInterested: func(attributeName string, path []*ast.MappingValueNode) bool {
return attributeName == "target" && len(path) == 3 && path[2].Key.GetToken().Value == "build"
},
modify: func(file *ast.File, manager *document.Manager, u *url.URL, edit protocol.TextEdit, attributeName string, path []*ast.MappingValueNode) protocol.TextEdit {
if _, ok := path[2].Value.(*ast.NullNode); ok {
dockerfilePath, err := types.LocalDockerfile(u)
if err == nil {
stages := findBuildStages(manager, dockerfilePath, "")
if len(stages) > 0 {
edit.NewText = fmt.Sprintf("%v%v", edit.NewText, createChoiceSnippetText(stages))
return edit
}
}
} else if mappingNode, ok := path[2].Value.(*ast.MappingNode); ok {
dockerfileAttributePath := "Dockerfile"
for _, buildAttribute := range mappingNode.Values {
switch buildAttribute.Key.GetToken().Value {
case "dockerfile_inline":
return edit
case "dockerfile":
dockerfileAttributePath = buildAttribute.Value.GetToken().Value
}
}

dockerfilePath, err := types.AbsolutePath(u, dockerfileAttributePath)
if err == nil {
stages := findBuildStages(manager, dockerfilePath, "")
if len(stages) > 0 {
edit.NewText = fmt.Sprintf("%v%v", edit.NewText, createChoiceSnippetText(stages))
return edit
}
}
}
return edit
},
}

var serviceSuggestionModifier = textEditModifier{
isInterested: func(attributeName string, path []*ast.MappingValueNode) bool {
return attributeName == "service" && len(path) == 3 && path[0].Key.GetToken().Value == "services" && path[2].Key.GetToken().Value == "extends"
},
modify: func(file *ast.File, manager *document.Manager, u *url.URL, edit protocol.TextEdit, attributeName string, path []*ast.MappingValueNode) protocol.TextEdit {
services := []completionItemText{}
for _, service := range findDependencies(file, "services") {
if service != path[1].Key.GetToken().Value {
services = append(services, completionItemText{newText: service})
}
}
edit.NewText = fmt.Sprintf("%v%v", edit.NewText, createChoiceSnippetText(services))
return edit
},
}

var textEditModifiers = []textEditModifier{buildTargetModifier, serviceSuggestionModifier}

func prefix(line string, character int) string {
sb := strings.Builder{}
for i := range character {
Expand Down Expand Up @@ -89,7 +150,8 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
}

wordPrefix := prefix(lines[lspLine], character-1)
dependencies := dependencyCompletionItems(file, path, params, protocol.UInteger(len(wordPrefix)))
path, nodeProps, arrayAttributes := nodeProperties(path, line, character)
dependencies := dependencyCompletionItems(file, path, nodeProps, params, protocol.UInteger(len(wordPrefix)))
if len(dependencies) > 0 {
return &protocol.CompletionList{Items: dependencies}, nil
}
Expand All @@ -107,7 +169,6 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
items = namedDependencyCompletionItems(file, path, "secrets", "secrets", params, protocol.UInteger(len(wordPrefix)))
}
isArray := array(lines[lspLine], character-1)
path, nodeProps, arrayAttributes := nodeProperties(path, line, character)
if isArray != arrayAttributes {
return nil, nil
}
Expand Down Expand Up @@ -190,7 +251,7 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
},
}
}
item.TextEdit = modifyTextEdit(manager, u, item.TextEdit.(protocol.TextEdit), attributeName, path)
item.TextEdit = modifyTextEdit(file, manager, u, item.TextEdit.(protocol.TextEdit), attributeName, path)
items = append(items, item)
}
}
Expand All @@ -216,36 +277,10 @@ func createChoiceSnippetText(itemTexts []completionItemText) string {
return sb.String()
}

func modifyTextEdit(manager *document.Manager, u *url.URL, edit protocol.TextEdit, attributeName string, path []*ast.MappingValueNode) protocol.TextEdit {
if attributeName == "target" && len(path) == 3 && path[2].Key.GetToken().Value == "build" {
if _, ok := path[2].Value.(*ast.NullNode); ok {
dockerfilePath, err := types.LocalDockerfile(u)
if err == nil {
stages := findBuildStages(manager, dockerfilePath, "")
if len(stages) > 0 {
edit.NewText = fmt.Sprintf("%v%v", edit.NewText, createChoiceSnippetText(stages))
return edit
}
}
} else if mappingNode, ok := path[2].Value.(*ast.MappingNode); ok {
dockerfileAttributePath := "Dockerfile"
for _, buildAttribute := range mappingNode.Values {
switch buildAttribute.Key.GetToken().Value {
case "dockerfile_inline":
return edit
case "dockerfile":
dockerfileAttributePath = buildAttribute.Value.GetToken().Value
}
}

dockerfilePath, err := types.AbsolutePath(u, dockerfileAttributePath)
if err == nil {
stages := findBuildStages(manager, dockerfilePath, "")
if len(stages) > 0 {
edit.NewText = fmt.Sprintf("%v%v", edit.NewText, createChoiceSnippetText(stages))
return edit
}
}
func modifyTextEdit(file *ast.File, manager *document.Manager, u *url.URL, edit protocol.TextEdit, attributeName string, path []*ast.MappingValueNode) protocol.TextEdit {
for _, modified := range textEditModifiers {
if modified.isInterested(attributeName, path) {
return modified.modify(file, manager, u, edit, attributeName, path)
}
}
return edit
Expand Down Expand Up @@ -339,7 +374,7 @@ func createBuildStageItems(params *protocol.CompletionParams, manager *document.
return items
}

func dependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, params *protocol.CompletionParams, prefixLength protocol.UInteger) []protocol.CompletionItem {
func dependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, nodeProps any, params *protocol.CompletionParams, prefixLength protocol.UInteger) []protocol.CompletionItem {
dependency := map[string]string{
"depends_on": "services",
"networks": "networks",
Expand All @@ -350,6 +385,30 @@ func dependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, par
return items
}
}
if len(path) >= 3 && path[2].Key.GetToken().Value == "extends" && path[0].Key.GetToken().Value == "services" {
if (len(path) == 4 && path[3].Key.GetToken().Value == "service") || params.Position.Line == protocol.UInteger(path[2].Key.GetToken().Position.Line)-1 {
items := []protocol.CompletionItem{}
for _, service := range findDependencies(file, "services") {
if service != path[1].Key.GetToken().Value {
item := protocol.CompletionItem{
Label: service,
TextEdit: protocol.TextEdit{
NewText: service,
Range: protocol.Range{
Start: protocol.Position{
Line: params.Position.Line,
Character: params.Position.Character - prefixLength,
},
End: params.Position,
},
},
}
items = append(items, item)
}
}
return items
}
}
return nil
}

Expand Down
76 changes: 75 additions & 1 deletion internal/compose/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,7 @@ services:
}
}

func TestCompletion_Custom(t *testing.T) {
func TestCompletion_NamedDependencies(t *testing.T) {
testCases := []struct {
name string
content string
Expand Down Expand Up @@ -2491,6 +2491,80 @@ services:
},
},
},
{
name: "extends as a string",
content: `
services:
test:
image: alpine
extends:
test2:
image: alpine`,
line: 4,
character: 13,
list: &protocol.CompletionList{
Items: []protocol.CompletionItem{
{
Label: "test2",
TextEdit: textEdit("test2", 4, 13, 0),
},
},
},
},
{
name: "extends as an object with the service attribute",
content: `
services:
test:
image: alpine
extends:
service:
test2:
image: alpine`,
line: 5,
character: 15,
list: &protocol.CompletionList{
Items: []protocol.CompletionItem{
{
Label: "test2",
TextEdit: textEdit("test2", 5, 15, 0),
},
},
},
},
{
name: "extends object attributes",
content: `
services:
test:
image: alpine
extends:

test2:
image: alpine`,
line: 5,
character: 6,
list: &protocol.CompletionList{
Items: []protocol.CompletionItem{
{
Label: "file",
Detail: types.CreateStringPointer("string"),
Documentation: "The file path where the service to extend is defined.",
TextEdit: textEdit("file: ", 5, 6, 0),
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
InsertTextFormat: types.CreateInsertTextFormatPointer(protocol.InsertTextFormatSnippet),
},
{
Label: "service",
Detail: types.CreateStringPointer("string"),
Documentation: "The name of the service to extend.",
TextEdit: textEdit("service: ${1|test2|}", 5, 6, 0),
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
InsertTextFormat: types.CreateInsertTextFormatPointer(protocol.InsertTextFormatSnippet),
},
},
},
},
{
name: "networks array items",
content: `
Expand Down
Loading