blob: 5a4257efaa067c88d718049f14b242eb6a16339e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
pub mod capabilities;
pub mod chktex;
pub mod cursor;
pub mod diagnostics;
pub mod line_index_ext;
pub mod lsp_enums;
use std::path::PathBuf;
use lsp_types::Url;
use once_cell::sync::Lazy;
pub static HOME_DIR: Lazy<Option<PathBuf>> = Lazy::new(dirs::home_dir);
pub fn normalize_uri(uri: &mut Url) {
if let Some(mut segments) = uri.path_segments() {
if let Some(mut path) = segments.next().and_then(fix_drive_letter) {
for segment in segments {
path.push('/');
path.push_str(segment);
}
uri.set_path(&path);
}
}
uri.set_fragment(None);
}
fn fix_drive_letter(text: &str) -> Option<String> {
if !text.is_ascii() {
return None;
}
match &text[1..] {
":" => Some(text.to_ascii_uppercase()),
"%3A" | "%3a" => Some(format!("{}:", text[0..1].to_ascii_uppercase())),
_ => None,
}
}
#[cfg(test)]
mod tests {
use lsp_types::Url;
use super::normalize_uri;
#[test]
fn test_lowercase_drive_letter() {
let mut uri = Url::parse("file://c:/foo/bar.txt").unwrap();
normalize_uri(&mut uri);
assert_eq!(uri.as_str(), "file:///C:/foo/bar.txt");
}
#[test]
fn test_uppercase_drive_letter() {
let mut uri = Url::parse("file://C:/foo/bar.txt").unwrap();
normalize_uri(&mut uri);
assert_eq!(uri.as_str(), "file:///C:/foo/bar.txt");
}
#[test]
fn test_fragment() {
let mut uri = Url::parse("foo:///bar/baz.txt#qux").unwrap();
normalize_uri(&mut uri);
assert_eq!(uri.as_str(), "foo:///bar/baz.txt");
}
}
|