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 ++++++++++++++++++++++ .../code/src/notifications/publish_decorations.ts | 3 +- 2 files changed, 35 insertions(+), 2 deletions(-) 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"); +} diff --git a/editors/code/src/notifications/publish_decorations.ts b/editors/code/src/notifications/publish_decorations.ts index 120eabbc6..4441e2b28 100644 --- a/editors/code/src/notifications/publish_decorations.ts +++ b/editors/code/src/notifications/publish_decorations.ts @@ -15,8 +15,7 @@ export function handle(params: PublishDecorationsParams) { // Unescaped URI should be something like: // file:///c:/Workspace/ra-test/src/main.rs // RA server might send it with the drive letter uppercased, so we force only the drive letter to lowercase. - const uriWithLowercasedDrive = params.uri.substr(0, 8) + params.uri[8].toLowerCase() + params.uri.substr(9); - return unescapedUri === uriWithLowercasedDrive + return unescapedUri === params.uri } ); -- cgit v1.2.3