Skip to content

Commit

Permalink
feat(editor): Create a command to apply all auto-fixes for the curren…
Browse files Browse the repository at this point in the history
…t active text editor
  • Loading branch information
nrayburn-tech committed Dec 5, 2024
1 parent 6ae178e commit ae0b823
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
45 changes: 44 additions & 1 deletion crates/oxc_language_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ use log::{debug, error, info};
use oxc_linter::{FixKind, LinterBuilder, Oxlintrc};
use rustc_hash::FxBuildHasher;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::borrow::Cow;
use tokio::sync::{Mutex, OnceCell, RwLock, SetError};
use tower_lsp::lsp_types::{NumberOrString, Position, Range};
use tower_lsp::lsp_types::{
ExecuteCommandParams, NumberOrString, Position, Range, VersionedTextDocumentIdentifier,
};
use tower_lsp::{
jsonrpc::{Error, ErrorCode, Result},
lsp_types::{
Expand Down Expand Up @@ -398,6 +402,45 @@ impl LanguageServer for Backend {

Ok(None)
}

async fn execute_command(&self, params: ExecuteCommandParams) -> Result<Option<Value>> {
if params.command == "oxc.applyAllFixes" && params.arguments.len() == 1 {
let text_document =
VersionedTextDocumentIdentifier::deserialize(params.arguments.first().unwrap())
.unwrap();
let uri = text_document.uri;
if let Some(value) = self.diagnostics_report_map.get(&uri.to_string()) {
join_all(value.iter().filter(|report| report.fixed_content.is_some()).map(
|report| async {
let fixed_content = report.fixed_content.as_ref().unwrap();
return self
.client
.apply_edit(WorkspaceEdit {
#[expect(clippy::disallowed_types)]
changes: Some(std::collections::HashMap::from([(
uri.clone(),
vec![TextEdit {
range: fixed_content.range,
new_text: fixed_content.code.clone(),
}],
)])),
..WorkspaceEdit::default()
})
.await;
},
))
.await;

return Ok(None);
};
}

Err(Error {
code: ErrorCode::InvalidRequest,
message: Cow::Owned(format!("Command {} is not supported", params.command)),
data: None,
})
}
}

impl Backend {
Expand Down
1 change: 1 addition & 0 deletions editors/vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ This is the linter for Oxc. The currently supported features are listed below.
- Highlighting for warnings or errors identified by Oxlint
- Quick fixes to fix a warning or error when possible
- JSON schema validation for supported Oxlint configuration files (does not include ESLint configuration files)
- Command to fix all auto-fixable content within the current text editor.
32 changes: 31 additions & 1 deletion editors/vscode/client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { promises as fsPromises } from 'node:fs';

import { commands, ExtensionContext, StatusBarAlignment, StatusBarItem, ThemeColor, window, workspace } from 'vscode';

import { MessageType, ShowMessageNotification } from 'vscode-languageclient';
import {
ExecuteCommandRequest,
MessageType,
ShowMessageNotification,
VersionedTextDocumentIdentifier,
} from 'vscode-languageclient';

import { Executable, LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node';

Expand Down Expand Up @@ -62,7 +67,32 @@ export async function activate(context: ExtensionContext) {
},
);

const applyAllFixes = commands.registerCommand(
OxcCommands.ApplyAllFixes,
async () => {
if (!client) {
window.showErrorMessage('oxc client not found');
return;
}
const textEditor = window.activeTextEditor;
if (!textEditor) {
window.showErrorMessage('active text editor not found');
return;
}
const textDocument: VersionedTextDocumentIdentifier = {
uri: textEditor.document.uri.toString(),
version: textEditor.document.version,
};

await client.sendRequest(ExecuteCommandRequest.type, {
command: OxcCommands.ApplyAllFixes,
arguments: [textDocument],
});
},
);

context.subscriptions.push(
applyAllFixes,
restartCommand,
showOutputCommand,
toggleEnable,
Expand Down
5 changes: 5 additions & 0 deletions editors/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
"command": "oxc.showOutputChannel",
"title": "Show Output Channel",
"category": "Oxc"
},
{
"command": "oxc.applyAllFixes",
"title": "Apply all fixes",
"category": "Oxc"
}
],
"configuration": {
Expand Down

0 comments on commit ae0b823

Please sign in to comment.