mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:45:24 +00:00
Enable formatting for Jupyter notebooks (#7749)
## Summary This PR enables `ruff format` to format Jupyter notebooks. Most of the work is contained in a new `format_source` method that formats a generic `SourceKind`, then returns `Some(transformed)` if the source required formatting, or `None` otherwise. Closes https://github.com/astral-sh/ruff/issues/7598. ## Test Plan Ran `cat foo.py | cargo run -p ruff_cli -- format --stdin-filename Untitled.ipynb`; verified that the console showed a reasonable error: ```console warning: Failed to read notebook Untitled.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: EOF while parsing a value at line 1 column 0 ``` Ran `cat Untitled.ipynb | cargo run -p ruff_cli -- format --stdin-filename Untitled.ipynb`; verified that the JSON output contained formatted source code.
This commit is contained in:
parent
0961f008b8
commit
bdf285225d
7 changed files with 422 additions and 164 deletions
|
@ -46,10 +46,7 @@ impl SourceMap {
|
|||
/// The `output_length` is the length of the transformed string before the
|
||||
/// edit is applied.
|
||||
pub fn push_start_marker(&mut self, edit: &Edit, output_length: TextSize) {
|
||||
self.0.push(SourceMarker {
|
||||
source: edit.start(),
|
||||
dest: output_length,
|
||||
});
|
||||
self.push_marker(edit.start(), output_length);
|
||||
}
|
||||
|
||||
/// Push the end marker for an [`Edit`].
|
||||
|
@ -58,16 +55,18 @@ impl SourceMap {
|
|||
/// edit has been applied.
|
||||
pub fn push_end_marker(&mut self, edit: &Edit, output_length: TextSize) {
|
||||
if edit.is_insertion() {
|
||||
self.0.push(SourceMarker {
|
||||
source: edit.start(),
|
||||
dest: output_length,
|
||||
});
|
||||
self.push_marker(edit.start(), output_length);
|
||||
} else {
|
||||
// Deletion or replacement
|
||||
self.0.push(SourceMarker {
|
||||
source: edit.end(),
|
||||
dest: output_length,
|
||||
});
|
||||
self.push_marker(edit.end(), output_length);
|
||||
}
|
||||
}
|
||||
|
||||
/// Push a new marker to the sourcemap.
|
||||
pub fn push_marker(&mut self, offset: TextSize, output_length: TextSize) {
|
||||
self.0.push(SourceMarker {
|
||||
source: offset,
|
||||
dest: output_length,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue