Skip to content

Commit 04c487d

Browse files
committed
Auto merge of rust-lang#13063 - Veykril:stop-flycheck, r=Veykril
Implement lsp extension for cancelling running flychecks Fixes rust-lang/rust-analyzer#4828
2 parents 917bd68 + 45b7b6a commit 04c487d

File tree

9 files changed

+69
-22
lines changed

9 files changed

+69
-22
lines changed

crates/flycheck/src/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ impl FlycheckHandle {
7777
}
7878

7979
/// Schedule a re-start of the cargo check worker.
80-
pub fn update(&self) {
81-
self.sender.send(Restart).unwrap();
80+
pub fn restart(&self) {
81+
self.sender.send(Restart::Yes).unwrap();
82+
}
83+
84+
/// Stop this cargo check worker.
85+
pub fn cancel(&self) {
86+
self.sender.send(Restart::No).unwrap();
8287
}
8388

8489
pub fn id(&self) -> usize {
@@ -122,7 +127,10 @@ pub enum Progress {
122127
DidCancel,
123128
}
124129

125-
struct Restart;
130+
enum Restart {
131+
Yes,
132+
No,
133+
}
126134

127135
struct FlycheckActor {
128136
id: usize,
@@ -149,6 +157,7 @@ impl FlycheckActor {
149157
config: FlycheckConfig,
150158
workspace_root: AbsPathBuf,
151159
) -> FlycheckActor {
160+
tracing::info!(%id, ?workspace_root, "Spawning flycheck");
152161
FlycheckActor { id, sender, config, workspace_root, cargo_handle: None }
153162
}
154163
fn progress(&self, progress: Progress) {
@@ -164,10 +173,13 @@ impl FlycheckActor {
164173
fn run(mut self, inbox: Receiver<Restart>) {
165174
while let Some(event) = self.next_event(&inbox) {
166175
match event {
167-
Event::Restart(Restart) => {
176+
Event::Restart(Restart::No) => {
177+
self.cancel_check_process();
178+
}
179+
Event::Restart(Restart::Yes) => {
168180
// Cancel the previously spawned process
169181
self.cancel_check_process();
170-
while let Ok(Restart) = inbox.recv_timeout(Duration::from_millis(50)) {}
182+
while let Ok(_) = inbox.recv_timeout(Duration::from_millis(50)) {}
171183

172184
let command = self.check_command();
173185
tracing::debug!(?command, "will restart flycheck");
@@ -223,6 +235,10 @@ impl FlycheckActor {
223235

224236
fn cancel_check_process(&mut self) {
225237
if let Some(cargo_handle) = self.cargo_handle.take() {
238+
tracing::debug!(
239+
command = ?self.check_command(),
240+
"did cancel flycheck"
241+
);
226242
cargo_handle.cancel();
227243
self.progress(Progress::DidCancel);
228244
}

crates/rust-analyzer/src/handlers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ pub(crate) fn handle_workspace_reload(state: &mut GlobalState, _: ()) -> Result<
5151
Ok(())
5252
}
5353

54+
pub(crate) fn handle_cancel_flycheck(state: &mut GlobalState, _: ()) -> Result<()> {
55+
let _p = profile::span("handle_stop_flycheck");
56+
state.flycheck.iter().for_each(|flycheck| flycheck.cancel());
57+
Ok(())
58+
}
59+
5460
pub(crate) fn handle_analyzer_status(
5561
snap: GlobalStateSnapshot,
5662
params: lsp_ext::AnalyzerStatusParams,

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ pub struct ExpandedMacro {
129129
pub expansion: String,
130130
}
131131

132+
pub enum CancelFlycheck {}
133+
134+
impl Request for CancelFlycheck {
135+
type Params = ();
136+
type Result = ();
137+
const METHOD: &'static str = "rust-analyzer/cancelFlycheck";
138+
}
139+
132140
pub enum MatchingBrace {}
133141

134142
impl Request for MatchingBrace {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl GlobalState {
288288

289289
if became_quiescent {
290290
// Project has loaded properly, kick off initial flycheck
291-
self.flycheck.iter().for_each(FlycheckHandle::update);
291+
self.flycheck.iter().for_each(FlycheckHandle::restart);
292292
if self.config.prefill_caches() {
293293
self.prime_caches_queue.request_op("became quiescent".to_string());
294294
}
@@ -590,6 +590,7 @@ impl GlobalState {
590590
.on_sync_mut::<lsp_ext::ReloadWorkspace>(handlers::handle_workspace_reload)
591591
.on_sync_mut::<lsp_ext::MemoryUsage>(handlers::handle_memory_usage)
592592
.on_sync_mut::<lsp_ext::ShuffleCrateGraph>(handlers::handle_shuffle_crate_graph)
593+
.on_sync_mut::<lsp_ext::CancelFlycheck>(handlers::handle_cancel_flycheck)
593594
.on_sync::<lsp_ext::JoinLines>(handlers::handle_join_lines)
594595
.on_sync::<lsp_ext::OnEnter>(handlers::handle_on_enter)
595596
.on_sync::<lsp_types::request::SelectionRangeRequest>(handlers::handle_selection_range)
@@ -779,7 +780,7 @@ impl GlobalState {
779780
for (id, _) in workspace_ids.clone() {
780781
if id == flycheck.id() {
781782
updated = true;
782-
flycheck.update();
783+
flycheck.restart();
783784
continue;
784785
}
785786
}
@@ -798,7 +799,7 @@ impl GlobalState {
798799
// No specific flycheck was triggered, so let's trigger all of them.
799800
if !updated {
800801
for flycheck in &this.flycheck {
801-
flycheck.update();
802+
flycheck.restart();
802803
}
803804
}
804805
Ok(())

docs/dev/lsp-extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp_ext.rs hash: 2a188defec26cc7c
2+
lsp_ext.rs hash: 7b710095d773b978
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@
235235
"command": "rust-analyzer.moveItemDown",
236236
"title": "Move item down",
237237
"category": "rust-analyzer"
238+
},
239+
{
240+
"command": "rust-analyzer.cancelFlycheck",
241+
"title": "Cancel running flychecks",
242+
"category": "rust-analyzer"
238243
}
239244
],
240245
"keybindings": [

editors/code/src/commands.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,12 @@ export function openDocs(ctx: Ctx): Cmd {
817817
};
818818
}
819819

820+
export function cancelFlycheck(ctx: Ctx): Cmd {
821+
return async () => {
822+
await ctx.client.sendRequest(ra.cancelFlycheck);
823+
};
824+
}
825+
820826
export function resolveCodeAction(ctx: Ctx): Cmd {
821827
const client = ctx.client;
822828
return async (params: lc.CodeAction) => {

editors/code/src/lsp_ext.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ export const expandMacro = new lc.RequestType<ExpandMacroParams, ExpandedMacro |
7575
"rust-analyzer/expandMacro"
7676
);
7777

78+
export const relatedTests = new lc.RequestType<lc.TextDocumentPositionParams, TestInfo[], void>(
79+
"rust-analyzer/relatedTests"
80+
);
81+
82+
export const cancelFlycheck = new lc.RequestType0<void, void>("rust-analyzer/cancelFlycheck");
83+
84+
// Experimental extensions
85+
86+
export interface SsrParams {
87+
query: string;
88+
parseOnly: boolean;
89+
textDocument: lc.TextDocumentIdentifier;
90+
position: lc.Position;
91+
selections: readonly lc.Range[];
92+
}
93+
export const ssr = new lc.RequestType<SsrParams, lc.WorkspaceEdit, void>("experimental/ssr");
94+
7895
export interface MatchingBraceParams {
7996
textDocument: lc.TextDocumentIdentifier;
8097
positions: lc.Position[];
@@ -127,19 +144,6 @@ export interface TestInfo {
127144
runnable: Runnable;
128145
}
129146

130-
export const relatedTests = new lc.RequestType<lc.TextDocumentPositionParams, TestInfo[], void>(
131-
"rust-analyzer/relatedTests"
132-
);
133-
134-
export interface SsrParams {
135-
query: string;
136-
parseOnly: boolean;
137-
textDocument: lc.TextDocumentIdentifier;
138-
position: lc.Position;
139-
selections: readonly lc.Range[];
140-
}
141-
export const ssr = new lc.RequestType<SsrParams, lc.WorkspaceEdit, void>("experimental/ssr");
142-
143147
export interface CommandLink extends lc.Command {
144148
/**
145149
* A tooltip for the command, when represented in the UI.

editors/code/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) {
163163
ctx.registerCommand("peekTests", commands.peekTests);
164164
ctx.registerCommand("moveItemUp", commands.moveItemUp);
165165
ctx.registerCommand("moveItemDown", commands.moveItemDown);
166+
ctx.registerCommand("cancelFlycheck", commands.cancelFlycheck);
166167

167168
defaultOnEnter.dispose();
168169
ctx.registerCommand("onEnter", commands.onEnter);

0 commit comments

Comments
 (0)