Skip to content

Commit 2ce0659

Browse files
autofix-ci[bot]Sysix
authored andcommitted
[autofix.ci] apply automated fixes
1 parent f6e2c1a commit 2ce0659

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_language_server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ oxc_diagnostics = { workspace = true }
2727
oxc_linter = { workspace = true, features = ["language_server"] }
2828

2929
#
30+
cow-utils = { workspace = true }
3031
env_logger = { workspace = true, features = ["humantime"] }
3132
futures = { workspace = true }
3233
globset = { workspace = true }

crates/oxc_language_server/src/uri_ext.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
use std::{path::{Path, PathBuf}, str::FromStr};
1+
use std::{
2+
path::{Path, PathBuf},
3+
str::FromStr,
4+
};
25

6+
use cow_utils::CowUtils;
37
use percent_encoding::AsciiSet;
48
use tower_lsp_server::lsp_types::Uri;
59

610
fn path_to_uri(path: &PathBuf) -> Uri {
711
let path_str = normalize_path_with_utf8_percent_encode(path);
8-
Uri::from_str(&format!("file://{}", path_str.to_string_lossy())).expect("Failed to create URI from path")
12+
let path = if cfg!(target_os = "windows") {
13+
// On Windows, we need to convert the path to a URI format
14+
// that includes replaces ckslashes with forward slashes.
15+
// Tripleslash is a shorthand for `file://localhost/C:/Windows` with the `localhost` omitted
16+
format!("file:///{}", path_str.to_string_lossy().cow_replace('\\', "/"))
17+
} else {
18+
// For Unix-like systems, just convert to a file URI directly
19+
format!("file://{}", path_str.to_string_lossy())
20+
};
21+
Uri::from_str(&path).expect("Failed to create URI from path")
922
}
1023

11-
const ASCII_SET: AsciiSet = percent_encoding::NON_ALPHANUMERIC.remove(b'.');
24+
const ASCII_SET: AsciiSet =
25+
percent_encoding::NON_ALPHANUMERIC.remove(b'-').remove(b'.').remove(b'_').remove(b'~');
1226

1327
/// Normalize a path by removing `.` and resolving `..` components,
1428
/// without touching the filesystem.
@@ -20,15 +34,24 @@ pub fn normalize_path_with_utf8_percent_encode<P: AsRef<Path>>(path: P) -> PathB
2034
match component {
2135
std::path::Component::Prefix(_) => {
2236
// Keep the prefix (e.g., drive letter on Windows)
23-
result.push(component.as_os_str());
37+
result.push(
38+
percent_encoding::utf8_percent_encode(
39+
component.as_os_str().to_str().unwrap(),
40+
&ASCII_SET,
41+
)
42+
.to_string(),
43+
);
2444
}
2545
std::path::Component::RootDir => {
2646
// Keep the root directory
2747
result.push(component.as_os_str());
2848
}
2949
std::path::Component::Normal(part) => {
3050
// Normal components are added to the path
31-
result.push(percent_encoding::utf8_percent_encode(&part.to_str().unwrap(), &ASCII_SET).to_string());
51+
result.push(
52+
percent_encoding::utf8_percent_encode(part.to_str().unwrap(), &ASCII_SET)
53+
.to_string(),
54+
);
3255
}
3356
_ => {}
3457
}
@@ -42,26 +65,45 @@ mod test {
4265

4366
use crate::uri_ext::path_to_uri;
4467

68+
const EXPECTED_SCHEMA: &str = if cfg!(target_os = "windows") { "file:///" } else { "file://" };
69+
70+
fn with_schema(path: &str) -> String {
71+
format!("{EXPECTED_SCHEMA}{path}")
72+
}
4573

4674
#[test]
4775
fn test_path_to_uri() {
4876
let path = PathBuf::from("/some/path/to/file.txt");
4977
let uri = path_to_uri(&path);
50-
assert_eq!(uri.to_string(), "file:///some/path/to/file.txt");
78+
assert_eq!(uri.to_string(), with_schema("/some/path/to/file.txt"));
5179
}
5280

5381
#[test]
5482
fn test_path_to_uri_with_spaces() {
5583
let path = PathBuf::from("/some/path/to/file with spaces.txt");
5684
let uri = path_to_uri(&path);
57-
assert_eq!(uri.to_string(), "file:///some/path/to/file%20with%20spaces.txt");
85+
assert_eq!(uri.to_string(), with_schema("/some/path/to/file%20with%20spaces.txt"));
5886
}
5987

6088
#[test]
61-
6289
fn test_path_to_uri_with_special_characters() {
6390
let path = PathBuf::from("/some/path/[[...rest]]/file.txt");
6491
let uri = path_to_uri(&path);
65-
assert_eq!(uri.to_string(), "file:///some/path/%5B%5B...rest%5D%5D/file.txt");
92+
assert_eq!(uri.to_string(), with_schema("/some/path/%5B%5B...rest%5D%5D/file.txt"));
93+
}
94+
95+
#[test]
96+
fn test_path_to_uri_non_ascii() {
97+
let path = PathBuf::from("/some/path/to/файл.txt");
98+
let uri = path_to_uri(&path);
99+
assert_eq!(uri.to_string(), with_schema("/some/path/to/%D1%84%D0%B0%D0%B9%D0%BB.txt"));
100+
}
101+
102+
#[cfg(all(test, target_os = "windows"))]
103+
#[test]
104+
fn test_path_to_uri_windows() {
105+
let path = PathBuf::from("C:\\some\\path\\to\\file.txt");
106+
let uri = path_to_uri(&path);
107+
assert_eq!(uri.to_string(), with_schema("c%3A/some/path/to/file.txt"));
66108
}
67109
}

0 commit comments

Comments
 (0)