mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 05:53:45 +00:00
Merge #5056
5056: Canonicalize actor API r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
4ec0a2c454
2 changed files with 56 additions and 70 deletions
|
@ -10,7 +10,7 @@ use std::{
|
||||||
time::Instant,
|
time::Instant,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crossbeam_channel::{never, select, unbounded, Receiver, RecvError, Sender};
|
use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
|
||||||
|
|
||||||
pub use cargo_metadata::diagnostic::{
|
pub use cargo_metadata::diagnostic::{
|
||||||
Applicability, Diagnostic, DiagnosticLevel, DiagnosticSpan, DiagnosticSpanMacroExpansion,
|
Applicability, Diagnostic, DiagnosticLevel, DiagnosticSpan, DiagnosticSpanMacroExpansion,
|
||||||
|
@ -61,7 +61,7 @@ impl FlycheckHandle {
|
||||||
) -> FlycheckHandle {
|
) -> FlycheckHandle {
|
||||||
let (cmd_send, cmd_recv) = unbounded::<Restart>();
|
let (cmd_send, cmd_recv) = unbounded::<Restart>();
|
||||||
let handle = jod_thread::spawn(move || {
|
let handle = jod_thread::spawn(move || {
|
||||||
FlycheckActor::new(sender, config, workspace_root).run(&cmd_recv);
|
FlycheckActor::new(sender, config, workspace_root).run(cmd_recv);
|
||||||
});
|
});
|
||||||
FlycheckHandle { cmd_send, handle }
|
FlycheckHandle { cmd_send, handle }
|
||||||
}
|
}
|
||||||
|
@ -98,14 +98,18 @@ struct FlycheckActor {
|
||||||
config: FlycheckConfig,
|
config: FlycheckConfig,
|
||||||
workspace_root: PathBuf,
|
workspace_root: PathBuf,
|
||||||
last_update_req: Option<Instant>,
|
last_update_req: Option<Instant>,
|
||||||
// XXX: drop order is significant
|
|
||||||
message_recv: Receiver<CheckEvent>,
|
|
||||||
/// WatchThread exists to wrap around the communication needed to be able to
|
/// WatchThread exists to wrap around the communication needed to be able to
|
||||||
/// run `cargo check` without blocking. Currently the Rust standard library
|
/// run `cargo check` without blocking. Currently the Rust standard library
|
||||||
/// doesn't provide a way to read sub-process output without blocking, so we
|
/// doesn't provide a way to read sub-process output without blocking, so we
|
||||||
/// have to wrap sub-processes output handling in a thread and pass messages
|
/// have to wrap sub-processes output handling in a thread and pass messages
|
||||||
/// back over a channel.
|
/// back over a channel.
|
||||||
check_process: Option<jod_thread::JoinHandle>,
|
// XXX: drop order is significant
|
||||||
|
check_process: Option<(Receiver<CheckEvent>, jod_thread::JoinHandle)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Event {
|
||||||
|
Restart(Restart),
|
||||||
|
CheckEvent(Option<CheckEvent>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FlycheckActor {
|
impl FlycheckActor {
|
||||||
|
@ -114,65 +118,23 @@ impl FlycheckActor {
|
||||||
config: FlycheckConfig,
|
config: FlycheckConfig,
|
||||||
workspace_root: PathBuf,
|
workspace_root: PathBuf,
|
||||||
) -> FlycheckActor {
|
) -> FlycheckActor {
|
||||||
FlycheckActor {
|
FlycheckActor { sender, config, workspace_root, last_update_req: None, check_process: None }
|
||||||
sender,
|
|
||||||
config,
|
|
||||||
workspace_root,
|
|
||||||
last_update_req: None,
|
|
||||||
message_recv: never(),
|
|
||||||
check_process: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&mut self, cmd_recv: &Receiver<Restart>) {
|
fn run(&mut self, inbox: Receiver<Restart>) {
|
||||||
// If we rerun the thread, we need to discard the previous check results first
|
// If we rerun the thread, we need to discard the previous check results first
|
||||||
self.clean_previous_results();
|
|
||||||
|
|
||||||
loop {
|
|
||||||
select! {
|
|
||||||
recv(&cmd_recv) -> cmd => match cmd {
|
|
||||||
Ok(Restart) => self.last_update_req = Some(Instant::now()),
|
|
||||||
Err(RecvError) => {
|
|
||||||
// Command channel has closed, so shut down
|
|
||||||
break;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
recv(self.message_recv) -> msg => match msg {
|
|
||||||
Ok(msg) => self.handle_message(msg),
|
|
||||||
Err(RecvError) => {
|
|
||||||
// Watcher finished, replace it with a never channel to
|
|
||||||
// avoid busy-waiting.
|
|
||||||
self.message_recv = never();
|
|
||||||
self.check_process = None;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if self.should_recheck() {
|
|
||||||
self.last_update_req = None;
|
|
||||||
self.send(Message::ClearDiagnostics);
|
|
||||||
self.restart_check_process();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn clean_previous_results(&self) {
|
|
||||||
self.send(Message::ClearDiagnostics);
|
self.send(Message::ClearDiagnostics);
|
||||||
self.send(Message::Progress(Progress::End));
|
self.send(Message::Progress(Progress::End));
|
||||||
}
|
|
||||||
|
|
||||||
fn should_recheck(&mut self) -> bool {
|
while let Some(event) = self.next_event(&inbox) {
|
||||||
if let Some(_last_update_req) = &self.last_update_req {
|
match event {
|
||||||
// We currently only request an update on save, as we need up to
|
Event::Restart(Restart) => self.last_update_req = Some(Instant::now()),
|
||||||
// date source on disk for cargo check to do it's magic, so we
|
Event::CheckEvent(None) => {
|
||||||
// don't really need to debounce the requests at this point.
|
// Watcher finished, replace it with a never channel to
|
||||||
return true;
|
// avoid busy-waiting.
|
||||||
|
self.check_process = None;
|
||||||
}
|
}
|
||||||
false
|
Event::CheckEvent(Some(event)) => match event {
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_message(&self, msg: CheckEvent) {
|
|
||||||
match msg {
|
|
||||||
CheckEvent::Begin => {
|
CheckEvent::Begin => {
|
||||||
self.send(Message::Progress(Progress::Being));
|
self.send(Message::Progress(Progress::Being));
|
||||||
}
|
}
|
||||||
|
@ -196,12 +158,36 @@ impl FlycheckActor {
|
||||||
| CheckEvent::Msg(cargo_metadata::Message::BuildFinished(_))
|
| CheckEvent::Msg(cargo_metadata::Message::BuildFinished(_))
|
||||||
| CheckEvent::Msg(cargo_metadata::Message::TextLine(_))
|
| CheckEvent::Msg(cargo_metadata::Message::TextLine(_))
|
||||||
| CheckEvent::Msg(cargo_metadata::Message::Unknown) => {}
|
| CheckEvent::Msg(cargo_metadata::Message::Unknown) => {}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
if self.should_recheck() {
|
||||||
|
self.last_update_req = None;
|
||||||
|
self.send(Message::ClearDiagnostics);
|
||||||
|
self.restart_check_process();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_event(&self, inbox: &Receiver<Restart>) -> Option<Event> {
|
||||||
|
let check_chan = self.check_process.as_ref().map(|(chan, _thread)| chan);
|
||||||
|
select! {
|
||||||
|
recv(inbox) -> msg => msg.ok().map(Event::Restart),
|
||||||
|
recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_recheck(&mut self) -> bool {
|
||||||
|
if let Some(_last_update_req) = &self.last_update_req {
|
||||||
|
// We currently only request an update on save, as we need up to
|
||||||
|
// date source on disk for cargo check to do it's magic, so we
|
||||||
|
// don't really need to debounce the requests at this point.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn restart_check_process(&mut self) {
|
fn restart_check_process(&mut self) {
|
||||||
// First, clear and cancel the old thread
|
// First, clear and cancel the old thread
|
||||||
self.message_recv = never();
|
|
||||||
self.check_process = None;
|
self.check_process = None;
|
||||||
|
|
||||||
let mut cmd = match &self.config {
|
let mut cmd = match &self.config {
|
||||||
|
@ -237,8 +223,7 @@ impl FlycheckActor {
|
||||||
cmd.current_dir(&self.workspace_root);
|
cmd.current_dir(&self.workspace_root);
|
||||||
|
|
||||||
let (message_send, message_recv) = unbounded();
|
let (message_send, message_recv) = unbounded();
|
||||||
self.message_recv = message_recv;
|
let thread = jod_thread::spawn(move || {
|
||||||
self.check_process = Some(jod_thread::spawn(move || {
|
|
||||||
// If we trigger an error here, we will do so in the loop instead,
|
// If we trigger an error here, we will do so in the loop instead,
|
||||||
// which will break out of the loop, and continue the shutdown
|
// which will break out of the loop, and continue the shutdown
|
||||||
let _ = message_send.send(CheckEvent::Begin);
|
let _ = message_send.send(CheckEvent::Begin);
|
||||||
|
@ -267,7 +252,8 @@ impl FlycheckActor {
|
||||||
// We can ignore any error here, as we are already in the progress
|
// We can ignore any error here, as we are already in the progress
|
||||||
// of shutting down.
|
// of shutting down.
|
||||||
let _ = message_send.send(CheckEvent::End);
|
let _ = message_send.send(CheckEvent::End);
|
||||||
}))
|
});
|
||||||
|
self.check_process = Some((message_recv, thread))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send(&self, check_task: Message) {
|
fn send(&self, check_task: Message) {
|
||||||
|
|
|
@ -83,8 +83,8 @@ impl NotifyActor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(mut self, receiver: Receiver<Message>) {
|
fn run(mut self, inbox: Receiver<Message>) {
|
||||||
while let Some(event) = self.next_event(&receiver) {
|
while let Some(event) = self.next_event(&inbox) {
|
||||||
log::debug!("vfs-notify event: {:?}", event);
|
log::debug!("vfs-notify event: {:?}", event);
|
||||||
match event {
|
match event {
|
||||||
Event::Message(msg) => match msg {
|
Event::Message(msg) => match msg {
|
||||||
|
|
Loading…
Reference in a new issue