-
-
Notifications
You must be signed in to change notification settings - Fork 862
feat(oxc_language_server): implement oxc.fixAll workspace command
#8858
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d9af89
feat(oxc_language_server): implement oxc.fixAll command
marekvospel e0c248c
test(oxc_language_server): add workspace_edit to vscode, intellij cap…
marekvospel 583df02
chore(vscode): fix all through oxc.fixAll command
marekvospel 3ac3225
[autofix.ci] apply automated fixes
autofix-ci[bot] 76473c7
chore(oxc_language_server): workspace_commands capabilities, enable L…
marekvospel b26c57f
chore: fix clippy warnings
marekvospel 04db8e0
chore(oxc_language_server): get rid of unnecessary clones
marekvospel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| use log::error; | ||
| use serde::Deserialize; | ||
| use tower_lsp::{ | ||
| jsonrpc::{self, Error}, | ||
| lsp_types::{ | ||
| request::ApplyWorkspaceEdit, ApplyWorkspaceEditParams, TextEdit, Url, WorkspaceEdit, | ||
| }, | ||
| }; | ||
|
|
||
| use crate::{capabilities::Capabilities, Backend}; | ||
|
|
||
| pub const LSP_COMMANDS: [WorkspaceCommands; 1] = [WorkspaceCommands::FixAll(FixAllCommand)]; | ||
|
|
||
| pub trait WorkspaceCommand { | ||
| fn command_id(&self) -> String; | ||
| fn available(&self, cap: Capabilities) -> bool; | ||
| type CommandArgs<'a>: serde::Deserialize<'a>; | ||
| async fn execute( | ||
| &self, | ||
| backend: &Backend, | ||
| args: Self::CommandArgs<'_>, | ||
| ) -> jsonrpc::Result<Option<serde_json::Value>>; | ||
| } | ||
|
|
||
| pub enum WorkspaceCommands { | ||
| FixAll(FixAllCommand), | ||
| } | ||
|
|
||
| impl WorkspaceCommands { | ||
| pub fn command_id(&self) -> String { | ||
| match self { | ||
| WorkspaceCommands::FixAll(c) => c.command_id(), | ||
| } | ||
| } | ||
| pub fn available(&self, cap: Capabilities) -> bool { | ||
| match self { | ||
| WorkspaceCommands::FixAll(c) => c.available(cap), | ||
| } | ||
| } | ||
| pub async fn execute( | ||
| &self, | ||
| backend: &Backend, | ||
| args: Vec<serde_json::Value>, | ||
| ) -> jsonrpc::Result<Option<serde_json::Value>> { | ||
| match self { | ||
| WorkspaceCommands::FixAll(c) => { | ||
| let arg: Result< | ||
| <FixAllCommand as WorkspaceCommand>::CommandArgs<'_>, | ||
| serde_json::Error, | ||
| > = serde_json::from_value(serde_json::Value::Array(args)); | ||
| if let Err(e) = arg { | ||
| error!("Invalid args passed to {:?}: {e}", c.command_id()); | ||
| return Err(Error::invalid_request()); | ||
| } | ||
| let arg = arg.unwrap(); | ||
|
|
||
| c.execute(backend, arg).await | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct FixAllCommand; | ||
|
|
||
| #[derive(Deserialize)] | ||
| pub struct FixAllCommandArg { | ||
| uri: String, | ||
| } | ||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| impl WorkspaceCommand for FixAllCommand { | ||
| fn command_id(&self) -> String { | ||
| "oxc.fixAll".into() | ||
| } | ||
| fn available(&self, cap: Capabilities) -> bool { | ||
| cap.workspace_apply_edit | ||
| } | ||
| type CommandArgs<'a> = (FixAllCommandArg,); | ||
|
|
||
| async fn execute( | ||
| &self, | ||
| backend: &Backend, | ||
| args: Self::CommandArgs<'_>, | ||
| ) -> jsonrpc::Result<Option<serde_json::Value>> { | ||
| let url = Url::parse(&args.0.uri); | ||
| if let Err(e) = url { | ||
| error!("Invalid uri passed to {:?}: {e}", self.command_id()); | ||
| return Err(Error::invalid_request()); | ||
| } | ||
| let url = url.unwrap(); | ||
|
|
||
| let mut edits = vec![]; | ||
| if let Some(value) = backend.diagnostics_report_map.get(&url.to_string()) { | ||
| for report in value.iter() { | ||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if let Some(fixed) = &report.fixed_content { | ||
| edits.push(TextEdit { range: fixed.range, new_text: fixed.code.clone() }); | ||
| } | ||
| } | ||
| let _ = backend | ||
| .client | ||
| .send_request::<ApplyWorkspaceEdit>(ApplyWorkspaceEditParams { | ||
| label: Some(match edits.len() { | ||
| 1 => "Oxlint: 1 fix applied".into(), | ||
| n => format!("Oxlint: {n} fixes applied"), | ||
| }), | ||
| edit: WorkspaceEdit { | ||
| #[expect(clippy::disallowed_types)] | ||
| changes: Some(std::collections::HashMap::from([(url, edits)])), | ||
| ..WorkspaceEdit::default() | ||
| }, | ||
| }) | ||
| .await; | ||
| } | ||
|
|
||
| Ok(None) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.