Skip to content

Commit 4f26a37

Browse files
committed
Indicate when project needs a reload
1 parent 3ef7676 commit 4f26a37

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

crates/rust-analyzer/src/global_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub(crate) enum Status {
3232
Loading,
3333
Ready,
3434
Invalid,
35+
NeedsReload,
3536
}
3637

3738
impl Default for Status {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,35 @@ impl GlobalState {
111111
}
112112

113113
fn run(mut self, inbox: Receiver<lsp_server::Message>) -> Result<()> {
114+
let registration_options = lsp_types::TextDocumentRegistrationOptions {
115+
document_selector: Some(vec![
116+
lsp_types::DocumentFilter {
117+
language: None,
118+
scheme: None,
119+
pattern: Some("**/*.rs".into()),
120+
},
121+
lsp_types::DocumentFilter {
122+
language: None,
123+
scheme: None,
124+
pattern: Some("**/Cargo.toml".into()),
125+
},
126+
lsp_types::DocumentFilter {
127+
language: None,
128+
scheme: None,
129+
pattern: Some("**/Cargo.lock".into()),
130+
},
131+
]),
132+
};
133+
let registration = lsp_types::Registration {
134+
id: "textDocument/didSave".to_string(),
135+
method: "textDocument/didSave".to_string(),
136+
register_options: Some(serde_json::to_value(registration_options).unwrap()),
137+
};
138+
self.send_request::<lsp_types::request::RegisterCapability>(
139+
lsp_types::RegistrationParams { registrations: vec![registration] },
140+
|_, _| (),
141+
);
142+
114143
self.reload();
115144

116145
while let Some(event) = self.next_event(&inbox) {
@@ -281,6 +310,7 @@ impl GlobalState {
281310
Status::Loading => lsp_ext::Status::Loading,
282311
Status::Ready => lsp_ext::Status::Ready,
283312
Status::Invalid => lsp_ext::Status::Invalid,
313+
Status::NeedsReload => lsp_ext::Status::NeedsReload,
284314
};
285315
self.send_notification::<lsp_ext::StatusNotification>(lsp_status);
286316
}
@@ -395,10 +425,16 @@ impl GlobalState {
395425
);
396426
Ok(())
397427
})?
398-
.on::<lsp_types::notification::DidSaveTextDocument>(|this, _params| {
428+
.on::<lsp_types::notification::DidSaveTextDocument>(|this, params| {
399429
if let Some(flycheck) = &this.flycheck {
400430
flycheck.handle.update();
401431
}
432+
let uri = params.text_document.uri.as_str();
433+
if uri.ends_with("Cargo.toml") || uri.ends_with("Cargo.lock") {
434+
if matches!(this.status, Status::Ready | Status::Invalid) {
435+
this.transition(Status::NeedsReload);
436+
}
437+
}
402438
Ok(())
403439
})?
404440
.on::<lsp_types::notification::DidChangeConfiguration>(|this, _params| {

crates/rust-analyzer/src/reload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl GlobalState {
7878
.collect(),
7979
};
8080
let registration = lsp_types::Registration {
81-
id: "file-watcher".to_string(),
81+
id: "workspace/didChangeWatchedFiles".to_string(),
8282
method: "workspace/didChangeWatchedFiles".to_string(),
8383
register_options: Some(serde_json::to_value(registration_options).unwrap()),
8484
};

crates/rust-analyzer/tests/heavy_tests/support.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,19 @@ impl Server {
176176
while let Some(msg) = self.recv() {
177177
match msg {
178178
Message::Request(req) => {
179-
if req.method != "window/workDoneProgress/create"
180-
&& !(req.method == "client/registerCapability"
181-
&& req.params.to_string().contains("workspace/didChangeWatchedFiles"))
182-
{
183-
panic!("unexpected request: {:?}", req)
179+
if req.method == "window/workDoneProgress/create" {
180+
continue;
184181
}
182+
if req.method == "client/registerCapability" {
183+
let params = req.params.to_string();
184+
if ["workspace/didChangeWatchedFiles", "textDocument/didSave"]
185+
.iter()
186+
.any(|&it| params.contains(it))
187+
{
188+
continue;
189+
}
190+
}
191+
panic!("unexpected request: {:?}", req)
185192
}
186193
Message::Notification(_) => (),
187194
Message::Response(res) => {

0 commit comments

Comments
 (0)