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

Commit 3e23a2f

Browse files
committed
Adjust relative paths to the sysroot rather than rustup's home
1 parent 8904d5f commit 3e23a2f

File tree

1 file changed

+8
-43
lines changed

1 file changed

+8
-43
lines changed

rls/src/actions/hover.rs

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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,20 +576,22 @@ 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 = cargo_home.join("registry").join("src").join("github.com-1ecc6299db9ec823");
582+
let rust_src_path = racer::get_rust_src_path().ok();
620583

621584
let contextstr = m.contextstr.replacen("\\\\?\\", "", 1);
622585
let contextstr_path = PathBuf::from(&contextstr);
623586
let contextstr_path = collapse_parents(contextstr_path);
624587

588+
625589
// 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))
630595
// Make the path relative to the root of the project, if possible.
631596
.or_else(|| {
632597
contextstr_path.strip_prefix(&ctx.current_project).ok().map(ToOwned::to_owned)

0 commit comments

Comments
 (0)