From 656c25409a95f5ef88a408a24cde35e2af2c8dab Mon Sep 17 00:00:00 2001 From: Charlie Egan Date: Tue, 5 Aug 2025 13:56:22 +0100 Subject: [PATCH] lsp/uri: Refactor URI handling This had been botched some time back and can be made simpler. Signed-off-by: Charlie Egan --- internal/lsp/uri/uri.go | 27 ++++++++------------------- internal/lsp/uri/uri_test.go | 4 ++++ log.txt | 0 3 files changed, 12 insertions(+), 19 deletions(-) delete mode 100644 log.txt diff --git a/internal/lsp/uri/uri.go b/internal/lsp/uri/uri.go index e22a0d02..8c12936b 100644 --- a/internal/lsp/uri/uri.go +++ b/internal/lsp/uri/uri.go @@ -9,10 +9,7 @@ import ( "github.com/styrainc/regal/internal/lsp/clients" ) -var ( - drivePattern = regexp.MustCompile(`^([A-Za-z]):`) - drivePatternMaybeEncoded = regexp.MustCompile(`^([A-Za-z])(%3[aA]|:)`) -) +var drivePattern = regexp.MustCompile(`^\/?([A-Za-z]):`) // FromPath converts a file path to a URI for a given client. // Since clients expect URIs to be in a specific format, this function @@ -55,28 +52,20 @@ func FromPath(client clients.Identifier, path string) string { // Some clients represent URIs differently, and so this function exists to convert // client URIs into a standard file paths. func ToPath(client clients.Identifier, uri string) string { - // if the uri appears to be a URI with a file prefix, then remove the prefix + // if it looks like uri was a file URI, then there might be encoded characters in the path path, hadPrefix := strings.CutPrefix(uri, "file://") if hadPrefix { - // if it looks like a URI, then try and decode it - if decodedPath, err := url.QueryUnescape(path); err == nil { + // if it looks like a URI, then try and decode the path + decodedPath, err := url.QueryUnescape(path) + if err == nil { path = decodedPath } } - if client == clients.IdentifierVSCode { + // handling case for windows when the drive letter is set + if client == clients.IdentifierVSCode && + drivePattern.MatchString(path) { path = strings.TrimPrefix(path, "/") - // handling case for windows when the drive letter is set - - // TODO; never set? - var driveLetter string - - if matches := drivePatternMaybeEncoded.FindStringSubmatch(path); len(matches) > 1 { - path = strings.TrimPrefix(path, matches[0]) - path = matches[1] + ":" + strings.TrimPrefix(path, driveLetter) - } else { - path = "/" + path - } } // Convert path to use system separators diff --git a/internal/lsp/uri/uri_test.go b/internal/lsp/uri/uri_test.go index 8d4216a2..a8c48079 100644 --- a/internal/lsp/uri/uri_test.go +++ b/internal/lsp/uri/uri_test.go @@ -139,6 +139,10 @@ func TestURIToPath_VSCode(t *testing.T) { uri: "file:///c%3A/foo/bar", want: filepath.FromSlash("c:/foo/bar"), }, + "windows encoded uppercase drive": { + uri: "file:///C%3A/foo/bar", + want: filepath.FromSlash("C:/foo/bar"), + }, "unix encoded with space in path": { uri: "file:///Users/foo/bar%20baz", want: filepath.FromSlash("/Users/foo/bar baz"), diff --git a/log.txt b/log.txt deleted file mode 100644 index e69de29b..00000000