Skip to content

Fix code completion panic in an empty Compose file #201

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 15, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ All notable changes to the Docker Language Server will be documented in this fil

### Fixed

- Compose
- textDocument/completion
- fix panic in code completion in an empty file ([#196](https://github.com/docker/docker-language-server/issues/196))
- Bake
- textDocument/publishDiagnostics
- stop flagging `BUILDKIT_SYNTAX` as an unrecognized `ARG` ([#187](https://github.com/docker/docker-language-server/issues/187))
Expand Down
70 changes: 48 additions & 22 deletions internal/compose/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,42 +108,58 @@ func array(line string, character int) bool {
return isArray
}

func createTopLevelItems() []protocol.CompletionItem {
items := []protocol.CompletionItem{}
for attributeName, schema := range schemaProperties() {
item := protocol.CompletionItem{Label: attributeName}
if schema.Description != "" {
item.Documentation = schema.Description
}
items = append(items, item)
}
slices.SortFunc(items, func(a, b protocol.CompletionItem) int {
return strings.Compare(a.Label, b.Label)
})
return items
}

func calculateTopLevelNodeOffset(file *ast.File) int {
if len(file.Docs) == 1 {
if m, ok := file.Docs[0].Body.(*ast.MappingNode); ok {
return m.Values[0].Key.GetToken().Position.Column - 1
}
}
return -1
}

func Completion(ctx context.Context, params *protocol.CompletionParams, manager *document.Manager, doc document.ComposeDocument) (*protocol.CompletionList, error) {
u, err := url.Parse(params.TextDocument.URI)
if err != nil {
return nil, fmt.Errorf("LSP client sent invalid URI: %v", params.TextDocument.URI)
}

if params.Position.Character == 0 {
items := []protocol.CompletionItem{}
for attributeName, schema := range schemaProperties() {
item := protocol.CompletionItem{Label: attributeName}
if schema.Description != "" {
item.Documentation = schema.Description
}
items = append(items, item)
}
slices.SortFunc(items, func(a, b protocol.CompletionItem) int {
return strings.Compare(a.Label, b.Label)
})
return &protocol.CompletionList{Items: items}, nil
file := doc.File()
if file == nil || len(file.Docs) == 0 {
return nil, nil
}

lines := strings.Split(string(doc.Input()), "\n")
lspLine := int(params.Position.Line)
if strings.HasPrefix(strings.TrimSpace(lines[lspLine]), "#") {
return nil, nil
topLevelNodeOffset := calculateTopLevelNodeOffset(file)
if topLevelNodeOffset != -1 && params.Position.Character == uint32(topLevelNodeOffset) {
return &protocol.CompletionList{Items: createTopLevelItems()}, nil
}

file := doc.File()
if file == nil || len(file.Docs) == 0 {
lines := strings.Split(string(doc.Input()), "\n")
if strings.HasPrefix(strings.TrimSpace(lines[lspLine]), "#") {
return nil, nil
}

line := int(lspLine) + 1
character := int(params.Position.Character) + 1
path := constructCompletionNodePath(file, line)
if len(path) == 1 {
if len(path) == 0 {
return &protocol.CompletionList{Items: createTopLevelItems()}, nil
} else if len(path) == 1 {
return nil, nil
} else if path[1].Key.GetToken().Position.Column >= character {
return nil, nil
Expand Down Expand Up @@ -452,9 +468,19 @@ func namedDependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode
}

func constructCompletionNodePath(file *ast.File, line int) []*ast.MappingValueNode {
for _, documentNode := range file.Docs {
if mappingNode, ok := documentNode.Body.(*ast.MappingNode); ok {
return NodeStructure(line, mappingNode.Values)
for i := range len(file.Docs) {
if i+1 == len(file.Docs) {
if mappingNode, ok := file.Docs[i].Body.(*ast.MappingNode); ok {
return NodeStructure(line, mappingNode.Values)
}
}

if m, ok := file.Docs[i].Body.(*ast.MappingNode); ok {
if n, ok := file.Docs[i+1].Body.(*ast.MappingNode); ok {
if m.Values[0].Key.GetToken().Position.Line <= line && line <= n.Values[0].Key.GetToken().Position.Line {
return NodeStructure(line, m.Values)
}
}
}
}
return nil
Expand Down
153 changes: 119 additions & 34 deletions internal/compose/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@ import (
"go.lsp.dev/uri"
)

var topLevelNodes = []protocol.CompletionItem{
{
Label: "configs",
Documentation: "Configurations that are shared among multiple services.",
},
{
Label: "include",
Documentation: "compose sub-projects to be included.",
},
{
Label: "name",
Documentation: "define the Compose project name, until user defines one explicitly.",
},
{
Label: "networks",
Documentation: "Networks that are shared among multiple services.",
},
{
Label: "secrets",
Documentation: "Secrets that are shared among multiple services.",
},
{
Label: "services",
Documentation: "The services that will be used by your application.",
},
{
Label: "version",
Documentation: "declared for backward compatibility, ignored. Please remove it.",
},
{
Label: "volumes",
Documentation: "Named volumes that are shared among multiple services.",
},
}

func serviceProperties(line, character, prefixLength protocol.UInteger) []protocol.CompletionItem {
return []protocol.CompletionItem{
{
Expand Down Expand Up @@ -934,40 +969,90 @@ configs:
line: 3,
character: 0,
list: &protocol.CompletionList{
Items: []protocol.CompletionItem{
{
Label: "configs",
Documentation: "Configurations that are shared among multiple services.",
},
{
Label: "include",
Documentation: "compose sub-projects to be included.",
},
{
Label: "name",
Documentation: "define the Compose project name, until user defines one explicitly.",
},
{
Label: "networks",
Documentation: "Networks that are shared among multiple services.",
},
{
Label: "secrets",
Documentation: "Secrets that are shared among multiple services.",
},
{
Label: "services",
Documentation: "The services that will be used by your application.",
},
{
Label: "version",
Documentation: "declared for backward compatibility, ignored. Please remove it.",
},
{
Label: "volumes",
Documentation: "Named volumes that are shared among multiple services.",
},
},
Items: topLevelNodes,
},
},
{
name: "top level node suggestions with a space in the front",
content: ` `,
line: 0,
character: 1,
list: &protocol.CompletionList{
Items: topLevelNodes,
},
},
{
name: "top level node suggestions with indented content but code completion is unindented",
content: `
configs:
test:
`,
line: 3,
character: 0,
list: nil,
},
{
name: "top level node suggestions with indented content and code completion is aligned correctly",
content: `
configs:
test:
`,
line: 3,
character: 1,
list: &protocol.CompletionList{
Items: topLevelNodes,
},
},
{
name: "alignment correct with multiple documents",
content: `
---
---
configs:
test:
`,
line: 5,
character: 1,
list: &protocol.CompletionList{
Items: topLevelNodes,
},
},
{
name: "alignment incorrect with multiple documents",
content: `
---
configs:
test:
---
configs:
test2:
`,
line: 7,
character: 0,
list: nil,
},
{
name: "top level node suggestions with indented content and code completion is aligned correctly but in a comment",
content: `
configs:
test:
#`,
line: 3,
character: 1,
list: nil,
},
{
name: "top level node suggestions with multiple files",
content: `
---
configs:
test:
---
`,
line: 5,
character: 0,
list: &protocol.CompletionList{
Items: topLevelNodes,
},
},
{
Expand Down
Loading