11//! This module provides the functionality needed to convert diagnostics from
22//! `cargo check` json format to the LSP diagnostic format.
3- use std:: {
4- collections:: HashMap ,
5- path:: { Component , Path , Prefix } ,
6- str:: FromStr ,
7- } ;
3+ use std:: { collections:: HashMap , path:: Path } ;
84
95use lsp_types:: {
106 Diagnostic , DiagnosticRelatedInformation , DiagnosticSeverity , DiagnosticTag , Location ,
@@ -13,7 +9,7 @@ use lsp_types::{
139use ra_flycheck:: { Applicability , DiagnosticLevel , DiagnosticSpan , DiagnosticSpanMacroExpansion } ;
1410use stdx:: format_to;
1511
16- use crate :: { lsp_ext, Result } ;
12+ use crate :: { lsp_ext, to_proto :: url_from_abs_path } ;
1713
1814/// Converts a Rust level string to a LSP severity
1915fn map_level_to_severity ( val : DiagnosticLevel ) -> Option < DiagnosticSeverity > {
@@ -65,7 +61,7 @@ fn map_span_to_location(span: &DiagnosticSpan, workspace_root: &Path) -> Locatio
6561fn map_span_to_location_naive ( span : & DiagnosticSpan , workspace_root : & Path ) -> Location {
6662 let mut file_name = workspace_root. to_path_buf ( ) ;
6763 file_name. push ( & span. file_name ) ;
68- let uri = url_from_path_with_drive_lowercasing ( file_name) . unwrap ( ) ;
64+ let uri = url_from_abs_path ( & file_name) ;
6965
7066 // FIXME: this doesn't handle UTF16 offsets correctly
7167 let range = Range :: new (
@@ -275,70 +271,16 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
275271 . collect ( )
276272}
277273
278- /// Returns a `Url` object from a given path, will lowercase drive letters if present.
279- /// This will only happen when processing windows paths.
280- ///
281- /// When processing non-windows path, this is essentially the same as `Url::from_file_path`.
282- pub fn url_from_path_with_drive_lowercasing ( path : impl AsRef < Path > ) -> Result < Url > {
283- let component_has_windows_drive = path. as_ref ( ) . components ( ) . any ( |comp| {
284- if let Component :: Prefix ( c) = comp {
285- return matches ! ( c. kind( ) , Prefix :: Disk ( _) | Prefix :: VerbatimDisk ( _) ) ;
286- }
287- false
288- } ) ;
289-
290- // VSCode expects drive letters to be lowercased, where rust will uppercase the drive letters.
291- let res = if component_has_windows_drive {
292- let url_original = Url :: from_file_path ( & path)
293- . map_err ( |_| format ! ( "can't convert path to url: {}" , path. as_ref( ) . display( ) ) ) ?;
294-
295- let drive_partition: Vec < & str > = url_original. as_str ( ) . rsplitn ( 2 , ':' ) . collect ( ) ;
296-
297- // There is a drive partition, but we never found a colon.
298- // This should not happen, but in this case we just pass it through.
299- if drive_partition. len ( ) == 1 {
300- return Ok ( url_original) ;
301- }
302-
303- let joined = drive_partition[ 1 ] . to_ascii_lowercase ( ) + ":" + drive_partition[ 0 ] ;
304- let url = Url :: from_str ( & joined) . expect ( "This came from a valid `Url`" ) ;
305-
306- url
307- } else {
308- Url :: from_file_path ( & path)
309- . map_err ( |_| format ! ( "can't convert path to url: {}" , path. as_ref( ) . display( ) ) ) ?
310- } ;
311- Ok ( res)
312- }
313-
314274#[ cfg( test) ]
275+ #[ cfg( not( windows) ) ]
315276mod tests {
316277 use super :: * ;
317278
318- // `Url` is not able to parse windows paths on unix machines.
319- #[ test]
320- #[ cfg( target_os = "windows" ) ]
321- fn test_lowercase_drive_letter_with_drive ( ) {
322- let url = url_from_path_with_drive_lowercasing ( "C:\\ Test" ) . unwrap ( ) ;
323-
324- assert_eq ! ( url. to_string( ) , "file:///c:/Test" ) ;
325- }
326-
327- #[ test]
328- #[ cfg( target_os = "windows" ) ]
329- fn test_drive_without_colon_passthrough ( ) {
330- let url = url_from_path_with_drive_lowercasing ( r#"\\localhost\C$\my_dir"# ) . unwrap ( ) ;
331-
332- assert_eq ! ( url. to_string( ) , "file://localhost/C$/my_dir" ) ;
333- }
334-
335- #[ cfg( not( windows) ) ]
336279 fn parse_diagnostic ( val : & str ) -> ra_flycheck:: Diagnostic {
337280 serde_json:: from_str :: < ra_flycheck:: Diagnostic > ( val) . unwrap ( )
338281 }
339282
340283 #[ test]
341- #[ cfg( not( windows) ) ]
342284 fn snap_rustc_incompatible_type_for_trait ( ) {
343285 let diag = parse_diagnostic (
344286 r##"{
@@ -392,7 +334,6 @@ mod tests {
392334 }
393335
394336 #[ test]
395- #[ cfg( not( windows) ) ]
396337 fn snap_rustc_unused_variable ( ) {
397338 let diag = parse_diagnostic (
398339 r##"{
@@ -475,7 +416,6 @@ mod tests {
475416 }
476417
477418 #[ test]
478- #[ cfg( not( windows) ) ]
479419 fn snap_rustc_wrong_number_of_parameters ( ) {
480420 let diag = parse_diagnostic (
481421 r##"{
@@ -600,7 +540,6 @@ mod tests {
600540 }
601541
602542 #[ test]
603- #[ cfg( not( windows) ) ]
604543 fn snap_clippy_pass_by_ref ( ) {
605544 let diag = parse_diagnostic (
606545 r##"{
@@ -721,7 +660,6 @@ mod tests {
721660 }
722661
723662 #[ test]
724- #[ cfg( not( windows) ) ]
725663 fn snap_rustc_mismatched_type ( ) {
726664 let diag = parse_diagnostic (
727665 r##"{
@@ -765,7 +703,6 @@ mod tests {
765703 }
766704
767705 #[ test]
768- #[ cfg( not( windows) ) ]
769706 fn snap_handles_macro_location ( ) {
770707 let diag = parse_diagnostic (
771708 r##"{
@@ -1037,7 +974,6 @@ mod tests {
1037974 }
1038975
1039976 #[ test]
1040- #[ cfg( not( windows) ) ]
1041977 fn snap_macro_compiler_error ( ) {
1042978 let diag = parse_diagnostic (
1043979 r##"{
@@ -1267,7 +1203,6 @@ mod tests {
12671203 }
12681204
12691205 #[ test]
1270- #[ cfg( not( windows) ) ]
12711206 fn snap_multi_line_fix ( ) {
12721207 let diag = parse_diagnostic (
12731208 r##"{
0 commit comments