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
27 changes: 8 additions & 19 deletions internal/lsp/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this? 🙂 Seems like CutPrefix answers the same question in one function call instead of two, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that makes sense. I'll keep that in there!

// 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
Expand Down
4 changes: 4 additions & 0 deletions internal/lsp/uri/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Empty file removed log.txt
Empty file.