Skip to content

Commit 0d80497

Browse files
authored
Lint TOML files in the LSP (#26862)
Summary -- This PR adds TOML linting and fixing support to the LSP following #26772. It's organized as 3 initial refactoring commits followed by the two commits actually adding TOML support. After this lands, we'll also need to add TOML support in the VS Code extension, like we did for Markdown files in astral-sh/ruff-vscode#950. Test Plan -- New e2e tests
1 parent d91586b commit 0d80497

5 files changed

Lines changed: 471 additions & 126 deletions

File tree

‎crates/ruff_server/src/fix.rs‎

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use ruff_python_ast::SourceType;
3+
use ruff_python_ast::{SourceType, TomlSourceType};
44
use rustc_hash::FxHashMap;
55

66
use crate::{
@@ -14,6 +14,7 @@ use ruff_linter::{
1414
linter::FixerResult,
1515
packaging::detect_package_root,
1616
settings::{LinterSettings, flags},
17+
toml::lint_fix_toml,
1718
};
1819
use ruff_notebook::SourceValue;
1920
use ruff_source_file::LineIndex;
@@ -30,11 +31,6 @@ pub(crate) fn fix_all(
3031
let settings = query.settings();
3132
let document_path = query.virtual_file_path();
3233

33-
let SourceType::Python(source_type) = query.source_type_for_lint() else {
34-
return Ok(Fixes::default());
35-
};
36-
let source_kind = query.make_python_source_kind(source_type);
37-
3834
// If the document is excluded, return an empty list of fixes.
3935
if is_document_excluded_for_linting(
4036
&document_path,
@@ -45,6 +41,15 @@ pub(crate) fn fix_all(
4541
return Ok(Fixes::default());
4642
}
4743

44+
let source_type = match query.source_type_for_lint() {
45+
SourceType::Python(source_type) => source_type,
46+
SourceType::Toml(source_type @ (TomlSourceType::Pyproject | TomlSourceType::Ruff)) => {
47+
return fix_toml(query, linter_settings, source_type, encoding);
48+
}
49+
SourceType::Toml(_) | SourceType::Markdown => return Ok(Fixes::default()),
50+
};
51+
let source_kind = query.make_python_source_kind(source_type);
52+
4853
let file_path = query.file_path();
4954
let package = if let Some(file_path) = &file_path {
5055
detect_package_root(
@@ -132,28 +137,69 @@ pub(crate) fn fix_all(
132137
}
133138
Ok(fixes)
134139
} else {
135-
let source_index = LineIndex::from_source_text(source_kind.source_code());
140+
Ok(text_document_fixes(
141+
query,
142+
source_kind.source_code(),
143+
transformed.source_code(),
144+
encoding,
145+
))
146+
}
147+
}
136148

137-
let modified = transformed.source_code();
138-
let modified_index = LineIndex::from_source_text(modified);
149+
fn fix_toml(
150+
query: &DocumentQuery,
151+
linter_settings: &LinterSettings,
152+
source_type: TomlSourceType,
153+
encoding: PositionEncoding,
154+
) -> crate::Result<Fixes> {
155+
let document = query.as_single_document()?;
156+
let transformed = lint_fix_toml(
157+
&query.virtual_file_path(),
158+
document.contents(),
159+
linter_settings,
160+
source_type,
161+
query.settings().unsafe_fixes,
162+
)
163+
.transformed;
139164

140-
let Replacement {
141-
source_range,
142-
modified_range,
143-
} = Replacement::between(
144-
source_kind.source_code(),
145-
source_index.line_starts(),
146-
modified,
147-
modified_index.line_starts(),
148-
);
149-
Ok([(
150-
query.make_key().into_uri(),
151-
vec![lsp_types::TextEdit {
152-
range: source_range.to_range(source_kind.source_code(), &source_index, encoding),
153-
new_text: modified[modified_range].to_owned(),
154-
}],
155-
)]
156-
.into_iter()
157-
.collect())
165+
if let Cow::Borrowed(_) = transformed {
166+
return Ok(Fixes::default());
158167
}
168+
169+
Ok(text_document_fixes(
170+
query,
171+
document.contents(),
172+
transformed.as_ref(),
173+
encoding,
174+
))
175+
}
176+
177+
fn text_document_fixes(
178+
query: &DocumentQuery,
179+
source: &str,
180+
modified: &str,
181+
encoding: PositionEncoding,
182+
) -> Fixes {
183+
let source_index = LineIndex::from_source_text(source);
184+
let modified_index = LineIndex::from_source_text(modified);
185+
186+
let Replacement {
187+
source_range,
188+
modified_range,
189+
} = Replacement::between(
190+
source,
191+
source_index.line_starts(),
192+
modified,
193+
modified_index.line_starts(),
194+
);
195+
196+
[(
197+
query.make_key().into_uri(),
198+
vec![lsp_types::TextEdit {
199+
range: source_range.to_range(source, &source_index, encoding),
200+
new_text: modified[modified_range].to_owned(),
201+
}],
202+
)]
203+
.into_iter()
204+
.collect()
159205
}

0 commit comments

Comments
 (0)