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 (
@@ -274,70 +270,16 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
274270 . collect ( )
275271}
276272
277- /// Returns a `Url` object from a given path, will lowercase drive letters if present.
278- /// This will only happen when processing windows paths.
279- ///
280- /// When processing non-windows path, this is essentially the same as `Url::from_file_path`.
281- pub fn url_from_path_with_drive_lowercasing ( path : impl AsRef < Path > ) -> Result < Url > {
282- let component_has_windows_drive = path. as_ref ( ) . components ( ) . any ( |comp| {
283- if let Component :: Prefix ( c) = comp {
284- return matches ! ( c. kind( ) , Prefix :: Disk ( _) | Prefix :: VerbatimDisk ( _) ) ;
285- }
286- false
287- } ) ;
288-
289- // VSCode expects drive letters to be lowercased, where rust will uppercase the drive letters.
290- let res = if component_has_windows_drive {
291- let url_original = Url :: from_file_path ( & path)
292- . map_err ( |_| format ! ( "can't convert path to url: {}" , path. as_ref( ) . display( ) ) ) ?;
293-
294- let drive_partition: Vec < & str > = url_original. as_str ( ) . rsplitn ( 2 , ':' ) . collect ( ) ;
295-
296- // There is a drive partition, but we never found a colon.
297- // This should not happen, but in this case we just pass it through.
298- if drive_partition. len ( ) == 1 {
299- return Ok ( url_original) ;
300- }
301-
302- let joined = drive_partition[ 1 ] . to_ascii_lowercase ( ) + ":" + drive_partition[ 0 ] ;
303- let url = Url :: from_str ( & joined) . expect ( "This came from a valid `Url`" ) ;
304-
305- url
306- } else {
307- Url :: from_file_path ( & path)
308- . map_err ( |_| format ! ( "can't convert path to url: {}" , path. as_ref( ) . display( ) ) ) ?
309- } ;
310- Ok ( res)
311- }
312-
313273#[ cfg( test) ]
274+ #[ cfg( not( windows) ) ]
314275mod tests {
315276 use super :: * ;
316277
317- // `Url` is not able to parse windows paths on unix machines.
318- #[ test]
319- #[ cfg( target_os = "windows" ) ]
320- fn test_lowercase_drive_letter_with_drive ( ) {
321- let url = url_from_path_with_drive_lowercasing ( "C:\\ Test" ) . unwrap ( ) ;
322-
323- assert_eq ! ( url. to_string( ) , "file:///c:/Test" ) ;
324- }
325-
326- #[ test]
327- #[ cfg( target_os = "windows" ) ]
328- fn test_drive_without_colon_passthrough ( ) {
329- let url = url_from_path_with_drive_lowercasing ( r#"\\localhost\C$\my_dir"# ) . unwrap ( ) ;
330-
331- assert_eq ! ( url. to_string( ) , "file://localhost/C$/my_dir" ) ;
332- }
333-
334- #[ cfg( not( windows) ) ]
335278 fn parse_diagnostic ( val : & str ) -> ra_flycheck:: Diagnostic {
336279 serde_json:: from_str :: < ra_flycheck:: Diagnostic > ( val) . unwrap ( )
337280 }
338281
339282 #[ test]
340- #[ cfg( not( windows) ) ]
341283 fn snap_rustc_incompatible_type_for_trait ( ) {
342284 let diag = parse_diagnostic (
343285 r##"{
@@ -391,7 +333,6 @@ mod tests {
391333 }
392334
393335 #[ test]
394- #[ cfg( not( windows) ) ]
395336 fn snap_rustc_unused_variable ( ) {
396337 let diag = parse_diagnostic (
397338 r##"{
@@ -474,7 +415,6 @@ mod tests {
474415 }
475416
476417 #[ test]
477- #[ cfg( not( windows) ) ]
478418 fn snap_rustc_wrong_number_of_parameters ( ) {
479419 let diag = parse_diagnostic (
480420 r##"{
@@ -599,7 +539,6 @@ mod tests {
599539 }
600540
601541 #[ test]
602- #[ cfg( not( windows) ) ]
603542 fn snap_clippy_pass_by_ref ( ) {
604543 let diag = parse_diagnostic (
605544 r##"{
@@ -720,7 +659,6 @@ mod tests {
720659 }
721660
722661 #[ test]
723- #[ cfg( not( windows) ) ]
724662 fn snap_rustc_mismatched_type ( ) {
725663 let diag = parse_diagnostic (
726664 r##"{
@@ -764,7 +702,6 @@ mod tests {
764702 }
765703
766704 #[ test]
767- #[ cfg( not( windows) ) ]
768705 fn snap_handles_macro_location ( ) {
769706 let diag = parse_diagnostic (
770707 r##"{
@@ -1036,7 +973,6 @@ mod tests {
1036973 }
1037974
1038975 #[ test]
1039- #[ cfg( not( windows) ) ]
1040976 fn snap_macro_compiler_error ( ) {
1041977 let diag = parse_diagnostic (
1042978 r##"{
@@ -1266,7 +1202,6 @@ mod tests {
12661202 }
12671203
12681204 #[ test]
1269- #[ cfg( not( windows) ) ]
12701205 fn snap_multi_line_fix ( ) {
12711206 let diag = parse_diagnostic (
12721207 r##"{
0 commit comments