Skip to content

Commit d70e61d

Browse files
committed
Auto merge of #17143 - Veykril:status-info, r=Veykril
internal: Show workspace info in the status bar cc #17127
2 parents 56bee2d + 18ca22a commit d70e61d

File tree

15 files changed

+168
-81
lines changed

15 files changed

+168
-81
lines changed

.git-blame-ignore-revs

+7
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@
66

77
# prettier format
88
f247090558c9ba3c551566eae5882b7ca865225f
9+
10+
# subtree syncs
11+
932d85b52946d917deab2c23ead552f7f713b828
12+
3e358a6827d83e8d6473913a5e304734aadfed04
13+
9d2cb42a413e51deb50b36794a2e1605381878fc
14+
f532576ac53ddcc666bc8d59e0b6437065e2f599
15+
c48062fe2ab9a2d913d1985a6b0aec4bf936bfc1

crates/hir/src/semantics.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub struct SemanticsImpl<'db> {
131131
pub db: &'db dyn HirDatabase,
132132
s2d_cache: RefCell<SourceToDefCache>,
133133
/// Rootnode to HirFileId cache
134-
cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
134+
root_to_file_cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
135135
// These 2 caches are mainly useful for semantic highlighting as nothing else descends a lot of tokens
136136
// So we might wanna move them out into something specific for semantic highlighting
137137
expansion_info_cache: RefCell<FxHashMap<MacroFileId, ExpansionInfo>>,
@@ -294,7 +294,7 @@ impl<'db> SemanticsImpl<'db> {
294294
SemanticsImpl {
295295
db,
296296
s2d_cache: Default::default(),
297-
cache: Default::default(),
297+
root_to_file_cache: Default::default(),
298298
expansion_info_cache: Default::default(),
299299
macro_call_cache: Default::default(),
300300
}
@@ -690,6 +690,7 @@ impl<'db> SemanticsImpl<'db> {
690690
exp_info
691691
});
692692

693+
// FIXME: uncached parse
693694
// Create the source analyzer for the macro call scope
694695
let Some(sa) = self.analyze_no_infer(&self.parse_or_expand(expansion_info.call_file()))
695696
else {
@@ -1025,6 +1026,7 @@ impl<'db> SemanticsImpl<'db> {
10251026
None => {
10261027
let call_node = file_id.macro_file()?.call_node(db);
10271028
// cache the node
1029+
// FIXME: uncached parse
10281030
self.parse_or_expand(call_node.file_id);
10291031
Some(call_node)
10301032
}
@@ -1397,7 +1399,7 @@ impl<'db> SemanticsImpl<'db> {
13971399

13981400
fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) {
13991401
assert!(root_node.parent().is_none());
1400-
let mut cache = self.cache.borrow_mut();
1402+
let mut cache = self.root_to_file_cache.borrow_mut();
14011403
let prev = cache.insert(root_node, file_id);
14021404
assert!(prev.is_none() || prev == Some(file_id))
14031405
}
@@ -1407,7 +1409,7 @@ impl<'db> SemanticsImpl<'db> {
14071409
}
14081410

14091411
fn lookup(&self, root_node: &SyntaxNode) -> Option<HirFileId> {
1410-
let cache = self.cache.borrow();
1412+
let cache = self.root_to_file_cache.borrow();
14111413
cache.get(root_node).copied()
14121414
}
14131415

@@ -1427,7 +1429,7 @@ impl<'db> SemanticsImpl<'db> {
14271429
known nodes: {}\n\n",
14281430
node,
14291431
root_node,
1430-
self.cache
1432+
self.root_to_file_cache
14311433
.borrow()
14321434
.keys()
14331435
.map(|it| format!("{it:?}"))

crates/load-cargo/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ pub fn load_workspace(
6868
let proc_macro_server = match &load_config.with_proc_macro_server {
6969
ProcMacroServerChoice::Sysroot => ws
7070
.find_sysroot_proc_macro_srv()
71-
.and_then(|it| ProcMacroServer::spawn(it, extra_env).map_err(Into::into)),
71+
.and_then(|it| ProcMacroServer::spawn(&it, extra_env).map_err(Into::into)),
7272
ProcMacroServerChoice::Explicit(path) => {
73-
ProcMacroServer::spawn(path.clone(), extra_env).map_err(Into::into)
73+
ProcMacroServer::spawn(path, extra_env).map_err(Into::into)
7474
}
7575
ProcMacroServerChoice::None => Err(anyhow::format_err!("proc macro server disabled")),
7676
};

crates/proc-macro-api/src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod version;
1313

1414
use base_db::Env;
1515
use indexmap::IndexSet;
16-
use paths::AbsPathBuf;
16+
use paths::{AbsPath, AbsPathBuf};
1717
use rustc_hash::FxHashMap;
1818
use span::Span;
1919
use std::{
@@ -54,6 +54,7 @@ pub struct ProcMacroServer {
5454
///
5555
/// Therefore, we just wrap the `ProcMacroProcessSrv` in a mutex here.
5656
process: Arc<Mutex<ProcMacroProcessSrv>>,
57+
path: AbsPathBuf,
5758
}
5859

5960
pub struct MacroDylib {
@@ -113,11 +114,18 @@ pub struct MacroPanic {
113114
impl ProcMacroServer {
114115
/// Spawns an external process as the proc macro server and returns a client connected to it.
115116
pub fn spawn(
116-
process_path: AbsPathBuf,
117+
process_path: &AbsPath,
117118
env: &FxHashMap<String, String>,
118119
) -> io::Result<ProcMacroServer> {
119120
let process = ProcMacroProcessSrv::run(process_path, env)?;
120-
Ok(ProcMacroServer { process: Arc::new(Mutex::new(process)) })
121+
Ok(ProcMacroServer {
122+
process: Arc::new(Mutex::new(process)),
123+
path: process_path.to_owned(),
124+
})
125+
}
126+
127+
pub fn path(&self) -> &AbsPath {
128+
&self.path
121129
}
122130

123131
pub fn load_dylib(&self, dylib: MacroDylib) -> Result<Vec<ProcMacro>, ServerError> {

crates/proc-macro-api/src/process.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
sync::Arc,
77
};
88

9-
use paths::{AbsPath, AbsPathBuf};
9+
use paths::AbsPath;
1010
use rustc_hash::FxHashMap;
1111
use stdx::JodChild;
1212

@@ -28,11 +28,11 @@ pub(crate) struct ProcMacroProcessSrv {
2828

2929
impl ProcMacroProcessSrv {
3030
pub(crate) fn run(
31-
process_path: AbsPathBuf,
31+
process_path: &AbsPath,
3232
env: &FxHashMap<String, String>,
3333
) -> io::Result<ProcMacroProcessSrv> {
3434
let create_srv = |null_stderr| {
35-
let mut process = Process::run(process_path.clone(), env, null_stderr)?;
35+
let mut process = Process::run(process_path, env, null_stderr)?;
3636
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
3737

3838
io::Result::Ok(ProcMacroProcessSrv {
@@ -153,11 +153,11 @@ struct Process {
153153

154154
impl Process {
155155
fn run(
156-
path: AbsPathBuf,
156+
path: &AbsPath,
157157
env: &FxHashMap<String, String>,
158158
null_stderr: bool,
159159
) -> io::Result<Process> {
160-
let child = JodChild(mk_child(&path, env, null_stderr)?);
160+
let child = JodChild(mk_child(path, env, null_stderr)?);
161161
Ok(Process { child })
162162
}
163163

crates/project-model/src/project_json.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use serde::{de, Deserialize, Serialize};
5656
use span::Edition;
5757

5858
use crate::cfg::CfgFlag;
59+
use crate::ManifestPath;
5960

6061
/// Roots and crates that compose this Rust project.
6162
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -65,6 +66,7 @@ pub struct ProjectJson {
6566
/// e.g. `path/to/sysroot/lib/rustlib/src/rust`
6667
pub(crate) sysroot_src: Option<AbsPathBuf>,
6768
project_root: AbsPathBuf,
69+
manifest: Option<ManifestPath>,
6870
crates: Vec<Crate>,
6971
}
7072

@@ -96,12 +98,17 @@ impl ProjectJson {
9698
/// * `base` - The path to the workspace root (i.e. the folder containing `rust-project.json`)
9799
/// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via
98100
/// configuration.
99-
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
101+
pub fn new(
102+
manifest: Option<ManifestPath>,
103+
base: &AbsPath,
104+
data: ProjectJsonData,
105+
) -> ProjectJson {
100106
let absolutize_on_base = |p| base.absolutize(p);
101107
ProjectJson {
102108
sysroot: data.sysroot.map(absolutize_on_base),
103109
sysroot_src: data.sysroot_src.map(absolutize_on_base),
104110
project_root: base.to_path_buf(),
111+
manifest,
105112
crates: data
106113
.crates
107114
.into_iter()
@@ -159,6 +166,11 @@ impl ProjectJson {
159166
pub fn path(&self) -> &AbsPath {
160167
&self.project_root
161168
}
169+
170+
/// Returns the path to the project's manifest or root folder, if no manifest exists.
171+
pub fn manifest_or_root(&self) -> &AbsPath {
172+
self.manifest.as_ref().map_or(&self.project_root, |manifest| manifest.as_ref())
173+
}
162174
}
163175

164176
#[derive(Serialize, Deserialize, Debug, Clone)]

crates/project-model/src/sysroot.rs

+18
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ impl Sysroot {
133133
}
134134
}
135135

136+
pub fn check_has_core(&self) -> Result<(), String> {
137+
let Some(Ok(src_root)) = &self.src_root else { return Ok(()) };
138+
let has_core = match &self.mode {
139+
SysrootMode::Workspace(ws) => ws.packages().any(|p| ws[p].name == "core"),
140+
SysrootMode::Stitched(stitched) => stitched.by_name("core").is_some(),
141+
};
142+
if !has_core {
143+
let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
144+
" (`RUST_SRC_PATH` might be incorrect, try unsetting it)"
145+
} else {
146+
" try running `rustup component add rust-src` to possible fix this"
147+
};
148+
Err(format!("could not find libcore in loaded sysroot at `{}`{var_note}", src_root,))
149+
} else {
150+
Ok(())
151+
}
152+
}
153+
136154
pub fn num_packages(&self) -> usize {
137155
match &self.mode {
138156
SysrootMode::Workspace(ws) => ws.packages().count(),

crates/project-model/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ fn rooted_project_json(data: ProjectJsonData) -> ProjectJson {
152152
replace_root(&mut root, true);
153153
let path = Utf8Path::new(&root);
154154
let base = AbsPath::assert(path);
155-
ProjectJson::new(base, data)
155+
ProjectJson::new(None, base, data)
156156
}
157157

158158
fn to_crate_graph(project_workspace: ProjectWorkspace) -> (CrateGraph, ProcMacroPaths) {

crates/project-model/src/workspace.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ impl ProjectWorkspace {
199199
let data = serde_json::from_str(&file)
200200
.with_context(|| format!("Failed to deserialize json file {project_json}"))?;
201201
let project_location = project_json.parent().to_path_buf();
202-
let project_json: ProjectJson = ProjectJson::new(&project_location, data);
202+
let project_json: ProjectJson =
203+
ProjectJson::new(Some(project_json.clone()), &project_location, data);
203204
ProjectWorkspace::load_inline(
204205
project_json,
205206
config.target.as_deref(),
@@ -555,7 +556,7 @@ impl ProjectWorkspace {
555556
pub fn manifest_or_root(&self) -> &AbsPath {
556557
match &self.kind {
557558
ProjectWorkspaceKind::Cargo { cargo, .. } => cargo.manifest_path(),
558-
ProjectWorkspaceKind::Json(project) => project.path(),
559+
ProjectWorkspaceKind::Json(project) => project.manifest_or_root(),
559560
ProjectWorkspaceKind::DetachedFile { file, .. } => file,
560561
}
561562
}

crates/rust-analyzer/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ impl Config {
13231323
.map(Into::into)
13241324
}
13251325
ManifestOrProjectJson::ProjectJson(it) => {
1326-
Some(ProjectJson::new(&self.root_path, it.clone()).into())
1326+
Some(ProjectJson::new(None, &self.root_path, it.clone()).into())
13271327
}
13281328
})
13291329
.collect(),

crates/rust-analyzer/src/lsp/ext.rs

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![allow(clippy::disallowed_types)]
44

5+
use std::ops;
56
use std::path::PathBuf;
67

78
use ide_db::line_index::WideEncoding;
@@ -494,10 +495,12 @@ impl Notification for ServerStatusNotification {
494495
}
495496

496497
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone)]
498+
#[serde(rename_all = "camelCase")]
497499
pub struct ServerStatusParams {
498500
pub health: Health,
499501
pub quiescent: bool,
500502
pub message: Option<String>,
503+
pub workspace_info: Option<String>,
501504
}
502505

503506
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
@@ -508,6 +511,16 @@ pub enum Health {
508511
Error,
509512
}
510513

514+
impl ops::BitOrAssign for Health {
515+
fn bitor_assign(&mut self, rhs: Self) {
516+
*self = match (*self, rhs) {
517+
(Health::Error, _) | (_, Health::Error) => Health::Error,
518+
(Health::Warning, _) | (_, Health::Warning) => Health::Warning,
519+
_ => Health::Ok,
520+
}
521+
}
522+
}
523+
511524
pub enum CodeActionRequest {}
512525

513526
impl Request for CodeActionRequest {

0 commit comments

Comments
 (0)