mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +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
|
@ -44,5 +44,11 @@
|
|||
"docstring_code_line_width": 60,
|
||||
"indent_style": "space",
|
||||
"indent_width": 4
|
||||
},
|
||||
{
|
||||
"docstring_code": "enabled",
|
||||
"docstring_code_line_width": "dynamic",
|
||||
"indent_style": "space",
|
||||
"indent_width": 4
|
||||
}
|
||||
]
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
[
|
||||
{
|
||||
"docstring_code": "enabled",
|
||||
"docstring_code_line_width": "dynamic",
|
||||
"indent_style": "space",
|
||||
"indent_width": 4
|
||||
},
|
||||
{
|
||||
"docstring_code": "enabled",
|
||||
"docstring_code_line_width": "dynamic",
|
||||
"indent_style": "space",
|
||||
"indent_width": 2
|
||||
},
|
||||
{
|
||||
"docstring_code": "enabled",
|
||||
"docstring_code_line_width": "dynamic",
|
||||
"indent_style": "tab",
|
||||
"indent_width": 4
|
||||
},
|
||||
{
|
||||
"docstring_code": "enabled",
|
||||
"docstring_code_line_width": "dynamic",
|
||||
"indent_style": "tab",
|
||||
"indent_width": 8
|
||||
}
|
||||
]
|
|
@ -0,0 +1,172 @@
|
|||
def simple():
|
||||
"""
|
||||
First line.
|
||||
|
||||
```py
|
||||
class Abcdefghijklmopqrstuvwxyz(Abc, Def, Ghi, Jkl, Mno, Pqr, Stu, Vwx, Yz, A1, A2, A3, A4, A5):
|
||||
def abcdefghijklmnopqrstuvwxyz(self, abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
```
|
||||
|
||||
Done.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
# Like simple, but we double everything up to ensure the indent level is
|
||||
# tracked correctly.
|
||||
def repeated():
|
||||
"""
|
||||
First line.
|
||||
|
||||
```py
|
||||
class Abcdefghijklmopqrstuvwxyz(Abc, Def, Ghi, Jkl, Mno, Pqr, Stu, Vwx, Yz, A1, A2, A3, A4, A5):
|
||||
def abcdefghijklmnopqrstuvwxyz(self, abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
|
||||
def abcdefghijklmnopqrstuvwxyz(self, abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
|
||||
|
||||
class Abcdefghijklmopqrstuvwxyz(Abc, Def, Ghi, Jkl, Mno, Pqr, Stu, Vwx, Yz, A1, A2, A3, A4, A5):
|
||||
def abcdefghijklmnopqrstuvwxyz(self, abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
|
||||
def abcdefghijklmnopqrstuvwxyz(self, abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
```
|
||||
|
||||
Done.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
# Like simple, but we make one line exactly one character longer than the limit
|
||||
# (for 4-space indents) and make sure it gets wrapped.
|
||||
def barely_exceeds_limit():
|
||||
"""
|
||||
First line.
|
||||
|
||||
```py
|
||||
class Abcdefghijklmopqrstuvwxyz(Abc, Def, Ghi, Jkl, Mno, Pqr, Stu, Vwx, Yz, A1, A2, A3, A4, A5):
|
||||
def abcdefghijklmnopqrstuvwxyz(self, abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is 89 columns, which is one
|
||||
# more than the limit. Therefore, it should get wrapped for
|
||||
# indent_width >= 4.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a5678)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
```
|
||||
|
||||
Done.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
# This tests that if the code block is unindented, that it gets indented and
|
||||
# the dynamic line width setting is applied correctly.
|
||||
def unindented():
|
||||
"""
|
||||
First line.
|
||||
|
||||
```py
|
||||
class Abcdefghijklmopqrstuvwxyz(Abc, Def, Ghi, Jkl, Mno, Pqr, Stu, Vwx, Yz, A1, A2, A3, A4, A5):
|
||||
def abcdefghijklmnopqrstuvwxyz(self, abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is just one character shy of
|
||||
# tripping the default line width of 88. So it should not be
|
||||
# wrapped.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a567)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
```
|
||||
|
||||
Done.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
# Like unindented, but contains a `print` line where it just barely exceeds the
|
||||
# globally configured line width *after* its indentation has been corrected.
|
||||
def unindented_barely_exceeds_limit():
|
||||
"""
|
||||
First line.
|
||||
|
||||
```py
|
||||
class Abcdefghijklmopqrstuvwxyz(Abc, Def, Ghi, Jkl, Mno, Pqr, Stu, Vwx, Yz, A1, A2, A3, A4, A5):
|
||||
def abcdefghijklmnopqrstuvwxyz(self, abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
def abcdefghijklmnopqrstuvwxyz(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4):
|
||||
# For 4 space indents, this is 89 columns, which is one
|
||||
# more than the limit. Therefore, it should get wrapped for
|
||||
# indent_width >= 4.
|
||||
print(abc, ddef, ghi, jkl, mno, pqr, stu, vwx, yz, a1, a2, a3, a4, a5678)
|
||||
return 5
|
||||
self.x = doit( 5 )
|
||||
```
|
||||
|
||||
Done.
|
||||
"""
|
||||
pass
|
Loading…
Add table
Add a link
Reference in a new issue