Skip to content

Commit 15ae9a8

Browse files
author
Charlie Egan
authored
lsp/uri: Refactor URI handling (#1644)
This had been botched some time back and can be made simpler. Signed-off-by: Charlie Egan <charlie@styra.com>
1 parent 65351f9 commit 15ae9a8

3 files changed

Lines changed: 12 additions & 19 deletions

File tree

internal/lsp/uri/uri.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import (
99
"github.com/styrainc/regal/internal/lsp/clients"
1010
)
1111

12-
var (
13-
drivePattern = regexp.MustCompile(`^([A-Za-z]):`)
14-
drivePatternMaybeEncoded = regexp.MustCompile(`^([A-Za-z])(%3[aA]|:)`)
15-
)
12+
var drivePattern = regexp.MustCompile(`^\/?([A-Za-z]):`)
1613

1714
// FromPath converts a file path to a URI for a given client.
1815
// Since clients expect URIs to be in a specific format, this function
@@ -55,28 +52,20 @@ func FromPath(client clients.Identifier, path string) string {
5552
// Some clients represent URIs differently, and so this function exists to convert
5653
// client URIs into a standard file paths.
5754
func ToPath(client clients.Identifier, uri string) string {
58-
// if the uri appears to be a URI with a file prefix, then remove the prefix
55+
// if it looks like uri was a file URI, then there might be encoded characters in the path
5956
path, hadPrefix := strings.CutPrefix(uri, "file://")
6057
if hadPrefix {
61-
// if it looks like a URI, then try and decode it
62-
if decodedPath, err := url.QueryUnescape(path); err == nil {
58+
// if it looks like a URI, then try and decode the path
59+
decodedPath, err := url.QueryUnescape(path)
60+
if err == nil {
6361
path = decodedPath
6462
}
6563
}
6664

67-
if client == clients.IdentifierVSCode {
65+
// handling case for windows when the drive letter is set
66+
if client == clients.IdentifierVSCode &&
67+
drivePattern.MatchString(path) {
6868
path = strings.TrimPrefix(path, "/")
69-
// handling case for windows when the drive letter is set
70-
71-
// TODO; never set?
72-
var driveLetter string
73-
74-
if matches := drivePatternMaybeEncoded.FindStringSubmatch(path); len(matches) > 1 {
75-
path = strings.TrimPrefix(path, matches[0])
76-
path = matches[1] + ":" + strings.TrimPrefix(path, driveLetter)
77-
} else {
78-
path = "/" + path
79-
}
8069
}
8170

8271
// Convert path to use system separators

internal/lsp/uri/uri_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ func TestURIToPath_VSCode(t *testing.T) {
139139
uri: "file:///c%3A/foo/bar",
140140
want: filepath.FromSlash("c:/foo/bar"),
141141
},
142+
"windows encoded uppercase drive": {
143+
uri: "file:///C%3A/foo/bar",
144+
want: filepath.FromSlash("C:/foo/bar"),
145+
},
142146
"unix encoded with space in path": {
143147
uri: "file:///Users/foo/bar%20baz",
144148
want: filepath.FromSlash("/Users/foo/bar baz"),

log.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)