Skip to content

Add a command to clear flycheck diagnostics #13792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,11 +1125,8 @@ impl Config {
}
}

pub fn flycheck(&self) -> Option<FlycheckConfig> {
if !self.data.checkOnSave_enable {
return None;
}
let flycheck_config = match &self.data.checkOnSave_overrideCommand {
pub fn flycheck(&self) -> FlycheckConfig {
match &self.data.checkOnSave_overrideCommand {
Some(args) if !args.is_empty() => {
let mut args = args.clone();
let command = args.remove(0);
Expand Down Expand Up @@ -1183,8 +1180,11 @@ impl Config {
extra_args: self.data.checkOnSave_extraArgs.clone(),
extra_env: self.check_on_save_extra_env(),
},
};
Some(flycheck_config)
}
}

pub fn check_on_save(&self) -> bool {
self.data.checkOnSave_enable
}

pub fn runnables(&self) -> RunnablesConfig {
Expand Down
10 changes: 8 additions & 2 deletions crates/rust-analyzer/src/lsp_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ pub struct ExpandedMacro {

pub enum CancelFlycheck {}

impl Request for CancelFlycheck {
impl Notification for CancelFlycheck {
type Params = ();
type Result = ();
const METHOD: &'static str = "rust-analyzer/cancelFlycheck";
}

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

pub enum ClearFlycheck {}

impl Notification for ClearFlycheck {
type Params = ();
const METHOD: &'static str = "rust-analyzer/clearFlycheck";
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RunFlycheckParams {
Expand Down
27 changes: 14 additions & 13 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,7 @@ impl GlobalState {
// When we're running multiple flychecks, we have to include a disambiguator in
// the title, or the editor complains. Note that this is a user-facing string.
let title = if self.flycheck.len() == 1 {
match self.config.flycheck() {
Some(config) => format!("{}", config),
None => "cargo check".to_string(),
}
format!("{}", self.config.flycheck())
} else {
format!("cargo check (#{})", id + 1)
};
Expand All @@ -593,7 +590,7 @@ impl GlobalState {
state,
message,
None,
Some(format!("rust-analyzer/checkOnSave/{}", id)),
Some(format!("rust-analyzer/flycheck/{}", id)),
);
}
}
Expand Down Expand Up @@ -638,7 +635,6 @@ impl GlobalState {
.on_sync_mut::<lsp_ext::ReloadWorkspace>(handlers::handle_workspace_reload)
.on_sync_mut::<lsp_ext::MemoryUsage>(handlers::handle_memory_usage)
.on_sync_mut::<lsp_ext::ShuffleCrateGraph>(handlers::handle_shuffle_crate_graph)
.on_sync_mut::<lsp_ext::CancelFlycheck>(handlers::handle_cancel_flycheck)
.on_sync::<lsp_ext::JoinLines>(handlers::handle_join_lines)
.on_sync::<lsp_ext::OnEnter>(handlers::handle_on_enter)
.on_sync::<lsp_types::request::SelectionRangeRequest>(handlers::handle_selection_range)
Expand Down Expand Up @@ -796,7 +792,7 @@ impl GlobalState {
})?
.on::<lsp_types::notification::WorkDoneProgressCancel>(|this, params| {
if let lsp_types::NumberOrString::String(s) = &params.token {
if let Some(id) = s.strip_prefix("rust-analyzer/checkOnSave/") {
if let Some(id) = s.strip_prefix("rust-analyzer/flycheck/") {
if let Ok(id) = u32::from_str_radix(id, 10) {
if let Some(flycheck) = this.flycheck.get(id as usize) {
flycheck.cancel();
Expand Down Expand Up @@ -825,6 +821,7 @@ impl GlobalState {
}
Ok(())
})?
.on::<lsp_ext::CancelFlycheck>(handlers::handle_cancel_flycheck)?
.on::<lsp_types::notification::DidChangeTextDocument>(|this, params| {
if let Ok(path) = from_proto::vfs_path(&params.text_document.uri) {
match this.mem_docs.get_mut(&path) {
Expand Down Expand Up @@ -864,6 +861,10 @@ impl GlobalState {
}
Ok(())
})?
.on::<lsp_ext::ClearFlycheck>(|this, ()| {
this.diagnostics.clear_check_all();
Ok(())
})?
.on::<lsp_ext::RunFlycheck>(|this, params| {
if let Some(text_document) = params.text_document {
if let Ok(vfs_path) = from_proto::vfs_path(&text_document.uri) {
Expand All @@ -888,14 +889,14 @@ impl GlobalState {
}
}

if run_flycheck(this, vfs_path) {
if !this.config.check_on_save() || run_flycheck(this, vfs_path) {
return Ok(());
}
}

// No specific flycheck was triggered, so let's trigger all of them.
for flycheck in this.flycheck.iter() {
flycheck.restart();
} else if this.config.check_on_save() {
// No specific flycheck was triggered, so let's trigger all of them.
for flycheck in this.flycheck.iter() {
flycheck.restart();
}
}
Ok(())
})?
Expand Down
10 changes: 1 addition & 9 deletions crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,7 @@ impl GlobalState {

fn reload_flycheck(&mut self) {
let _p = profile::span("GlobalState::reload_flycheck");
let config = match self.config.flycheck() {
Some(it) => it,
None => {
self.flycheck = Arc::new([]);
self.diagnostics.clear_check_all();
return;
}
};

let config = self.config.flycheck();
let sender = self.flycheck_sender.clone();
let invocation_strategy = match config {
FlycheckConfig::CargoCommand { .. } => flycheck::InvocationStrategy::PerWorkspace,
Expand Down
41 changes: 40 additions & 1 deletion docs/dev/lsp-extensions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!---
lsp_ext.rs hash: 1cb29d3afa36e743
lsp_ext.rs hash: 45bd7985265725c5

If you need to change the above hash to make the test pass, please check if you
need to adjust this doc as well and ping this issue:
Expand Down Expand Up @@ -459,6 +459,45 @@ Note that this functionality is intended primarily to inform the end user about
In particular, it's valid for the client to completely ignore this extension.
Clients are discouraged from but are allowed to use the `health` status to decide if it's worth sending a request to the server.

### Controlling Flycheck

The flycheck/checkOnSave feature can be controlled via notifications sent by the client to the server.

**Method:** `rust-analyzer/runFlycheck`

**Notification:**

```typescript
interface RunFlycheckParams {
/// The text document whose cargo workspace flycheck process should be started.
/// If the document is null or does not belong to a cargo workspace all flycheck processes will be started.
textDocument: lc.TextDocumentIdentifier | null;
}
```

Triggers the flycheck processes.


**Method:** `rust-analyzer/clearFlycheck`

**Notification:**

```typescript
interface ClearFlycheckParams {}
```

Clears the flycheck diagnostics.

**Method:** `rust-analyzer/cancelFlycheck`

**Notification:**

```typescript
interface CancelFlycheckParams {}
```

Cancels all running flycheck processes.

## Syntax Tree

**Method:** `rust-analyzer/syntaxTree`
Expand Down
5 changes: 5 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@
"command": "rust-analyzer.runFlycheck",
"title": "Run flycheck",
"category": "rust-analyzer"
},
{
"command": "rust-analyzer.clearFlycheck",
"title": "Clear flycheck diagnostics",
"category": "rust-analyzer"
}
],
"keybindings": [
Expand Down
8 changes: 7 additions & 1 deletion editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,13 @@ export function openDocs(ctx: CtxInit): Cmd {

export function cancelFlycheck(ctx: CtxInit): Cmd {
return async () => {
await ctx.client.sendRequest(ra.cancelFlycheck);
await ctx.client.sendNotification(ra.cancelFlycheck);
};
}

export function clearFlycheck(ctx: CtxInit): Cmd {
return async () => {
await ctx.client.sendNotification(ra.clearFlycheck);
};
}

Expand Down
4 changes: 2 additions & 2 deletions editors/code/src/lsp_ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export const relatedTests = new lc.RequestType<lc.TextDocumentPositionParams, Te
"rust-analyzer/relatedTests"
);

export const cancelFlycheck = new lc.RequestType0<void, void>("rust-analyzer/cancelFlycheck");

export const cancelFlycheck = new lc.NotificationType0("rust-analyzer/cancelFlycheck");
export const clearFlycheck = new lc.NotificationType0("rust-analyzer/clearFlycheck");
export const runFlycheck = new lc.NotificationType<{
textDocument: lc.TextDocumentIdentifier | null;
}>("rust-analyzer/runFlycheck");
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function createCommands(): Record<string, CommandFactory> {
moveItemUp: { enabled: commands.moveItemUp },
moveItemDown: { enabled: commands.moveItemDown },
cancelFlycheck: { enabled: commands.cancelFlycheck },
clearFlycheck: { enabled: commands.clearFlycheck },
runFlycheck: { enabled: commands.runFlycheck },
ssr: { enabled: commands.ssr },
serverVersion: { enabled: commands.serverVersion },
Expand Down