@@ -504,43 +504,6 @@ fn create_tooltip(
504504 tooltip
505505}
506506
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-
544507/// Collapses parent directory references inside of paths.
545508///
546509/// # Example
@@ -613,22 +576,28 @@ fn racer_match_to_def(ctx: &InitActionContext, m: &racer::Match) -> Option<Def>
613576 use std:: env;
614577
615578 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" ) ) ;
618579 let cargo_home =
619580 env:: var ( "CARGO_HOME" ) . map ( PathBuf :: from) . unwrap_or_else ( |_| home. join ( ".cargo" ) ) ;
581+ let cargo_registry_src =
582+ cargo_home. join ( "registry" ) . join ( "src" ) . join ( "github.com-1ecc6299db9ec823" ) ;
583+ let rust_src_path = racer:: get_rust_src_path ( ) . ok ( ) ;
620584
621585 let contextstr = m. contextstr . replacen ( "\\ \\ ?\\ " , "" , 1 ) ;
622586 let contextstr_path = PathBuf :: from ( & contextstr) ;
623587 let contextstr_path = collapse_parents ( contextstr_path) ;
624588
625- // 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 ) )
630- // Make the path relative to the root of the project, if possible.
589+ // Attempt to tidy up the module path
590+ rust_src_path
591+ . and_then ( |rust_src_path| {
592+ // Make the path relative to Rust src root
593+ contextstr_path. strip_prefix ( rust_src_path) . ok ( ) . map ( ToOwned :: to_owned)
594+ } )
595+ . or_else ( || {
596+ // Make the path relative to the package root cached in Cargo registry
597+ contextstr_path. strip_prefix ( cargo_registry_src) . ok ( ) . map ( ToOwned :: to_owned)
598+ } )
631599 . or_else ( || {
600+ // Make the path relative to the root of the project
632601 contextstr_path. strip_prefix ( & ctx. current_project ) . ok ( ) . map ( ToOwned :: to_owned)
633602 } )
634603 . and_then ( |path| path. to_str ( ) . map ( ToOwned :: to_owned) )
0 commit comments