ruff server now supports commands for auto-fixing, organizing imports, and formatting (#10654)

## Summary

This builds off of the work in
https://github.com/astral-sh/ruff/pull/10652 to implement a command
executor, backwards compatible with the commands from the previous LSP
(`ruff.applyAutofix`, `ruff.applyFormat` and
`ruff.applyOrganizeImports`).

This involved a lot of refactoring and tweaks to the code action
resolution code - the most notable change is that workspace edits are
specified in a slightly different way, using the more general `changes`
field instead of the `document_changes` field (which isn't supported on
all LSP clients). Additionally, the API for synchronous request handlers
has been updated to include access to the `Requester`, which we use to
send a `workspace/applyEdit` request to the client.

## Test Plan



7932e30f-d944-4e35-b828-1d81aa56c087
This commit is contained in:
Jane Lewis 2024-04-05 16:27:35 -07:00 committed by GitHub
parent 1b31d4e9f1
commit c11e6d709c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 379 additions and 96 deletions

View file

@ -35,7 +35,7 @@ pub(crate) struct DiagnosticFix {
pub(crate) fixed_diagnostic: lsp_types::Diagnostic,
pub(crate) title: String,
pub(crate) code: String,
pub(crate) document_edits: Vec<lsp_types::TextDocumentEdit>,
pub(crate) edits: Vec<lsp_types::TextEdit>,
}
pub(crate) fn check(
@ -90,11 +90,9 @@ pub(crate) fn check(
.collect()
}
pub(crate) fn fixes_for_diagnostics<'d>(
document: &'d crate::edit::Document,
url: &'d lsp_types::Url,
pub(crate) fn fixes_for_diagnostics(
document: &crate::edit::Document,
encoding: PositionEncoding,
version: crate::edit::DocumentVersion,
diagnostics: Vec<lsp_types::Diagnostic>,
) -> crate::Result<Vec<DiagnosticFix>> {
diagnostics
@ -118,14 +116,6 @@ pub(crate) fn fixes_for_diagnostics<'d>(
.to_range(document.contents(), document.index(), encoding),
new_text: edit.content().unwrap_or_default().to_string(),
});
let document_edits = vec![lsp_types::TextDocumentEdit {
text_document: lsp_types::OptionalVersionedTextDocumentIdentifier::new(
url.clone(),
version,
),
edits: edits.map(lsp_types::OneOf::Left).collect(),
}];
Ok(Some(DiagnosticFix {
fixed_diagnostic,
code: associated_data.code,
@ -133,7 +123,7 @@ pub(crate) fn fixes_for_diagnostics<'d>(
.kind
.suggestion
.unwrap_or(associated_data.kind.name),
document_edits,
edits: edits.collect(),
}))
})
.filter_map(crate::Result::transpose)