|
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 replace backslashes with forward slashes. |
| 10 | + // Tripleslash is a shorthand for `file://localhost/C:/Windows` with the `localhost` omitted. |
| 11 | + // We encode the driver Letter `C:` as well. LSP Specification allows it. |
| 12 | + // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#uri |
| 13 | + format!( |
| 14 | + "file:///{}", |
| 15 | + percent_encoding::utf8_percent_encode( |
| 16 | + &path.to_string_lossy().cow_replace('\\', "/"), |
| 17 | + &ASCII_SET |
| 18 | + ) |
| 19 | + ) |
| 20 | + } else { |
| 21 | + // For Unix-like systems, just convert to a file URI directly |
| 22 | + format!( |
| 23 | + "file://{}", |
| 24 | + percent_encoding::utf8_percent_encode(&path.to_string_lossy(), &ASCII_SET) |
| 25 | + ) |
| 26 | + }; |
| 27 | + Uri::from_str(&path).expect("Failed to create URI from path") |
9 | 28 | }
|
10 | 29 |
|
11 |
| -const ASCII_SET: AsciiSet = percent_encoding::NON_ALPHANUMERIC.remove(b'.'); |
| 30 | +const ASCII_SET: AsciiSet = |
| 31 | + // RFC3986 allows only alphanumeric characters, `-`, `.`, `_`, and `~` in the path. |
| 32 | + percent_encoding::NON_ALPHANUMERIC |
| 33 | + .remove(b'-') |
| 34 | + .remove(b'.') |
| 35 | + .remove(b'_') |
| 36 | + .remove(b'~') |
| 37 | + // we do not want path separators to be percent-encoded |
| 38 | + .remove(b'/'); |
12 | 39 |
|
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 | 40 | #[cfg(test)]
|
40 | 41 | mod test {
|
41 | 42 | use std::path::PathBuf;
|
42 | 43 |
|
43 | 44 | use crate::uri_ext::path_to_uri;
|
44 | 45 |
|
| 46 | + const EXPECTED_SCHEMA: &str = if cfg!(target_os = "windows") { "file:///" } else { "file://" }; |
| 47 | + |
| 48 | + fn with_schema(path: &str) -> String { |
| 49 | + format!("{EXPECTED_SCHEMA}{path}") |
| 50 | + } |
45 | 51 |
|
46 | 52 | #[test]
|
47 | 53 | fn test_path_to_uri() {
|
48 | 54 | let path = PathBuf::from("/some/path/to/file.txt");
|
49 | 55 | let uri = path_to_uri(&path);
|
50 |
| - assert_eq!(uri.to_string(), "file:///some/path/to/file.txt"); |
| 56 | + assert_eq!(uri.to_string(), with_schema("/some/path/to/file.txt")); |
51 | 57 | }
|
52 | 58 |
|
53 | 59 | #[test]
|
54 | 60 | fn test_path_to_uri_with_spaces() {
|
55 | 61 | let path = PathBuf::from("/some/path/to/file with spaces.txt");
|
56 | 62 | let uri = path_to_uri(&path);
|
57 |
| - assert_eq!(uri.to_string(), "file:///some/path/to/file%20with%20spaces.txt"); |
| 63 | + assert_eq!(uri.to_string(), with_schema("/some/path/to/file%20with%20spaces.txt")); |
58 | 64 | }
|
59 | 65 |
|
60 | 66 | #[test]
|
61 |
| - |
62 | 67 | fn test_path_to_uri_with_special_characters() {
|
63 | 68 | let path = PathBuf::from("/some/path/[[...rest]]/file.txt");
|
64 | 69 | let uri = path_to_uri(&path);
|
65 |
| - assert_eq!(uri.to_string(), "file:///some/path/%5B%5B...rest%5D%5D/file.txt"); |
| 70 | + assert_eq!(uri.to_string(), with_schema("/some/path/%5B%5B...rest%5D%5D/file.txt")); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_path_to_uri_non_ascii() { |
| 75 | + let path = PathBuf::from("/some/path/to/файл.txt"); |
| 76 | + let uri = path_to_uri(&path); |
| 77 | + assert_eq!(uri.to_string(), with_schema("/some/path/to/%D1%84%D0%B0%D0%B9%D0%BB.txt")); |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn test_path_to_uri_with_unicode() { |
| 82 | + let path = PathBuf::from("/some/path/to/文件.txt"); |
| 83 | + let uri = path_to_uri(&path); |
| 84 | + assert_eq!(uri.to_string(), with_schema("/some/path/to/%E6%96%87%E4%BB%B6.txt")); |
| 85 | + } |
| 86 | + |
| 87 | + #[cfg(all(test, target_os = "windows"))] |
| 88 | + #[test] |
| 89 | + fn test_path_to_uri_windows() { |
| 90 | + let path = PathBuf::from("C:\\some\\path\\to\\file.txt"); |
| 91 | + let uri = path_to_uri(&path); |
| 92 | + // yes we encode `:` too, LSP allows it |
| 93 | + // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#uri |
| 94 | + assert_eq!(uri.to_string(), with_schema("C%3A/some/path/to/file.txt")); |
66 | 95 | }
|
67 | 96 | }
|
0 commit comments