Skip to content

Commit eeff03d

Browse files
authored
Merge pull request #211 from docker/compose-completion-line-number-fix
Fix build target completion crash from line number differences
2 parents fa04add + 25516aa commit eeff03d

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ All notable changes to the Docker Language Server will be documented in this fil
2727
- Compose
2828
- textDocument/completion
2929
- fix panic in code completion in an empty file ([#196](https://github.com/docker/docker-language-server/issues/196))
30+
- fix line number assumption issues when using code completion for build targets ([#210](https://github.com/docker/docker-language-server/issues/210))
3031
- textDocument/hover
3132
- ensure results are returned even if the file has CRLFs ([#205](https://github.com/docker/docker-language-server/issues/205))
3233
- Bake

internal/compose/completion.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,10 @@ func buildTargetCompletionItems(params *protocol.CompletionParams, manager *docu
360360
if _, ok := path[3].Value.(*ast.NullNode); ok {
361361
return createBuildStageItems(params, manager, dockerfilePath, "", prefixLength), true
362362
} else if prefix, ok := path[3].Value.(*ast.StringNode); ok {
363-
offset := int(params.Position.Character) - path[3].Value.GetToken().Position.Column + 1
364-
return createBuildStageItems(params, manager, dockerfilePath, prefix.Value[0:offset], prefixLength), true
363+
if int(params.Position.Line) == path[3].Value.GetToken().Position.Line-1 {
364+
offset := int(params.Position.Character) - path[3].Value.GetToken().Position.Column + 1
365+
return createBuildStageItems(params, manager, dockerfilePath, prefix.Value[0:offset], prefixLength), true
366+
}
365367
}
366368
}
367369
}

internal/compose/completion_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,6 +3410,44 @@ services:
34103410
}
34113411
},
34123412
},
3413+
{
3414+
name: "completion on line different from the target attribute",
3415+
dockerfileContent: "FROM alpine as astage",
3416+
content: `
3417+
services:
3418+
postgres:
3419+
build:
3420+
target:
3421+
`,
3422+
line: 5,
3423+
character: 8,
3424+
list: func() *protocol.CompletionList {
3425+
return &protocol.CompletionList{
3426+
Items: []protocol.CompletionItem{
3427+
{
3428+
Label: "astage",
3429+
Documentation: "alpine",
3430+
TextEdit: textEdit("astage", 5, 8, 0),
3431+
},
3432+
},
3433+
}
3434+
},
3435+
},
3436+
{
3437+
name: "completion on a different line from target that already has content",
3438+
dockerfileContent: "FROM scratch AS stage",
3439+
content: `
3440+
services:
3441+
postgres:
3442+
build:
3443+
target: ab
3444+
`,
3445+
line: 5,
3446+
character: 8,
3447+
list: func() *protocol.CompletionList {
3448+
return nil
3449+
},
3450+
},
34133451
{
34143452
name: "no build stages suggested if dockerfile_inline used",
34153453
dockerfileContent: "FROM scratch AS base",

0 commit comments

Comments
 (0)