@@ -504,43 +504,6 @@ fn create_tooltip(
504
504
tooltip
505
505
}
506
506
507
- /// Skips `skip_components` from the `path` if the path starts with `prefix`.
508
- /// Returns the original path if there is no match.
509
- ///
510
- /// # Examples
511
- ///
512
- /// ```ignore
513
- /// # use std::path::Path;
514
- ///
515
- /// let base_path = Path::new(".rustup/toolchains/nightly-x86_64-pc-windows-msvc/lib/rustlib/src/rust/src/liballoc/string.rs");
516
- /// let tidy_path = skip_path_components(base_path, ".rustup", 7);
517
- /// assert_eq!(tidy_path, Some(PathBuf::from("liballoc/string.rs")));
518
- ///
519
- /// let base_path = Path::new("/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.2/lib.rs");
520
- /// let tidy_path = skip_path_components(base_path, "/home/user/.cargo", 3);
521
- /// assert_eq!(tidy_path, Some(PathBuf::from("smallvec-0.6.2/lib.rs")));
522
- ///
523
- /// let base_path = Path::new("/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.2/lib.rs");
524
- /// let tidy_path = skip_path_components(base_path, ".cargo", 3);
525
- /// assert_eq!(tidy_path, None);
526
- ///
527
- /// let base_path = Path::new("some/unknown/path/lib.rs");
528
- /// let tidy_path = skip_path_components(base_path, ".rustup", 4);
529
- /// assert_eq!(tidy_path, None);
530
- /// ```
531
- fn skip_path_components < P : AsRef < Path > > (
532
- path : & Path ,
533
- prefix : P ,
534
- skip_components : usize ,
535
- ) -> Option < PathBuf > {
536
- path. strip_prefix ( prefix) . ok ( ) . map ( |stripped| {
537
- stripped. components ( ) . skip ( skip_components) . fold ( PathBuf :: new ( ) , |mut comps, comp| {
538
- comps. push ( comp) ;
539
- comps
540
- } )
541
- } )
542
- }
543
-
544
507
/// Collapses parent directory references inside of paths.
545
508
///
546
509
/// # Example
@@ -613,20 +576,22 @@ fn racer_match_to_def(ctx: &InitActionContext, m: &racer::Match) -> Option<Def>
613
576
use std:: env;
614
577
615
578
let home = home:: home_dir ( ) . unwrap_or_default ( ) ;
616
- let rustup_home =
617
- env:: var ( "RUSTUP_HOME" ) . map ( PathBuf :: from) . unwrap_or_else ( |_| home. join ( ".rustup" ) ) ;
618
579
let cargo_home =
619
580
env:: var ( "CARGO_HOME" ) . map ( PathBuf :: from) . unwrap_or_else ( |_| home. join ( ".cargo" ) ) ;
581
+ let cargo_registry_src = cargo_home. join ( "registry" ) . join ( "src" ) . join ( "github.com-1ecc6299db9ec823" ) ;
582
+ let rust_src_path = racer:: get_rust_src_path ( ) . ok ( ) ;
620
583
621
584
let contextstr = m. contextstr . replacen ( "\\ \\ ?\\ " , "" , 1 ) ;
622
585
let contextstr_path = PathBuf :: from ( & contextstr) ;
623
586
let contextstr_path = collapse_parents ( contextstr_path) ;
624
587
588
+
625
589
// Tidy up the module path.
626
- // Skips `toolchains/$TOOLCHAIN/lib/rustlib/src/rust/src`.
627
- skip_path_components ( & contextstr_path, rustup_home, 7 )
628
- // Skips `/registry/src/github.com-1ecc6299db9ec823/`.
629
- . or_else ( || skip_path_components ( & contextstr_path, cargo_home, 3 ) )
590
+ // Try to display the path relative to libstd src root if possible.
591
+ rust_src_path
592
+ . and_then ( |path| contextstr_path. strip_prefix ( path) . ok ( ) . map ( ToOwned :: to_owned) )
593
+ // Make the path relative to the package root cached in Cargo registry
594
+ . or_else ( || contextstr_path. strip_prefix ( cargo_registry_src) . ok ( ) . map ( ToOwned :: to_owned) )
630
595
// Make the path relative to the root of the project, if possible.
631
596
. or_else ( || {
632
597
contextstr_path. strip_prefix ( & ctx. current_project ) . ok ( ) . map ( ToOwned :: to_owned)
0 commit comments