aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src
diff options
context:
space:
mode:
authorEmil Lauridsen <[email protected]>2019-12-29 17:27:14 +0000
committerEmil Lauridsen <[email protected]>2019-12-29 17:27:14 +0000
commit0d69d23d2f9fc23f53960eca60cdaea1a7e2281f (patch)
treebad6309a987281855dcf0d1be073f7cc084926bd /crates/ra_lsp_server/src
parent523b4cbc602447b14202dd2520f84241bb07c4e2 (diff)
Lowercase drive letters when getting paths from cargo check
Diffstat (limited to 'crates/ra_lsp_server/src')
-rw-r--r--crates/ra_lsp_server/src/world.rs64
1 files changed, 3 insertions, 61 deletions
diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs
index 4b3959e38..121ddfd1f 100644
--- a/crates/ra_lsp_server/src/world.rs
+++ b/crates/ra_lsp_server/src/world.rs
@@ -12,7 +12,9 @@ use crossbeam_channel::{unbounded, Receiver};
12use lsp_server::ErrorCode; 12use lsp_server::ErrorCode;
13use lsp_types::Url; 13use lsp_types::Url;
14use parking_lot::RwLock; 14use parking_lot::RwLock;
15use ra_cargo_watch::{CheckOptions, CheckWatcher, CheckWatcherSharedState}; 15use ra_cargo_watch::{
16 url_from_path_with_drive_lowercasing, CheckOptions, CheckWatcher, CheckWatcherSharedState,
17};
16use ra_ide::{ 18use ra_ide::{
17 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FeatureFlags, FileId, LibraryData, 19 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FeatureFlags, FileId, LibraryData,
18 SourceRootId, 20 SourceRootId,
@@ -21,13 +23,11 @@ use ra_project_model::{get_rustc_cfg_options, ProjectWorkspace};
21use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; 23use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
22use ra_vfs_glob::{Glob, RustPackageFilterBuilder}; 24use ra_vfs_glob::{Glob, RustPackageFilterBuilder};
23use relative_path::RelativePathBuf; 25use relative_path::RelativePathBuf;
24use std::path::{Component, Prefix};
25 26
26use crate::{ 27use crate::{
27 main_loop::pending_requests::{CompletedRequest, LatestRequests}, 28 main_loop::pending_requests::{CompletedRequest, LatestRequests},
28 LspError, Result, 29 LspError, Result,
29}; 30};
30use std::str::FromStr;
31 31
32#[derive(Debug, Clone)] 32#[derive(Debug, Clone)]
33pub struct Options { 33pub struct Options {
@@ -294,61 +294,3 @@ impl WorldSnapshot {
294 self.analysis.feature_flags() 294 self.analysis.feature_flags()
295 } 295 }
296} 296}
297
298/// Returns a `Url` object from a given path, will lowercase drive letters if present.
299/// This will only happen when processing windows paths.
300///
301/// When processing non-windows path, this is essentially the same as `Url::from_file_path`.
302fn url_from_path_with_drive_lowercasing(path: impl AsRef<Path>) -> Result<Url> {
303 let component_has_windows_drive = path.as_ref().components().any(|comp| {
304 if let Component::Prefix(c) = comp {
305 match c.kind() {
306 Prefix::Disk(_) | Prefix::VerbatimDisk(_) => return true,
307 _ => return false,
308 }
309 }
310 false
311 });
312
313 // VSCode expects drive letters to be lowercased, where rust will uppercase the drive letters.
314 if component_has_windows_drive {
315 let url_original = Url::from_file_path(&path)
316 .map_err(|_| format!("can't convert path to url: {}", path.as_ref().display()))?;
317
318 let drive_partition: Vec<&str> = url_original.as_str().rsplitn(2, ':').collect();
319
320 // There is a drive partition, but we never found a colon.
321 // This should not happen, but in this case we just pass it through.
322 if drive_partition.len() == 1 {
323 return Ok(url_original);
324 }
325
326 let joined = drive_partition[1].to_ascii_lowercase() + ":" + drive_partition[0];
327 let url = Url::from_str(&joined).expect("This came from a valid `Url`");
328
329 Ok(url)
330 } else {
331 Ok(Url::from_file_path(&path)
332 .map_err(|_| format!("can't convert path to url: {}", path.as_ref().display()))?)
333 }
334}
335
336// `Url` is not able to parse windows paths on unix machines.
337#[cfg(target_os = "windows")]
338#[cfg(test)]
339mod path_conversion_windows_tests {
340 use super::url_from_path_with_drive_lowercasing;
341 #[test]
342 fn test_lowercase_drive_letter_with_drive() {
343 let url = url_from_path_with_drive_lowercasing("C:\\Test").unwrap();
344
345 assert_eq!(url.to_string(), "file:///c:/Test");
346 }
347
348 #[test]
349 fn test_drive_without_colon_passthrough() {
350 let url = url_from_path_with_drive_lowercasing(r#"\\localhost\C$\my_dir"#).unwrap();
351
352 assert_eq!(url.to_string(), "file://localhost/C$/my_dir");
353 }
354}