Skip to content

Commit 07380e0

Browse files
authored
ruff_python_formatter: add docstring-code-line-width internal setting (#9055)
## Summary This does the light plumbing necessary to add a new internal option that permits setting the line width of code examples in docstrings. The plan is to add the corresponding user facing knob in #8854. Note that this effectively removes the `same-as-global` configuration style discussed [in this comment](#8855 (comment)). It replaces it with the `{integer}` configuration style only. There are a lot of commits here, but they are each tiny to make review easier because of the changes to snapshots. ## Test Plan I added a new docstring test configuration that sets `docstring-code-line-width = 60` and examined the differences.
1 parent 3aa6a30 commit 07380e0

16 files changed

+1728
-296
lines changed

crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_code_examples.options.json

+6
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,11 @@
3838
"docstring_code": "enabled",
3939
"indent_style": "tab",
4040
"indent_width": 4
41+
},
42+
{
43+
"docstring_code": "enabled",
44+
"docstring_code_line_width": 60,
45+
"indent_style": "space",
46+
"indent_width": 4
4147
}
4248
]

crates/ruff_python_formatter/src/expression/string/docstring.rs

+1
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ impl<'ast, 'buf, 'fmt, 'src> DocstringLinePrinter<'ast, 'buf, 'fmt, 'src> {
464464
.f
465465
.options()
466466
.clone()
467+
.with_line_width(self.f.options().docstring_code_line_width())
467468
// It's perhaps a little odd to be hard-coding the indent
468469
// style here, but I believe it is necessary as a result
469470
// of the whitespace normalization otherwise done in

crates/ruff_python_formatter/src/options.rs

+11
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ pub struct PyFormatOptions {
4949
/// enabled by default (opt-out) in the future.
5050
docstring_code: DocstringCode,
5151

52+
/// The preferred line width at which the formatter should wrap lines in
53+
/// docstring code examples. This only has an impact when `docstring_code`
54+
/// is enabled.
55+
#[cfg_attr(feature = "serde", serde(default = "default_line_width"))]
56+
docstring_code_line_width: LineWidth,
57+
5258
/// Whether preview style formatting is enabled or not
5359
preview: PreviewMode,
5460
}
@@ -77,6 +83,7 @@ impl Default for PyFormatOptions {
7783
magic_trailing_comma: MagicTrailingComma::default(),
7884
source_map_generation: SourceMapGeneration::default(),
7985
docstring_code: DocstringCode::default(),
86+
docstring_code_line_width: default_line_width(),
8087
preview: PreviewMode::default(),
8188
}
8289
}
@@ -119,6 +126,10 @@ impl PyFormatOptions {
119126
self.docstring_code
120127
}
121128

129+
pub fn docstring_code_line_width(&self) -> LineWidth {
130+
self.docstring_code_line_width
131+
}
132+
122133
pub const fn preview(&self) -> PreviewMode {
123134
self.preview
124135
}

crates/ruff_python_formatter/tests/fixtures.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -347,21 +347,23 @@ impl fmt::Display for DisplayPyOptions<'_> {
347347
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
348348
writeln!(
349349
f,
350-
r#"indent-style = {indent_style}
351-
line-width = {line_width}
352-
indent-width = {indent_width}
353-
quote-style = {quote_style:?}
354-
line-ending = {line_ending:?}
355-
magic-trailing-comma = {magic_trailing_comma:?}
356-
docstring-code = {docstring_code:?}
357-
preview = {preview:?}"#,
350+
r#"indent-style = {indent_style}
351+
line-width = {line_width}
352+
indent-width = {indent_width}
353+
quote-style = {quote_style:?}
354+
line-ending = {line_ending:?}
355+
magic-trailing-comma = {magic_trailing_comma:?}
356+
docstring-code = {docstring_code:?}
357+
docstring-code-line-width = {docstring_code_line_width:?}
358+
preview = {preview:?}"#,
358359
indent_style = self.0.indent_style(),
359360
indent_width = self.0.indent_width().value(),
360361
line_width = self.0.line_width().value(),
361362
quote_style = self.0.quote_style(),
362363
line_ending = self.0.line_ending(),
363364
magic_trailing_comma = self.0.magic_trailing_comma(),
364365
docstring_code = self.0.docstring_code(),
366+
docstring_code_line_width = self.0.docstring_code_line_width().value(),
365367
preview = self.0.preview()
366368
)
367369
}

crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap

+45-40
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,15 @@ def single_quoted():
166166
## Outputs
167167
### Output 1
168168
```
169-
indent-style = space
170-
line-width = 88
171-
indent-width = 4
172-
quote-style = Double
173-
line-ending = LineFeed
174-
magic-trailing-comma = Respect
175-
docstring-code = Disabled
176-
preview = Disabled
169+
indent-style = space
170+
line-width = 88
171+
indent-width = 4
172+
quote-style = Double
173+
line-ending = LineFeed
174+
magic-trailing-comma = Respect
175+
docstring-code = Disabled
176+
docstring-code-line-width = 88
177+
preview = Disabled
177178
```
178179

179180
```python
@@ -339,14 +340,15 @@ def single_quoted():
339340

340341
### Output 2
341342
```
342-
indent-style = space
343-
line-width = 88
344-
indent-width = 2
345-
quote-style = Double
346-
line-ending = LineFeed
347-
magic-trailing-comma = Respect
348-
docstring-code = Disabled
349-
preview = Disabled
343+
indent-style = space
344+
line-width = 88
345+
indent-width = 2
346+
quote-style = Double
347+
line-ending = LineFeed
348+
magic-trailing-comma = Respect
349+
docstring-code = Disabled
350+
docstring-code-line-width = 88
351+
preview = Disabled
350352
```
351353

352354
```python
@@ -512,14 +514,15 @@ def single_quoted():
512514

513515
### Output 3
514516
```
515-
indent-style = tab
516-
line-width = 88
517-
indent-width = 8
518-
quote-style = Double
519-
line-ending = LineFeed
520-
magic-trailing-comma = Respect
521-
docstring-code = Disabled
522-
preview = Disabled
517+
indent-style = tab
518+
line-width = 88
519+
indent-width = 8
520+
quote-style = Double
521+
line-ending = LineFeed
522+
magic-trailing-comma = Respect
523+
docstring-code = Disabled
524+
docstring-code-line-width = 88
525+
preview = Disabled
523526
```
524527

525528
```python
@@ -685,14 +688,15 @@ def single_quoted():
685688

686689
### Output 4
687690
```
688-
indent-style = tab
689-
line-width = 88
690-
indent-width = 4
691-
quote-style = Double
692-
line-ending = LineFeed
693-
magic-trailing-comma = Respect
694-
docstring-code = Disabled
695-
preview = Disabled
691+
indent-style = tab
692+
line-width = 88
693+
indent-width = 4
694+
quote-style = Double
695+
line-ending = LineFeed
696+
magic-trailing-comma = Respect
697+
docstring-code = Disabled
698+
docstring-code-line-width = 88
699+
preview = Disabled
696700
```
697701

698702
```python
@@ -858,14 +862,15 @@ def single_quoted():
858862

859863
### Output 5
860864
```
861-
indent-style = space
862-
line-width = 88
863-
indent-width = 4
864-
quote-style = Single
865-
line-ending = LineFeed
866-
magic-trailing-comma = Respect
867-
docstring-code = Disabled
868-
preview = Disabled
865+
indent-style = space
866+
line-width = 88
867+
indent-width = 4
868+
quote-style = Single
869+
line-ending = LineFeed
870+
magic-trailing-comma = Respect
871+
docstring-code = Disabled
872+
docstring-code-line-width = 88
873+
preview = Disabled
869874
```
870875

871876
```python

0 commit comments

Comments
 (0)