mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 03:14:41 +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
|
@ -52,8 +52,7 @@ pub struct PyFormatOptions {
|
|||
/// The preferred line width at which the formatter should wrap lines in
|
||||
/// docstring code examples. This only has an impact when `docstring_code`
|
||||
/// is enabled.
|
||||
#[cfg_attr(feature = "serde", serde(default = "default_line_width"))]
|
||||
docstring_code_line_width: LineWidth,
|
||||
docstring_code_line_width: DocstringCodeLineWidth,
|
||||
|
||||
/// Whether preview style formatting is enabled or not
|
||||
preview: PreviewMode,
|
||||
|
@ -83,7 +82,7 @@ impl Default for PyFormatOptions {
|
|||
magic_trailing_comma: MagicTrailingComma::default(),
|
||||
source_map_generation: SourceMapGeneration::default(),
|
||||
docstring_code: DocstringCode::default(),
|
||||
docstring_code_line_width: default_line_width(),
|
||||
docstring_code_line_width: DocstringCodeLineWidth::default(),
|
||||
preview: PreviewMode::default(),
|
||||
}
|
||||
}
|
||||
|
@ -126,7 +125,7 @@ impl PyFormatOptions {
|
|||
self.docstring_code
|
||||
}
|
||||
|
||||
pub fn docstring_code_line_width(&self) -> LineWidth {
|
||||
pub fn docstring_code_line_width(&self) -> DocstringCodeLineWidth {
|
||||
self.docstring_code_line_width
|
||||
}
|
||||
|
||||
|
@ -302,3 +301,50 @@ impl DocstringCode {
|
|||
matches!(self, DocstringCode::Enabled)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, CacheKey)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
|
||||
#[cfg_attr(feature = "serde", serde(untagged))]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
pub enum DocstringCodeLineWidth {
|
||||
Fixed(LineWidth),
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
serde(deserialize_with = "deserialize_docstring_code_line_width_dynamic")
|
||||
)]
|
||||
Dynamic,
|
||||
}
|
||||
|
||||
impl Default for DocstringCodeLineWidth {
|
||||
fn default() -> DocstringCodeLineWidth {
|
||||
DocstringCodeLineWidth::Fixed(default_line_width())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for DocstringCodeLineWidth {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match *self {
|
||||
DocstringCodeLineWidth::Fixed(v) => v.value().fmt(f),
|
||||
DocstringCodeLineWidth::Dynamic => "dynamic".fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Responsible for deserializing the `DocstringCodeLineWidth::Dynamic`
|
||||
/// variant.
|
||||
fn deserialize_docstring_code_line_width_dynamic<'de, D>(d: D) -> Result<(), D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
use serde::{de::Error, Deserialize};
|
||||
|
||||
let value = String::deserialize(d)?;
|
||||
match &*value {
|
||||
"dynamic" => Ok(()),
|
||||
s => Err(D::Error::invalid_value(
|
||||
serde::de::Unexpected::Str(s),
|
||||
&"dynamic",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue