Adjust relative paths to the sysroot rather than rustup's home

This commit is contained in:
Igor Matuszewski 2019-08-02 14:15:05 +02:00
parent 3acf89036a
commit 34946272c6
2 changed files with 18 additions and 46 deletions

View File

@ -15,6 +15,9 @@ fn main() {
);
println!(
"cargo:rustc-env=FIXTURES_DIR={}",
Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("tests/fixtures").display()
Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap())
.join("tests")
.join("fixtures")
.display()
);
}

View File

@ -504,43 +504,6 @@ fn create_tooltip(
tooltip
}
/// Skips `skip_components` from the `path` if the path starts with `prefix`.
/// Returns the original path if there is no match.
///
/// # Examples
///
/// ```ignore
/// # use std::path::Path;
///
/// let base_path = Path::new(".rustup/toolchains/nightly-x86_64-pc-windows-msvc/lib/rustlib/src/rust/src/liballoc/string.rs");
/// let tidy_path = skip_path_components(base_path, ".rustup", 7);
/// assert_eq!(tidy_path, Some(PathBuf::from("liballoc/string.rs")));
///
/// let base_path = Path::new("/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.2/lib.rs");
/// let tidy_path = skip_path_components(base_path, "/home/user/.cargo", 3);
/// assert_eq!(tidy_path, Some(PathBuf::from("smallvec-0.6.2/lib.rs")));
///
/// let base_path = Path::new("/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.2/lib.rs");
/// let tidy_path = skip_path_components(base_path, ".cargo", 3);
/// assert_eq!(tidy_path, None);
///
/// let base_path = Path::new("some/unknown/path/lib.rs");
/// let tidy_path = skip_path_components(base_path, ".rustup", 4);
/// assert_eq!(tidy_path, None);
/// ```
fn skip_path_components<P: AsRef<Path>>(
path: &Path,
prefix: P,
skip_components: usize,
) -> Option<PathBuf> {
path.strip_prefix(prefix).ok().map(|stripped| {
stripped.components().skip(skip_components).fold(PathBuf::new(), |mut comps, comp| {
comps.push(comp);
comps
})
})
}
/// Collapses parent directory references inside of paths.
///
/// # Example
@ -613,22 +576,28 @@ fn racer_match_to_def(ctx: &InitActionContext, m: &racer::Match) -> Option<Def>
use std::env;
let home = home::home_dir().unwrap_or_default();
let rustup_home =
env::var("RUSTUP_HOME").map(PathBuf::from).unwrap_or_else(|_| home.join(".rustup"));
let cargo_home =
env::var("CARGO_HOME").map(PathBuf::from).unwrap_or_else(|_| home.join(".cargo"));
let cargo_registry_src =
cargo_home.join("registry").join("src").join("github.com-1ecc6299db9ec823");
let rust_src_path = racer::get_rust_src_path().ok();
let contextstr = m.contextstr.replacen("\\\\?\\", "", 1);
let contextstr_path = PathBuf::from(&contextstr);
let contextstr_path = collapse_parents(contextstr_path);
// Tidy up the module path.
// Skips `toolchains/$TOOLCHAIN/lib/rustlib/src/rust/src`.
skip_path_components(&contextstr_path, rustup_home, 7)
// Skips `/registry/src/github.com-1ecc6299db9ec823/`.
.or_else(|| skip_path_components(&contextstr_path, cargo_home, 3))
// Make the path relative to the root of the project, if possible.
// Attempt to tidy up the module path
rust_src_path
.and_then(|rust_src_path| {
// Make the path relative to Rust src root
contextstr_path.strip_prefix(rust_src_path).ok().map(ToOwned::to_owned)
})
.or_else(|| {
// Make the path relative to the package root cached in Cargo registry
contextstr_path.strip_prefix(cargo_registry_src).ok().map(ToOwned::to_owned)
})
.or_else(|| {
// Make the path relative to the root of the project
contextstr_path.strip_prefix(&ctx.current_project).ok().map(ToOwned::to_owned)
})
.and_then(|path| path.to_str().map(ToOwned::to_owned))