Skip to content

Commit 9ed1829

Browse files
committed
Auto merge of #13792 - Veykril:flycheck, r=Veykril
Add a command to clear flycheck diagnostics And document the flycheck notifications
2 parents e0aa5af + cf8d89e commit 9ed1829

File tree

9 files changed

+85
-35
lines changed

9 files changed

+85
-35
lines changed

crates/rust-analyzer/src/config.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,8 @@ impl Config {
11251125
}
11261126
}
11271127

1128-
pub fn flycheck(&self) -> Option<FlycheckConfig> {
1129-
if !self.data.checkOnSave_enable {
1130-
return None;
1131-
}
1132-
let flycheck_config = match &self.data.checkOnSave_overrideCommand {
1128+
pub fn flycheck(&self) -> FlycheckConfig {
1129+
match &self.data.checkOnSave_overrideCommand {
11331130
Some(args) if !args.is_empty() => {
11341131
let mut args = args.clone();
11351132
let command = args.remove(0);
@@ -1183,8 +1180,11 @@ impl Config {
11831180
extra_args: self.data.checkOnSave_extraArgs.clone(),
11841181
extra_env: self.check_on_save_extra_env(),
11851182
},
1186-
};
1187-
Some(flycheck_config)
1183+
}
1184+
}
1185+
1186+
pub fn check_on_save(&self) -> bool {
1187+
self.data.checkOnSave_enable
11881188
}
11891189

11901190
pub fn runnables(&self) -> RunnablesConfig {

crates/rust-analyzer/src/lsp_ext.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ pub struct ExpandedMacro {
132132

133133
pub enum CancelFlycheck {}
134134

135-
impl Request for CancelFlycheck {
135+
impl Notification for CancelFlycheck {
136136
type Params = ();
137-
type Result = ();
138137
const METHOD: &'static str = "rust-analyzer/cancelFlycheck";
139138
}
140139

@@ -145,6 +144,13 @@ impl Notification for RunFlycheck {
145144
const METHOD: &'static str = "rust-analyzer/runFlycheck";
146145
}
147146

147+
pub enum ClearFlycheck {}
148+
149+
impl Notification for ClearFlycheck {
150+
type Params = ();
151+
const METHOD: &'static str = "rust-analyzer/clearFlycheck";
152+
}
153+
148154
#[derive(Deserialize, Serialize, Debug)]
149155
#[serde(rename_all = "camelCase")]
150156
pub struct RunFlycheckParams {

crates/rust-analyzer/src/main_loop.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,7 @@ impl GlobalState {
581581
// When we're running multiple flychecks, we have to include a disambiguator in
582582
// the title, or the editor complains. Note that this is a user-facing string.
583583
let title = if self.flycheck.len() == 1 {
584-
match self.config.flycheck() {
585-
Some(config) => format!("{}", config),
586-
None => "cargo check".to_string(),
587-
}
584+
format!("{}", self.config.flycheck())
588585
} else {
589586
format!("cargo check (#{})", id + 1)
590587
};
@@ -593,7 +590,7 @@ impl GlobalState {
593590
state,
594591
message,
595592
None,
596-
Some(format!("rust-analyzer/checkOnSave/{}", id)),
593+
Some(format!("rust-analyzer/flycheck/{}", id)),
597594
);
598595
}
599596
}
@@ -638,7 +635,6 @@ impl GlobalState {
638635
.on_sync_mut::<lsp_ext::ReloadWorkspace>(handlers::handle_workspace_reload)
639636
.on_sync_mut::<lsp_ext::MemoryUsage>(handlers::handle_memory_usage)
640637
.on_sync_mut::<lsp_ext::ShuffleCrateGraph>(handlers::handle_shuffle_crate_graph)
641-
.on_sync_mut::<lsp_ext::CancelFlycheck>(handlers::handle_cancel_flycheck)
642638
.on_sync::<lsp_ext::JoinLines>(handlers::handle_join_lines)
643639
.on_sync::<lsp_ext::OnEnter>(handlers::handle_on_enter)
644640
.on_sync::<lsp_types::request::SelectionRangeRequest>(handlers::handle_selection_range)
@@ -796,7 +792,7 @@ impl GlobalState {
796792
})?
797793
.on::<lsp_types::notification::WorkDoneProgressCancel>(|this, params| {
798794
if let lsp_types::NumberOrString::String(s) = &params.token {
799-
if let Some(id) = s.strip_prefix("rust-analyzer/checkOnSave/") {
795+
if let Some(id) = s.strip_prefix("rust-analyzer/flycheck/") {
800796
if let Ok(id) = u32::from_str_radix(id, 10) {
801797
if let Some(flycheck) = this.flycheck.get(id as usize) {
802798
flycheck.cancel();
@@ -825,6 +821,7 @@ impl GlobalState {
825821
}
826822
Ok(())
827823
})?
824+
.on::<lsp_ext::CancelFlycheck>(handlers::handle_cancel_flycheck)?
828825
.on::<lsp_types::notification::DidChangeTextDocument>(|this, params| {
829826
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
830827
match this.mem_docs.get_mut(&path) {
@@ -864,6 +861,10 @@ impl GlobalState {
864861
}
865862
Ok(())
866863
})?
864+
.on::<lsp_ext::ClearFlycheck>(|this, ()| {
865+
this.diagnostics.clear_check_all();
866+
Ok(())
867+
})?
867868
.on::<lsp_ext::RunFlycheck>(|this, params| {
868869
if let Some(text_document) = params.text_document {
869870
if let Ok(vfs_path) = from_proto::vfs_path(&text_document.uri) {
@@ -888,14 +889,14 @@ impl GlobalState {
888889
}
889890
}
890891

891-
if run_flycheck(this, vfs_path) {
892+
if !this.config.check_on_save() || run_flycheck(this, vfs_path) {
892893
return Ok(());
893894
}
894-
}
895-
896-
// No specific flycheck was triggered, so let's trigger all of them.
897-
for flycheck in this.flycheck.iter() {
898-
flycheck.restart();
895+
} else if this.config.check_on_save() {
896+
// No specific flycheck was triggered, so let's trigger all of them.
897+
for flycheck in this.flycheck.iter() {
898+
flycheck.restart();
899+
}
899900
}
900901
Ok(())
901902
})?

crates/rust-analyzer/src/reload.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,7 @@ impl GlobalState {
449449

450450
fn reload_flycheck(&mut self) {
451451
let _p = profile::span("GlobalState::reload_flycheck");
452-
let config = match self.config.flycheck() {
453-
Some(it) => it,
454-
None => {
455-
self.flycheck = Arc::new([]);
456-
self.diagnostics.clear_check_all();
457-
return;
458-
}
459-
};
460-
452+
let config = self.config.flycheck();
461453
let sender = self.flycheck_sender.clone();
462454
let invocation_strategy = match config {
463455
FlycheckConfig::CargoCommand { .. } => flycheck::InvocationStrategy::PerWorkspace,

docs/dev/lsp-extensions.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp_ext.rs hash: 1cb29d3afa36e743
2+
lsp_ext.rs hash: 45bd7985265725c5
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:
@@ -459,6 +459,45 @@ Note that this functionality is intended primarily to inform the end user about
459459
In particular, it's valid for the client to completely ignore this extension.
460460
Clients are discouraged from but are allowed to use the `health` status to decide if it's worth sending a request to the server.
461461

462+
### Controlling Flycheck
463+
464+
The flycheck/checkOnSave feature can be controlled via notifications sent by the client to the server.
465+
466+
**Method:** `rust-analyzer/runFlycheck`
467+
468+
**Notification:**
469+
470+
```typescript
471+
interface RunFlycheckParams {
472+
/// The text document whose cargo workspace flycheck process should be started.
473+
/// If the document is null or does not belong to a cargo workspace all flycheck processes will be started.
474+
textDocument: lc.TextDocumentIdentifier | null;
475+
}
476+
```
477+
478+
Triggers the flycheck processes.
479+
480+
481+
**Method:** `rust-analyzer/clearFlycheck`
482+
483+
**Notification:**
484+
485+
```typescript
486+
interface ClearFlycheckParams {}
487+
```
488+
489+
Clears the flycheck diagnostics.
490+
491+
**Method:** `rust-analyzer/cancelFlycheck`
492+
493+
**Notification:**
494+
495+
```typescript
496+
interface CancelFlycheckParams {}
497+
```
498+
499+
Cancels all running flycheck processes.
500+
462501
## Syntax Tree
463502

464503
**Method:** `rust-analyzer/syntaxTree`

editors/code/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@
251251
"command": "rust-analyzer.runFlycheck",
252252
"title": "Run flycheck",
253253
"category": "rust-analyzer"
254+
},
255+
{
256+
"command": "rust-analyzer.clearFlycheck",
257+
"title": "Clear flycheck diagnostics",
258+
"category": "rust-analyzer"
254259
}
255260
],
256261
"keybindings": [

editors/code/src/commands.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,13 @@ export function openDocs(ctx: CtxInit): Cmd {
788788

789789
export function cancelFlycheck(ctx: CtxInit): Cmd {
790790
return async () => {
791-
await ctx.client.sendRequest(ra.cancelFlycheck);
791+
await ctx.client.sendNotification(ra.cancelFlycheck);
792+
};
793+
}
794+
795+
export function clearFlycheck(ctx: CtxInit): Cmd {
796+
return async () => {
797+
await ctx.client.sendNotification(ra.clearFlycheck);
792798
};
793799
}
794800

editors/code/src/lsp_ext.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export const relatedTests = new lc.RequestType<lc.TextDocumentPositionParams, Te
7979
"rust-analyzer/relatedTests"
8080
);
8181

82-
export const cancelFlycheck = new lc.RequestType0<void, void>("rust-analyzer/cancelFlycheck");
83-
82+
export const cancelFlycheck = new lc.NotificationType0("rust-analyzer/cancelFlycheck");
83+
export const clearFlycheck = new lc.NotificationType0("rust-analyzer/clearFlycheck");
8484
export const runFlycheck = new lc.NotificationType<{
8585
textDocument: lc.TextDocumentIdentifier | null;
8686
}>("rust-analyzer/runFlycheck");

editors/code/src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ function createCommands(): Record<string, CommandFactory> {
150150
moveItemUp: { enabled: commands.moveItemUp },
151151
moveItemDown: { enabled: commands.moveItemDown },
152152
cancelFlycheck: { enabled: commands.cancelFlycheck },
153+
clearFlycheck: { enabled: commands.clearFlycheck },
153154
runFlycheck: { enabled: commands.runFlycheck },
154155
ssr: { enabled: commands.ssr },
155156
serverVersion: { enabled: commands.serverVersion },

0 commit comments

Comments
 (0)