Implement lsp extension for cancelling running flychecks

This commit is contained in:
Lukas Wirth 2022-08-19 08:52:31 +02:00
parent 917bd68b37
commit 45b7b6a60a
9 changed files with 69 additions and 22 deletions

View file

@ -77,8 +77,13 @@ impl FlycheckHandle {
}
/// Schedule a re-start of the cargo check worker.
pub fn update(&self) {
self.sender.send(Restart).unwrap();
pub fn restart(&self) {
self.sender.send(Restart::Yes).unwrap();
}
/// Stop this cargo check worker.
pub fn cancel(&self) {
self.sender.send(Restart::No).unwrap();
}
pub fn id(&self) -> usize {
@ -122,7 +127,10 @@ pub enum Progress {
DidCancel,
}
struct Restart;
enum Restart {
Yes,
No,
}
struct FlycheckActor {
id: usize,
@ -149,6 +157,7 @@ impl FlycheckActor {
config: FlycheckConfig,
workspace_root: AbsPathBuf,
) -> FlycheckActor {
tracing::info!(%id, ?workspace_root, "Spawning flycheck");
FlycheckActor { id, sender, config, workspace_root, cargo_handle: None }
}
fn progress(&self, progress: Progress) {
@ -164,10 +173,13 @@ impl FlycheckActor {
fn run(mut self, inbox: Receiver<Restart>) {
while let Some(event) = self.next_event(&inbox) {
match event {
Event::Restart(Restart) => {
Event::Restart(Restart::No) => {
self.cancel_check_process();
}
Event::Restart(Restart::Yes) => {
// Cancel the previously spawned process
self.cancel_check_process();
while let Ok(Restart) = inbox.recv_timeout(Duration::from_millis(50)) {}
while let Ok(_) = inbox.recv_timeout(Duration::from_millis(50)) {}
let command = self.check_command();
tracing::debug!(?command, "will restart flycheck");
@ -223,6 +235,10 @@ impl FlycheckActor {
fn cancel_check_process(&mut self) {
if let Some(cargo_handle) = self.cargo_handle.take() {
tracing::debug!(
command = ?self.check_command(),
"did cancel flycheck"
);
cargo_handle.cancel();
self.progress(Progress::DidCancel);
}