Skip to main content

nova_pm/
project.rs

1//! Finding a project (spec §6.1).
2
3use std::path::{Path, PathBuf};
4
5/// A project's manifest file name.
6pub const MANIFEST: &str = "nova.toml";
7
8/// The nearest directory at or above `start` that holds a `nova.toml`
9/// file.
10pub fn find_root(start: &Path) -> Option<PathBuf> {
11    start
12        .ancestors()
13        .find(|dir| dir.join(MANIFEST).is_file())
14        .map(Path::to_path_buf)
15}
16
17/// `path`, canonicalised when it exists, without Windows' `\\?\` prefix, so
18/// that it is spelled as the file system spells it. A package is identified
19/// by its directory's real path (spec 3.3a §3.3), and the language server
20/// builds unopened files' URIs on it.
21pub fn real_path(path: &Path) -> PathBuf {
22    let real = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
23    match real.to_str().and_then(|s| s.strip_prefix(r"\\?\")) {
24        Some(rest) => PathBuf::from(rest),
25        None => real,
26    }
27}