From cd4d64b8c70940e350e320130e233adebf3ed56b Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Tue, 10 Jun 2025 14:08:16 -0400 Subject: [PATCH] Fix offset calculation issue when calculating Compose suggestions Signed-off-by: Remy Suen --- CHANGELOG.md | 8 ++++++++ internal/compose/completion.go | 5 +++++ internal/compose/completion_test.go | 14 ++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 125f4c0..ccbd193 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to the Docker Language Server will be documented in this file. +## [Unreleased] + +### Fixed + +- Compose + - textDocument/completion + - fix error case triggered by using code completion before the first node ([#314](https://github.com/docker/docker-language-server/issues/314)) + ## [0.11.0] - 2025-06-10 ### Added diff --git a/internal/compose/completion.go b/internal/compose/completion.go index 8ad9361..0784533 100644 --- a/internal/compose/completion.go +++ b/internal/compose/completion.go @@ -206,6 +206,9 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager character := int(params.Position.Character) + 1 path := constructCompletionNodePath(file, line) if len(path) == 0 { + if topLevelNodeOffset != -1 && params.Position.Character != uint32(topLevelNodeOffset) { + return nil, nil + } return &protocol.CompletionList{Items: createTopLevelItems()}, nil } else if len(path) == 1 { return nil, nil @@ -570,6 +573,8 @@ func NodeStructure(line int, rootNodes []*ast.MappingValueNode) []*ast.MappingVa candidate = node } else if node.GetToken().Position.Line == line { return []*ast.MappingValueNode{node} + } else if candidate == nil { + return []*ast.MappingValueNode{} } else { break } diff --git a/internal/compose/completion_test.go b/internal/compose/completion_test.go index 95765f4..778ff62 100644 --- a/internal/compose/completion_test.go +++ b/internal/compose/completion_test.go @@ -2559,6 +2559,20 @@ services: character: 0, list: nil, }, + { + name: "completion is on a line before the first node and after it positionally", + content: " \nservices:", + line: 0, + character: 1, + list: nil, + }, + { + name: "completion is on a line before the first node and before it positionally", + content: " \n services:", + line: 0, + character: 1, + list: nil, + }, } composeFileURI := fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(os.TempDir(), "compose.yaml")), "/"))