Skip to content

Commit abe3115

Browse files
committed
Resolve todos and clippy
1 parent cde6bfd commit abe3115

1 file changed

Lines changed: 58 additions & 61 deletions

File tree

crates/ruff_server/src/format.rs

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io::Write;
22
use std::path::Path;
33
use std::process::{Command, Stdio};
44

5-
use anyhow::{Context, Error};
5+
use anyhow::Context;
66

77
use ruff_formatter::{FormatOptions, PrintedRange};
88
use ruff_markdown::{MarkdownResult, format_code_blocks};
@@ -76,15 +76,19 @@ fn format_internal(
7676
}
7777
SourceType::Markdown => {
7878
if !formatter_settings.preview.is_enabled() {
79-
return Ok(None); // todo
79+
tracing::warn!("Markdown formatting is experimental, enable preview mode.");
80+
return Ok(None);
8081
}
8182

8283
match format_code_blocks(document.contents(), Some(path), formatter_settings) {
8384
MarkdownResult::Formatted(formatted) => Ok(Some(formatted)),
8485
MarkdownResult::Unchanged => Ok(None),
8586
}
8687
}
87-
SourceType::Toml(_) => Ok(None), // todo
88+
SourceType::Toml(_) => {
89+
tracing::warn!("Formatting TOML files not supported");
90+
Ok(None)
91+
}
8892
}
8993
}
9094

@@ -95,20 +99,22 @@ fn format_external(
9599
formatter_settings: &FormatterSettings,
96100
path: &Path,
97101
) -> crate::Result<Option<String>> {
98-
match source_type {
102+
let format_options = match source_type {
99103
SourceType::Python(py_source_type) => {
100-
let format_options = formatter_settings.to_format_options(
101-
py_source_type,
102-
document.contents(),
103-
Some(path),
104-
);
105-
let uv_command = UvFormatCommand::from(format_options);
106-
uv_command.format_document(document.contents(), path)
104+
formatter_settings.to_format_options(py_source_type, document.contents(), Some(path))
107105
}
108-
SourceType::Markdown | SourceType::Toml(_) => {
109-
Ok(None) // todo
106+
SourceType::Markdown => formatter_settings.to_format_options(
107+
PySourceType::Python,
108+
document.contents(),
109+
Some(path),
110+
),
111+
SourceType::Toml(_) => {
112+
tracing::warn!("Formatting TOML files not supported");
113+
return Ok(None);
110114
}
111-
}
115+
};
116+
let uv_command = UvFormatCommand::from(format_options);
117+
uv_command.format_document(document.contents(), path)
112118
}
113119

114120
pub(crate) fn format_range(
@@ -119,81 +125,72 @@ pub(crate) fn format_range(
119125
path: &Path,
120126
backend: FormatBackend,
121127
) -> crate::Result<Option<PrintedRange>> {
128+
let py_source_type = match source_type {
129+
SourceType::Python(py_source_type) => py_source_type,
130+
SourceType::Markdown => {
131+
tracing::warn!("Range formatting for Markdown files not supported");
132+
return Ok(None);
133+
}
134+
SourceType::Toml(_) => {
135+
tracing::warn!("Formatting TOML files not supported");
136+
return Ok(None);
137+
}
138+
};
122139
match backend {
123140
FormatBackend::Uv => {
124-
format_range_external(document, source_type, formatter_settings, range, path)
141+
format_range_external(document, py_source_type, formatter_settings, range, path)
125142
}
126143
FormatBackend::Internal => {
127-
format_range_internal(document, source_type, formatter_settings, range, path)
144+
format_range_internal(document, py_source_type, formatter_settings, range, path)
128145
}
129146
}
130147
}
131148

132149
/// Format range using the built-in Ruff formatter
133150
fn format_range_internal(
134151
document: &TextDocument,
135-
source_type: SourceType,
152+
source_type: PySourceType,
136153
formatter_settings: &FormatterSettings,
137154
range: TextRange,
138155
path: &Path,
139156
) -> crate::Result<Option<PrintedRange>> {
140-
match source_type {
141-
SourceType::Python(py_source_type) => {
142-
let format_options = formatter_settings.to_format_options(
143-
py_source_type,
144-
document.contents(),
145-
Some(path),
146-
);
147-
148-
match ruff_python_formatter::format_range(document.contents(), range, format_options) {
149-
Ok(formatted) => {
150-
if formatted.as_code() == document.contents() {
151-
Ok(None)
152-
} else {
153-
Ok(Some(formatted))
154-
}
155-
}
156-
// Special case - syntax/parse errors are handled here instead of
157-
// being propagated as visible server errors.
158-
Err(FormatModuleError::ParseError(error)) => {
159-
tracing::warn!("Unable to format document range: {error}");
160-
Ok(None)
161-
}
162-
Err(err) => Err(err.into()),
157+
let format_options =
158+
formatter_settings.to_format_options(source_type, document.contents(), Some(path));
159+
160+
match ruff_python_formatter::format_range(document.contents(), range, format_options) {
161+
Ok(formatted) => {
162+
if formatted.as_code() == document.contents() {
163+
Ok(None)
164+
} else {
165+
Ok(Some(formatted))
163166
}
164167
}
165-
SourceType::Markdown | SourceType::Toml(_) => {
166-
Ok(None) // todo
168+
// Special case - syntax/parse errors are handled here instead of
169+
// being propagated as visible server errors.
170+
Err(FormatModuleError::ParseError(error)) => {
171+
tracing::warn!("Unable to format document range: {error}");
172+
Ok(None)
167173
}
174+
Err(err) => Err(err.into()),
168175
}
169176
}
170177

171178
/// Format range using an external command, i.e., `uv`.
172179
fn format_range_external(
173180
document: &TextDocument,
174-
source_type: SourceType,
181+
source_type: PySourceType,
175182
formatter_settings: &FormatterSettings,
176183
range: TextRange,
177184
path: &Path,
178185
) -> crate::Result<Option<PrintedRange>> {
179-
match source_type {
180-
SourceType::Python(py_source_type) => {
181-
let format_options = formatter_settings.to_format_options(
182-
py_source_type,
183-
document.contents(),
184-
Some(path),
185-
);
186-
let uv_command = UvFormatCommand::from(format_options);
187-
188-
// Format the range using uv and convert the result to `PrintedRange`
189-
match uv_command.format_range(document.contents(), range, path, document.index())? {
190-
Some(formatted) => Ok(Some(PrintedRange::new(formatted, range))),
191-
None => Ok(None),
192-
}
193-
}
194-
SourceType::Markdown | SourceType::Toml(_) => {
195-
Ok(None) // todo
196-
}
186+
let format_options =
187+
formatter_settings.to_format_options(source_type, document.contents(), Some(path));
188+
let uv_command = UvFormatCommand::from(format_options);
189+
190+
// Format the range using uv and convert the result to `PrintedRange`
191+
match uv_command.format_range(document.contents(), range, path, document.index())? {
192+
Some(formatted) => Ok(Some(PrintedRange::new(formatted, range))),
193+
None => Ok(None),
197194
}
198195
}
199196

0 commit comments

Comments
 (0)