|
1 |
| -use std::{path::{Path, PathBuf}, str::FromStr}; |
| 1 | +use std::{path::Path, str::FromStr}; |
2 | 2 |
|
| 3 | +use cow_utils::CowUtils; |
3 | 4 | use percent_encoding::AsciiSet;
|
4 | 5 | use tower_lsp_server::lsp_types::Uri;
|
5 | 6 |
|
6 |
| -fn path_to_uri(path: &PathBuf) -> Uri { |
7 |
| - 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") |
| 7 | +pub fn path_to_uri(path: &Path) -> Uri { |
| 8 | + let path = if cfg!(target_os = "windows") { |
| 9 | + // On Windows, we need to convert the path to a URI format |
| 10 | + // that includes replaces ckslashes with forward slashes. |
| 11 | + // Tripleslash is a shorthand for `file://localhost/C:/Windows` with the `localhost` omitted |
| 12 | + format!( |
| 13 | + "file:///{}", |
| 14 | + percent_encoding::utf8_percent_encode( |
| 15 | + &path.to_string_lossy().cow_replace('\\', "/"), |
| 16 | + &ASCII_SET |
| 17 | + ) |
| 18 | + ) |
| 19 | + } else { |
| 20 | + // For Unix-like systems, just convert to a file URI directly |
| 21 | + format!( |
| 22 | + "file://{}", |
| 23 | + percent_encoding::utf8_percent_encode(&path.to_string_lossy(), &ASCII_SET) |
| 24 | + ) |
| 25 | + }; |
| 26 | + Uri::from_str(&path).expect("Failed to create URI from path") |
9 | 27 | }
|
10 | 28 |
|
11 |
| -const ASCII_SET: AsciiSet = percent_encoding::NON_ALPHANUMERIC.remove(b'.'); |
| 29 | +const ASCII_SET: AsciiSet = |
| 30 | + // RFC396 allow only alphanumeric characters, `-`, `.`, `_`, and `~` in the path. |
| 31 | + percent_encoding::NON_ALPHANUMERIC |
| 32 | + .remove(b'-') |
| 33 | + .remove(b'.') |
| 34 | + .remove(b'_') |
| 35 | + .remove(b'~') |
| 36 | + // we do not want path separators to be percent-encoded |
| 37 | + .remove(b'/'); |
12 | 38 |
|
13 |
| -/// Normalize a path by removing `.` and resolving `..` components, |
14 |
| -/// without touching the filesystem. |
15 |
| -pub fn normalize_path_with_utf8_percent_encode<P: AsRef<Path>>(path: P) -> PathBuf { |
16 |
| - let mut result = PathBuf::new(); |
17 |
| - let components = path.as_ref().components(); |
18 |
| - |
19 |
| - for component in components { |
20 |
| - match component { |
21 |
| - std::path::Component::Prefix(_) => { |
22 |
| - // Keep the prefix (e.g., drive letter on Windows) |
23 |
| - result.push(component.as_os_str()); |
24 |
| - } |
25 |
| - std::path::Component::RootDir => { |
26 |
| - // Keep the root directory |
27 |
| - result.push(component.as_os_str()); |
28 |
| - } |
29 |
| - std::path::Component::Normal(part) => { |
30 |
| - // Normal components are added to the path |
31 |
| - result.push(percent_encoding::utf8_percent_encode(&part.to_str().unwrap(), &ASCII_SET).to_string()); |
32 |
| - } |
33 |
| - _ => {} |
34 |
| - } |
35 |
| - } |
36 |
| - |
37 |
| - result |
38 |
| -} |
39 | 39 | #[cfg(test)]
|
40 | 40 | mod test {
|
41 | 41 | use std::path::PathBuf;
|
42 | 42 |
|
43 | 43 | use crate::uri_ext::path_to_uri;
|
44 | 44 |
|
| 45 | + const EXPECTED_SCHEMA: &str = if cfg!(target_os = "windows") { "file:///" } else { "file://" }; |
| 46 | + |
| 47 | + fn with_schema(path: &str) -> String { |
| 48 | + format!("{EXPECTED_SCHEMA}{path}") |
| 49 | + } |
45 | 50 |
|
46 | 51 | #[test]
|
47 | 52 | fn test_path_to_uri() {
|
48 | 53 | let path = PathBuf::from("/some/path/to/file.txt");
|
49 | 54 | let uri = path_to_uri(&path);
|
50 |
| - assert_eq!(uri.to_string(), "file:///some/path/to/file.txt"); |
| 55 | + assert_eq!(uri.to_string(), with_schema("/some/path/to/file.txt")); |
51 | 56 | }
|
52 | 57 |
|
53 | 58 | #[test]
|
54 | 59 | fn test_path_to_uri_with_spaces() {
|
55 | 60 | let path = PathBuf::from("/some/path/to/file with spaces.txt");
|
56 | 61 | let uri = path_to_uri(&path);
|
57 |
| - assert_eq!(uri.to_string(), "file:///some/path/to/file%20with%20spaces.txt"); |
| 62 | + assert_eq!(uri.to_string(), with_schema("/some/path/to/file%20with%20spaces.txt")); |
58 | 63 | }
|
59 | 64 |
|
60 | 65 | #[test]
|
61 |
| - |
62 | 66 | fn test_path_to_uri_with_special_characters() {
|
63 | 67 | let path = PathBuf::from("/some/path/[[...rest]]/file.txt");
|
64 | 68 | let uri = path_to_uri(&path);
|
65 |
| - assert_eq!(uri.to_string(), "file:///some/path/%5B%5B...rest%5D%5D/file.txt"); |
| 69 | + assert_eq!(uri.to_string(), with_schema("/some/path/%5B%5B...rest%5D%5D/file.txt")); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_path_to_uri_non_ascii() { |
| 74 | + let path = PathBuf::from("/some/path/to/файл.txt"); |
| 75 | + let uri = path_to_uri(&path); |
| 76 | + assert_eq!(uri.to_string(), with_schema("/some/path/to/%D1%84%D0%B0%D0%B9%D0%BB.txt")); |
| 77 | + } |
| 78 | + |
| 79 | + #[cfg(all(test, target_os = "windows"))] |
| 80 | + #[test] |
| 81 | + fn test_path_to_uri_windows() { |
| 82 | + let path = PathBuf::from("C:\\some\\path\\to\\file.txt"); |
| 83 | + let uri = path_to_uri(&path); |
| 84 | + assert_eq!(uri.to_string(), with_schema("C%3A/some/path/to/file.txt")); |
66 | 85 | }
|
67 | 86 | }
|
0 commit comments