Skip to content

Commit

Permalink
ruff_python_formatter: add docstring-code-line-width internal setting (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
BurntSushi committed Dec 11, 2023
1 parent 3aa6a30 commit 07380e0
Show file tree
Hide file tree
Showing 16 changed files with 1,728 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@
"docstring_code": "enabled",
"indent_style": "tab",
"indent_width": 4
},
{
"docstring_code": "enabled",
"docstring_code_line_width": 60,
"indent_style": "space",
"indent_width": 4
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ impl<'ast, 'buf, 'fmt, 'src> DocstringLinePrinter<'ast, 'buf, 'fmt, 'src> {
.f
.options()
.clone()
.with_line_width(self.f.options().docstring_code_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
Expand Down
11 changes: 11 additions & 0 deletions crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ pub struct PyFormatOptions {
/// enabled by default (opt-out) in the future.
docstring_code: DocstringCode,

/// 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,

/// Whether preview style formatting is enabled or not
preview: PreviewMode,
}
Expand Down Expand Up @@ -77,6 +83,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(),
preview: PreviewMode::default(),
}
}
Expand Down Expand Up @@ -119,6 +126,10 @@ impl PyFormatOptions {
self.docstring_code
}

pub fn docstring_code_line_width(&self) -> LineWidth {
self.docstring_code_line_width
}

pub const fn preview(&self) -> PreviewMode {
self.preview
}
Expand Down
18 changes: 10 additions & 8 deletions crates/ruff_python_formatter/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,21 +347,23 @@ impl fmt::Display for DisplayPyOptions<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln!(
f,
r#"indent-style = {indent_style}
line-width = {line_width}
indent-width = {indent_width}
quote-style = {quote_style:?}
line-ending = {line_ending:?}
magic-trailing-comma = {magic_trailing_comma:?}
docstring-code = {docstring_code:?}
preview = {preview:?}"#,
r#"indent-style = {indent_style}
line-width = {line_width}
indent-width = {indent_width}
quote-style = {quote_style:?}
line-ending = {line_ending:?}
magic-trailing-comma = {magic_trailing_comma:?}
docstring-code = {docstring_code:?}
docstring-code-line-width = {docstring_code_line_width:?}
preview = {preview:?}"#,
indent_style = self.0.indent_style(),
indent_width = self.0.indent_width().value(),
line_width = self.0.line_width().value(),
quote_style = self.0.quote_style(),
line_ending = self.0.line_ending(),
magic_trailing_comma = self.0.magic_trailing_comma(),
docstring_code = self.0.docstring_code(),
docstring_code_line_width = self.0.docstring_code_line_width().value(),
preview = self.0.preview()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ def single_quoted():
## Outputs
### Output 1
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Disabled
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = 88
preview = Disabled
```

```python
Expand Down Expand Up @@ -339,14 +340,15 @@ def single_quoted():

### Output 2
```
indent-style = space
line-width = 88
indent-width = 2
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Disabled
indent-style = space
line-width = 88
indent-width = 2
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = 88
preview = Disabled
```

```python
Expand Down Expand Up @@ -512,14 +514,15 @@ def single_quoted():

### Output 3
```
indent-style = tab
line-width = 88
indent-width = 8
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Disabled
indent-style = tab
line-width = 88
indent-width = 8
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = 88
preview = Disabled
```

```python
Expand Down Expand Up @@ -685,14 +688,15 @@ def single_quoted():

### Output 4
```
indent-style = tab
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Disabled
indent-style = tab
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = 88
preview = Disabled
```

```python
Expand Down Expand Up @@ -858,14 +862,15 @@ def single_quoted():

### Output 5
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Single
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Disabled
indent-style = space
line-width = 88
indent-width = 4
quote-style = Single
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = 88
preview = Disabled
```

```python
Expand Down

0 comments on commit 07380e0

Please sign in to comment.