From 324cbe839f3110bd4d51726d5a7afe29808ade02 Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Sun, 15 Dec 2019 16:51:57 +0200 Subject: Lowercase drive letters on windows before sending to extension. --- crates/ra_lsp_server/src/world.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'crates/ra_lsp_server/src/world.rs') diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index 927449b45..be3a5bfb8 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -22,6 +22,7 @@ use crate::{ main_loop::pending_requests::{CompletedRequest, LatestRequests}, LspError, Result, }; +use std::str::FromStr; #[derive(Debug, Clone)] pub struct Options { @@ -235,6 +236,9 @@ impl WorldSnapshot { let path = self.vfs.read().file2path(VfsFile(id.0)); let url = Url::from_file_path(&path) .map_err(|_| format!("can't convert path to url: {}", path.display()))?; + + #[cfg(target_os = "windows")] + let url = lowercase_drive_letter(&url); Ok(url) } @@ -279,3 +283,33 @@ impl WorldSnapshot { self.analysis.feature_flags() } } + +#[cfg(target_os = "windows")] +fn lowercase_drive_letter(url: &Url) -> Url { + let s = url.to_string(); + let drive_partition: Vec<&str> = s.rsplitn(2, ':').collect::>(); + + if drive_partition.len() == 1 { + return url.clone(); + } + + let joined = drive_partition[1].to_ascii_lowercase() + ":" + drive_partition[0]; + let url = Url::from_str(&joined).expect("This came from a valid `Url`"); + url +} + +#[test] +fn test_lowercase_drive_letter_with_drive() { + let url = Url::from_file_path("C:\\Test").unwrap(); + let url = lowercase_drive_letter(&url); + + assert_eq!(url.to_string(), "file:///c:/Test"); +} + +#[test] +fn test_drive_without_colon_passthrough() { + let url = Url::from_file_path(r#"\\localhost\C$\my_dir"#).expect("Should work"); + let url = lowercase_drive_letter(&url); + + assert_eq!(url.to_string(), "file:///C$/my_dir"); +} -- cgit v1.2.3