Skip to content
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
100 changes: 73 additions & 27 deletions crates/ruff_server/src/fix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use ruff_python_ast::SourceType;
use ruff_python_ast::{SourceType, TomlSourceType};
use rustc_hash::FxHashMap;

use crate::{
Expand All @@ -14,6 +14,7 @@ use ruff_linter::{
linter::FixerResult,
packaging::detect_package_root,
settings::{LinterSettings, flags},
toml::lint_fix_toml,
};
use ruff_notebook::SourceValue;
use ruff_source_file::LineIndex;
Expand All @@ -30,11 +31,6 @@ pub(crate) fn fix_all(
let settings = query.settings();
let document_path = query.virtual_file_path();

let SourceType::Python(source_type) = query.source_type_for_lint() else {
return Ok(Fixes::default());
};
let source_kind = query.make_python_source_kind(source_type);

// If the document is excluded, return an empty list of fixes.
if is_document_excluded_for_linting(
&document_path,
Expand All @@ -45,6 +41,15 @@ pub(crate) fn fix_all(
return Ok(Fixes::default());
}

let source_type = match query.source_type_for_lint() {
SourceType::Python(source_type) => source_type,
SourceType::Toml(source_type @ (TomlSourceType::Pyproject | TomlSourceType::Ruff)) => {
return fix_toml(query, linter_settings, source_type, encoding);
}
SourceType::Toml(_) | SourceType::Markdown => return Ok(Fixes::default()),
};
let source_kind = query.make_python_source_kind(source_type);

let file_path = query.file_path();
let package = if let Some(file_path) = &file_path {
detect_package_root(
Expand Down Expand Up @@ -132,28 +137,69 @@ pub(crate) fn fix_all(
}
Ok(fixes)
} else {
let source_index = LineIndex::from_source_text(source_kind.source_code());
Ok(text_document_fixes(
query,
source_kind.source_code(),
transformed.source_code(),
encoding,
))
}
}

let modified = transformed.source_code();
let modified_index = LineIndex::from_source_text(modified);
fn fix_toml(
query: &DocumentQuery,
linter_settings: &LinterSettings,
source_type: TomlSourceType,
encoding: PositionEncoding,
) -> crate::Result<Fixes> {
let document = query.as_single_document()?;
let transformed = lint_fix_toml(
&query.virtual_file_path(),
document.contents(),
linter_settings,
source_type,
query.settings().unsafe_fixes,
)
.transformed;

let Replacement {
source_range,
modified_range,
} = Replacement::between(
source_kind.source_code(),
source_index.line_starts(),
modified,
modified_index.line_starts(),
);
Ok([(
query.make_key().into_uri(),
vec![lsp_types::TextEdit {
range: source_range.to_range(source_kind.source_code(), &source_index, encoding),
new_text: modified[modified_range].to_owned(),
}],
)]
.into_iter()
.collect())
if let Cow::Borrowed(_) = transformed {
return Ok(Fixes::default());
}

Ok(text_document_fixes(
query,
document.contents(),
transformed.as_ref(),
encoding,
))
}

fn text_document_fixes(
query: &DocumentQuery,
source: &str,
modified: &str,
encoding: PositionEncoding,
) -> Fixes {
let source_index = LineIndex::from_source_text(source);
let modified_index = LineIndex::from_source_text(modified);

let Replacement {
source_range,
modified_range,
} = Replacement::between(
source,
source_index.line_starts(),
modified,
modified_index.line_starts(),
);

[(
query.make_key().into_uri(),
vec![lsp_types::TextEdit {
range: source_range.to_range(source, &source_index, encoding),
new_text: modified[modified_range].to_owned(),
}],
)]
.into_iter()
.collect()
}
Loading
Loading