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/completion
- provide local file and folder name suggestions ([#414](https://github.com/docker/docker-language-server/issues/414))
- `context` attribute in a `target` block
- `contexts` attribute in a `target` block
- `dockerfile` attribute in a `target` block

### Fixed
Expand Down
21 changes: 16 additions & 5 deletions internal/bake/hcl/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,22 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
}

if attribute, ok := attributes["dockerfile"]; ok && isInsideRange(attribute.Expr.Range(), params.Position) {
return fileStructureCompletionItems(bakeDocument, attribute, params.Position.Character, false)
return fileStructureCompletionItems(bakeDocument, attribute.Expr, params.Position.Character, false)
}

if attribute, ok := attributes["context"]; ok && isInsideRange(attribute.Expr.Range(), params.Position) {
return fileStructureCompletionItems(bakeDocument, attribute, params.Position.Character, true)
return fileStructureCompletionItems(bakeDocument, attribute.Expr, params.Position.Character, true)
}

if attribute, ok := attributes["contexts"]; ok && isInsideRange(attribute.Expr.Range(), params.Position) {
if expr, ok := attribute.Expr.(*hclsyntax.ObjectConsExpr); ok {
for _, item := range expr.Items {
if isInsideRange(item.ValueExpr.Range(), params.Position) {
return fileStructureCompletionItems(bakeDocument, item.ValueExpr, params.Position.Character, true)
}
}
}
return nil, nil
}

dockerfileURI, dockerfilePath, err := bakeDocument.DockerfileForTarget(b)
Expand Down Expand Up @@ -166,9 +177,9 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
return list, nil
}

func fileStructureCompletionItems(bakeDocument document.BakeHCLDocument, attribute *hclsyntax.Attribute, character protocol.UInteger, hideFiles bool) (*protocol.CompletionList, error) {
if expr, ok := attribute.Expr.(*hclsyntax.TemplateExpr); ok && len(expr.Parts) == 1 {
if literal, ok := expr.Parts[0].(*hclsyntax.LiteralValueExpr); ok {
func fileStructureCompletionItems(bakeDocument document.BakeHCLDocument, expression hclsyntax.Expression, character protocol.UInteger, hideFiles bool) (*protocol.CompletionList, error) {
if templateExpr, ok := expression.(*hclsyntax.TemplateExpr); ok && len(templateExpr.Parts) == 1 {
if literal, ok := templateExpr.Parts[0].(*hclsyntax.LiteralValueExpr); ok {
path, err := bakeDocument.DocumentPath()
if err == nil {
value, _ := literal.Value(&hcl.EvalContext{})
Expand Down
25 changes: 25 additions & 0 deletions internal/bake/hcl/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,31 @@ target t1 {
line: 2,
character: 12,
},
{
name: "contexts attribute in a target block",
content: `
target t1 {
contexts = {
src = "%v"
}
}`,
hideFiles: true,
line: 3,
character: 9,
},
{
name: "contexts attribute in a target block with multiple contexts",
content: `
target t1 {
contexts = {
src = "../path/to/source"
src2 = "%v"
}
}`,
hideFiles: true,
line: 4,
character: 10,
},
}

setups := []struct {
Expand Down
Loading