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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ All notable changes to the Docker Language Server will be documented in this fil
- improve handling of malformed image attribute values with registry prefixes ([#369](https://github.com/docker/docker-language-server/issues/369))
- convert links properly if a WSL URI with a dollar sign is used ([#366](https://github.com/docker/docker-language-server/issues/366))
- Bake
- textDocument/codeLens
- refactor the URI handling code so it will accept a WSL URI with a dollar sign ([#388](https://github.com/docker/docker-language-server/issues/388))
- textDocument/documentLink
- convert links properly if a WSL URI with a dollar sign is used ([#378](https://github.com/docker/docker-language-server/issues/378))
- textDocument/inlineCompletion
Expand Down
31 changes: 15 additions & 16 deletions internal/bake/hcl/codeLens.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,52 @@ import (
"context"
"errors"
"fmt"
"net/url"
"path/filepath"

"github.com/docker/docker-language-server/internal/pkg/document"
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
"github.com/docker/docker-language-server/internal/types"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func CodeLens(ctx context.Context, filename string, doc document.BakeHCLDocument) ([]protocol.CodeLens, error) {
func CodeLens(ctx context.Context, documentURI string, doc document.BakeHCLDocument) ([]protocol.CodeLens, error) {
body, ok := doc.File().Body.(*hclsyntax.Body)
if !ok {
return nil, errors.New("unrecognized body in HCL document")
}

url, err := url.Parse(filename)
dp, err := doc.DocumentPath()
if err != nil {
return nil, fmt.Errorf("could not parse URI (%v): %w", filename, err)
return nil, fmt.Errorf("could not parse URI (%v): %w", documentURI, err)
}

filename = url.Path
_, cwd := types.Concatenate(dp.Folder, ".", dp.WSLDollarSignHost)
result := []protocol.CodeLens{}
for _, block := range body.Blocks {
if len(block.Labels) > 0 {
if block.Type == "group" {
switch block.Type {
case "group":
rng := protocol.Range{
Start: protocol.Position{Line: uint32(block.Range().Start.Line - 1)},
End: protocol.Position{Line: uint32(block.Range().Start.Line - 1)},
}
result = append(result, createCodeLens("Build", filename, "build", block.Labels[0], rng))
result = append(result, createCodeLens("Check", filename, "check", block.Labels[0], rng))
result = append(result, createCodeLens("Print", filename, "print", block.Labels[0], rng))
} else if block.Type == "target" {
result = append(result, createCodeLens("Build", cwd, "build", block.Labels[0], rng))
result = append(result, createCodeLens("Check", cwd, "check", block.Labels[0], rng))
result = append(result, createCodeLens("Print", cwd, "print", block.Labels[0], rng))
case "target":
rng := protocol.Range{
Start: protocol.Position{Line: uint32(block.Range().Start.Line - 1)},
End: protocol.Position{Line: uint32(block.Range().Start.Line - 1)},
}
result = append(result, createCodeLens("Build", filename, "build", block.Labels[0], rng))
result = append(result, createCodeLens("Check", filename, "check", block.Labels[0], rng))
result = append(result, createCodeLens("Print", filename, "print", block.Labels[0], rng))
result = append(result, createCodeLens("Build", cwd, "build", block.Labels[0], rng))
result = append(result, createCodeLens("Check", cwd, "check", block.Labels[0], rng))
result = append(result, createCodeLens("Print", cwd, "print", block.Labels[0], rng))
}
}
}
return result, nil
}

func createCodeLens(title, filename, call, target string, rng protocol.Range) protocol.CodeLens {
func createCodeLens(title, cwd, call, target string, rng protocol.Range) protocol.CodeLens {
return protocol.CodeLens{
Range: rng,
Command: &protocol.Command{
Expand All @@ -60,7 +59,7 @@ func createCodeLens(title, filename, call, target string, rng protocol.Range) pr
map[string]string{
"call": call,
"target": target,
"cwd": filepath.Dir(types.StripLeadingSlash(filename)),
"cwd": cwd,
},
},
},
Expand Down
7 changes: 7 additions & 0 deletions internal/bake/hcl/codeLens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ func TestCodeLens(t *testing.T) {
testsFolder := filepath.Join(os.TempDir(), "codeLensTests")
bakeFilePath := filepath.Join(testsFolder, "docker-bake.hcl")
uriString := fmt.Sprintf("file:///%v", filepath.ToSlash(bakeFilePath))
testCodeLens(t, testsFolder, uriString)
}

func TestCodeLens_WSL(t *testing.T) {
testCodeLens(t, "\\\\wsl$\\docker-desktop\\tmp", "file://wsl%24/docker-desktop/tmp/docker-bake.hcl")
}

func testCodeLens(t *testing.T, testsFolder, uriString string) {
testCases := []struct {
name string
content string
Expand Down
Loading