diff --git a/CHANGELOG.md b/CHANGELOG.md index 7195e45..63097f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/bake/hcl/codeLens.go b/internal/bake/hcl/codeLens.go index 611005a..6d7e85b 100644 --- a/internal/bake/hcl/codeLens.go +++ b/internal/bake/hcl/codeLens.go @@ -4,8 +4,6 @@ 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" @@ -13,44 +11,45 @@ import ( "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{ @@ -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, }, }, }, diff --git a/internal/bake/hcl/codeLens_test.go b/internal/bake/hcl/codeLens_test.go index 1a13d80..8ab1358 100644 --- a/internal/bake/hcl/codeLens_test.go +++ b/internal/bake/hcl/codeLens_test.go @@ -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