-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lsp: Correctly handle URIs and paths on Windows (#569)
* lsp: Correctly handle URIs and paths on Windows Signed-off-by: Charlie Egan <[email protected]> * Appease linter Signed-off-by: Charlie Egan <[email protected]> * Add tests for generic client URI behaviour Signed-off-by: Charlie Egan <[email protected]> --------- Signed-off-by: Charlie Egan <[email protected]>
- Loading branch information
1 parent
f11eef4
commit ece9977
Showing
6 changed files
with
277 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package clients | ||
|
||
// Identifier represent different supported clients and can be used to toggle or change | ||
// server behavior based on the client. | ||
type Identifier int | ||
|
||
const ( | ||
IdentifierGeneric Identifier = iota | ||
IdentifierVSCode | ||
) | ||
|
||
func DetermineClientIdentifier(clientName string) Identifier { | ||
if clientName == "Visual Studio Code" { | ||
return IdentifierVSCode | ||
} | ||
|
||
return IdentifierGeneric | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package uri | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/styrainc/regal/internal/lsp/clients" | ||
) | ||
|
||
// FromPath converts a file path to a URI for a given client. | ||
// Since clients expect URIs to be in a specific format, this function | ||
// will convert the path to the appropriate format for the client. | ||
func FromPath(client clients.Identifier, path string) string { | ||
path = strings.TrimPrefix(path, "file://") | ||
path = strings.TrimPrefix(path, "/") | ||
|
||
if client == clients.IdentifierVSCode { | ||
// Convert Windows path separators to Unix separators | ||
path = filepath.ToSlash(path) | ||
|
||
// If the path is a Windows path, the colon after the drive letter needs to be | ||
// percent-encoded. | ||
if parts := strings.Split(path, ":"); len(parts) > 1 { | ||
path = parts[0] + "%3A" + parts[1] | ||
} | ||
} | ||
|
||
return "file://" + "/" + path | ||
} | ||
|
||
// ToPath converts a URI to a file path from a format for a given client. | ||
// 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 { | ||
path := strings.TrimPrefix(uri, "file://") | ||
|
||
if client == clients.IdentifierVSCode { | ||
if strings.Contains(path, ":") || strings.Contains(path, "%3A") { | ||
path = strings.Replace(path, "%3A", ":", 1) | ||
path = strings.TrimPrefix(path, "/") | ||
} | ||
} | ||
|
||
return path | ||
} |
Oops, something went wrong.