mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Show workspace info in the status bar
This commit is contained in:
parent
56bee2ddaf
commit
18ca22a98e
15 changed files with 168 additions and 81 deletions
|
@ -56,6 +56,7 @@ use serde::{de, Deserialize, Serialize};
|
|||
use span::Edition;
|
||||
|
||||
use crate::cfg::CfgFlag;
|
||||
use crate::ManifestPath;
|
||||
|
||||
/// Roots and crates that compose this Rust project.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
@ -65,6 +66,7 @@ pub struct ProjectJson {
|
|||
/// e.g. `path/to/sysroot/lib/rustlib/src/rust`
|
||||
pub(crate) sysroot_src: Option<AbsPathBuf>,
|
||||
project_root: AbsPathBuf,
|
||||
manifest: Option<ManifestPath>,
|
||||
crates: Vec<Crate>,
|
||||
}
|
||||
|
||||
|
@ -96,12 +98,17 @@ impl ProjectJson {
|
|||
/// * `base` - The path to the workspace root (i.e. the folder containing `rust-project.json`)
|
||||
/// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via
|
||||
/// configuration.
|
||||
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
|
||||
pub fn new(
|
||||
manifest: Option<ManifestPath>,
|
||||
base: &AbsPath,
|
||||
data: ProjectJsonData,
|
||||
) -> ProjectJson {
|
||||
let absolutize_on_base = |p| base.absolutize(p);
|
||||
ProjectJson {
|
||||
sysroot: data.sysroot.map(absolutize_on_base),
|
||||
sysroot_src: data.sysroot_src.map(absolutize_on_base),
|
||||
project_root: base.to_path_buf(),
|
||||
manifest,
|
||||
crates: data
|
||||
.crates
|
||||
.into_iter()
|
||||
|
@ -159,6 +166,11 @@ impl ProjectJson {
|
|||
pub fn path(&self) -> &AbsPath {
|
||||
&self.project_root
|
||||
}
|
||||
|
||||
/// Returns the path to the project's manifest or root folder, if no manifest exists.
|
||||
pub fn manifest_or_root(&self) -> &AbsPath {
|
||||
self.manifest.as_ref().map_or(&self.project_root, |manifest| manifest.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
|
|
|
@ -133,6 +133,24 @@ impl Sysroot {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn check_has_core(&self) -> Result<(), String> {
|
||||
let Some(Ok(src_root)) = &self.src_root else { return Ok(()) };
|
||||
let has_core = match &self.mode {
|
||||
SysrootMode::Workspace(ws) => ws.packages().any(|p| ws[p].name == "core"),
|
||||
SysrootMode::Stitched(stitched) => stitched.by_name("core").is_some(),
|
||||
};
|
||||
if !has_core {
|
||||
let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
|
||||
" (`RUST_SRC_PATH` might be incorrect, try unsetting it)"
|
||||
} else {
|
||||
" try running `rustup component add rust-src` to possible fix this"
|
||||
};
|
||||
Err(format!("could not find libcore in loaded sysroot at `{}`{var_note}", src_root,))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn num_packages(&self) -> usize {
|
||||
match &self.mode {
|
||||
SysrootMode::Workspace(ws) => ws.packages().count(),
|
||||
|
|
|
@ -152,7 +152,7 @@ fn rooted_project_json(data: ProjectJsonData) -> ProjectJson {
|
|||
replace_root(&mut root, true);
|
||||
let path = Utf8Path::new(&root);
|
||||
let base = AbsPath::assert(path);
|
||||
ProjectJson::new(base, data)
|
||||
ProjectJson::new(None, base, data)
|
||||
}
|
||||
|
||||
fn to_crate_graph(project_workspace: ProjectWorkspace) -> (CrateGraph, ProcMacroPaths) {
|
||||
|
|
|
@ -199,7 +199,8 @@ impl ProjectWorkspace {
|
|||
let data = serde_json::from_str(&file)
|
||||
.with_context(|| format!("Failed to deserialize json file {project_json}"))?;
|
||||
let project_location = project_json.parent().to_path_buf();
|
||||
let project_json: ProjectJson = ProjectJson::new(&project_location, data);
|
||||
let project_json: ProjectJson =
|
||||
ProjectJson::new(Some(project_json.clone()), &project_location, data);
|
||||
ProjectWorkspace::load_inline(
|
||||
project_json,
|
||||
config.target.as_deref(),
|
||||
|
@ -555,7 +556,7 @@ impl ProjectWorkspace {
|
|||
pub fn manifest_or_root(&self) -> &AbsPath {
|
||||
match &self.kind {
|
||||
ProjectWorkspaceKind::Cargo { cargo, .. } => cargo.manifest_path(),
|
||||
ProjectWorkspaceKind::Json(project) => project.path(),
|
||||
ProjectWorkspaceKind::Json(project) => project.manifest_or_root(),
|
||||
ProjectWorkspaceKind::DetachedFile { file, .. } => file,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue