Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 173be77

Browse files
authored
Merge pull request #1201 from Xanewok/tooltip-relative-dir
Respect ${CARGO,RUSTUP}_HOME for tooltip relative dirs
2 parents f3bc772 + d6e2947 commit 173be77

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

src/actions/hover.rs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -543,35 +543,36 @@ fn create_tooltip(
543543
/// ```
544544
/// # use std::path::Path;
545545
///
546-
/// let base_path = Path::from(".rustup/toolchains/nightly-x86_64-pc-windows-msvc/lib/rustlib/src/rust/src/liballoc/string.rs");
547-
/// let tidy_path = skip_path_components(base_path.to_path_buf(), ".rustup", 8);
548-
/// assert_eq!("liballoc/string.rs", tidy_path);
546+
/// let base_path = Path::new(".rustup/toolchains/nightly-x86_64-pc-windows-msvc/lib/rustlib/src/rust/src/liballoc/string.rs");
547+
/// let tidy_path = skip_path_components(base_path, ".rustup", 7);
548+
/// assert_eq!(tidy_path, Some(PathBuf::from("liballoc/string.rs")));
549549
///
550-
/// let base_path = Path::from(".cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.2/lib.rs");
551-
/// let tidy_path = skip_path_components(base_path.to_path_buf(), ".cargo", 4);
552-
/// assert_eq!("smallvec-0.6.2/lib.rs", tidy_path);
550+
/// let base_path = Path::new("/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.2/lib.rs");
551+
/// let tidy_path = skip_path_components(base_path, "/home/user/.cargo", 3);
552+
/// assert_eq!(tidy_path, Some(PathBuf::from("smallvec-0.6.2/lib.rs")));
553553
///
554-
/// let base_path = Path::from("some/unknown/path/lib.rs");
555-
/// let tidy_path = skip_path_components(base_path.to_path_buf(), ".rustup", 4);
556-
/// assert_eq!("some/unknown/path/lib.rs", tidy_path);
554+
/// let base_path = Path::new("/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.2/lib.rs");
555+
/// let tidy_path = skip_path_components(base_path, ".cargo", 3);
556+
/// assert_eq!(tidy_path, None);
557+
///
558+
/// let base_path = Path::new("some/unknown/path/lib.rs");
559+
/// let tidy_path = skip_path_components(base_path, ".rustup", 4);
560+
/// assert_eq!(tidy_path, None);
557561
/// ```
558562
fn skip_path_components<P: AsRef<Path>>(
559-
path: PathBuf,
563+
path: &Path,
560564
prefix: P,
561565
skip_components: usize,
562-
) -> PathBuf {
563-
if path.starts_with(prefix) {
564-
let comps: PathBuf =
565-
path.components()
566-
.skip(skip_components)
567-
.fold(PathBuf::new(), |mut comps, comp| {
568-
comps.push(comp);
569-
comps
570-
});
571-
comps
572-
} else {
573-
path
574-
}
566+
) -> Option<PathBuf> {
567+
path.strip_prefix(prefix).ok().map(|stripped| {
568+
stripped
569+
.components()
570+
.skip(skip_components)
571+
.fold(PathBuf::new(), |mut comps, comp| {
572+
comps.push(comp);
573+
comps
574+
})
575+
})
575576
}
576577

577578
/// Collapse parent directory references inside of paths.
@@ -647,23 +648,26 @@ fn racer_match_to_def(ctx: &InitActionContext, m: &racer::Match) -> Option<Def>
647648

648649
let home = env::var("HOME")
649650
.or_else(|_| env::var("USERPROFILE"))
650-
.map(|dir| PathBuf::from(&dir))
651-
.unwrap_or_else(|_| PathBuf::new());
651+
.map(PathBuf::from)
652+
.unwrap_or_default();
653+
let rustup_home = env::var("RUSTUP_HOME")
654+
.map(PathBuf::from)
655+
.unwrap_or_else(|_| home.join(".rustup"));
656+
let cargo_home = env::var("CARGO_HOME")
657+
.map(PathBuf::from)
658+
.unwrap_or_else(|_| home.join(".cargo"));
652659

653660
let contextstr = m.contextstr.replacen("\\\\?\\", "", 1);
654661
let contextstr_path = PathBuf::from(&contextstr);
655662
let contextstr_path = collapse_parents(contextstr_path);
656663

657664
// Tidy up the module path
658-
contextstr_path
659-
// Strip current project dir prefix
660-
.strip_prefix(&ctx.current_project)
661-
// Strip home directory prefix
662-
.or_else(|_| contextstr_path.strip_prefix(&home))
663-
.ok()
664-
.map(|path| path.to_path_buf())
665-
.map(|path| skip_path_components(path, ".rustup", 8))
666-
.map(|path| skip_path_components(path, ".cargo", 4))
665+
// Skips toolchains/$TOOLCHAIN/lib/rustlib/src/rust/src
666+
skip_path_components(&contextstr_path, rustup_home, 7)
667+
// Skips /registry/src/github.com-1ecc6299db9ec823/
668+
.or_else(|| skip_path_components(&contextstr_path, cargo_home, 3))
669+
// Make the path relative to the root of the project, if possible
670+
.or_else(|| contextstr_path.strip_prefix(&ctx.current_project).ok().map(|x| x.to_owned()))
667671
.and_then(|path| path.to_str().map(|s| s.to_string()))
668672
.unwrap_or_else(|| contextstr.to_string())
669673
} else {

0 commit comments

Comments
 (0)