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](https://github.com/astral-sh/ruff/issues/8855#issuecomment-1847230440).
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.
This commit is contained in:
Andrew Gallant 2023-12-11 08:20:59 -05:00 committed by GitHub
parent 3aa6a30395
commit 07380e0657
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 1728 additions and 296 deletions

View file

@ -347,14 +347,15 @@ 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(),
@ -362,6 +363,7 @@ preview = {preview:?}"#,
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()
)
}

View file

@ -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
@ -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
@ -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
@ -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
@ -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

View file

@ -18,14 +18,15 @@ def doctest_line_ending():
## Outputs
### Output 1
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = CarriageReturnLineFeed
magic-trailing-comma = Respect
docstring-code = Enabled
preview = Disabled
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = CarriageReturnLineFeed
magic-trailing-comma = Respect
docstring-code = Enabled
docstring-code-line-width = 88
preview = Disabled
```
```python

View file

@ -129,14 +129,15 @@ test_particular = [
## 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
@ -280,14 +281,15 @@ test_particular = [
### Output 2
```
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

View file

@ -144,14 +144,15 @@ trailing_preferred_quote_texts = [''' "''', ''' ""''', ''' """''', ''' """"''']
## 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
@ -319,14 +320,15 @@ trailing_preferred_quote_texts = [''' "''', ''' ""''', ''' """''', ''' """"''']
### Output 2
```
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

View file

@ -28,14 +28,15 @@ def test():
## 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
@ -63,14 +64,15 @@ def test():
### 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

View file

@ -9,14 +9,15 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off
## 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
@ -25,14 +26,15 @@ preview = Disabled
### Output 2
```
indent-style = space
line-width = 88
indent-width = 1
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Disabled
indent-style = space
line-width = 88
indent-width = 1
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = 88
preview = Disabled
```
```python
@ -41,14 +43,15 @@ preview = Disabled
### Output 3
```
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

View file

@ -24,14 +24,15 @@ not_fixed
## 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
@ -56,14 +57,15 @@ not_fixed
### 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
@ -88,14 +90,15 @@ not_fixed
### Output 3
```
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

View file

@ -75,14 +75,15 @@ def f():
## 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
@ -156,14 +157,15 @@ def f():
### Output 2
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Enabled
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 = Enabled
```
```python

View file

@ -59,14 +59,15 @@ def docstring_single():
## Outputs
### Output 1
```
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
@ -129,14 +130,15 @@ def docstring_single():
### Output 2
```
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
@ -199,14 +201,15 @@ def docstring_single():
### Output 3
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Preserve
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Disabled
indent-style = space
line-width = 88
indent-width = 4
quote-style = Preserve
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = 88
preview = Disabled
```
```python

View file

@ -42,14 +42,15 @@ with (a,): # magic trailing comma
## 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
@ -97,14 +98,15 @@ with (
### Output 2
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Ignore
docstring-code = Disabled
preview = Disabled
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Ignore
docstring-code = Disabled
docstring-code-line-width = 88
preview = Disabled
```
```python

View file

@ -17,14 +17,15 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/tab_width.
## Outputs
### Output 1
```
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
@ -41,14 +42,15 @@ preview = Disabled
### Output 2
```
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
@ -68,14 +70,15 @@ preview = Disabled
### Output 3
```
indent-style = space
line-width = 88
indent-width = 8
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
preview = Disabled
indent-style = space
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