mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 09:31:09 +00:00
ruff_python_formatter: implement "dynamic" line width mode for docstring code formatting (#9098)
## Summary This PR changes the internal `docstring-code-line-width` setting to additionally accept a string value `dynamic`. When `dynamic` is set, the line width is dynamically adjusted when reformatting code snippets in docstrings based on the indent level of the docstring. The result is that the reformatted lines from the code snippet should not exceed the "global" line width configuration for the surrounding source. This PR does not change the default behavior, although I suspect the default should probably be `dynamic`. ## Test Plan I added a new configuration to the existing docstring code tests and also added a new set of tests dedicated to the new `dynamic` mode.
This commit is contained in:
parent
5559827a78
commit
b972455ac7
11 changed files with 3394 additions and 12 deletions
|
@ -7,13 +7,13 @@ use std::{borrow::Cow, collections::VecDeque};
|
|||
use {once_cell::sync::Lazy, regex::Regex};
|
||||
|
||||
use {
|
||||
ruff_formatter::{write, IndentStyle, Printed},
|
||||
ruff_formatter::{write, FormatOptions, IndentStyle, LineWidth, Printed},
|
||||
ruff_python_trivia::{is_python_whitespace, PythonWhitespace},
|
||||
ruff_source_file::Locator,
|
||||
ruff_text_size::{Ranged, TextLen, TextRange, TextSize},
|
||||
};
|
||||
|
||||
use crate::{prelude::*, FormatModuleError};
|
||||
use crate::{prelude::*, DocstringCodeLineWidth, FormatModuleError};
|
||||
|
||||
use super::{NormalizedString, QuoteChar};
|
||||
|
||||
|
@ -460,11 +460,22 @@ impl<'ast, 'buf, 'fmt, 'src> DocstringLinePrinter<'ast, 'buf, 'fmt, 'src> {
|
|||
.map(|line| line.code)
|
||||
.collect::<Vec<&str>>()
|
||||
.join("\n");
|
||||
let line_width = match self.f.options().docstring_code_line_width() {
|
||||
DocstringCodeLineWidth::Fixed(width) => width,
|
||||
DocstringCodeLineWidth::Dynamic => {
|
||||
let global_line_width = self.f.options().line_width().value();
|
||||
let indent_width = self.f.options().indent_width();
|
||||
let indent_level = self.f.context().indent_level();
|
||||
let current_indent = indent_level.to_ascii_spaces(indent_width);
|
||||
let width = std::cmp::max(1, global_line_width.saturating_sub(current_indent));
|
||||
LineWidth::try_from(width).expect("width is capped at a minimum of 1")
|
||||
}
|
||||
};
|
||||
let options = self
|
||||
.f
|
||||
.options()
|
||||
.clone()
|
||||
.with_line_width(self.f.options().docstring_code_line_width())
|
||||
.with_line_width(line_width)
|
||||
// It's perhaps a little odd to be hard-coding the indent
|
||||
// style here, but I believe it is necessary as a result
|
||||
// of the whitespace normalization otherwise done in
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue