Add tab width option (#6848)

This commit is contained in:
Micha Reiser 2023-08-26 12:29:58 +02:00 committed by GitHub
parent f91bacbb94
commit 9d77552e18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 345 additions and 44 deletions

View file

@ -253,9 +253,11 @@ impl fmt::Display for DisplayPyOptions<'_> {
f,
r#"indent-style = {indent_style}
line-width = {line_width}
tab-width = {tab_width}
quote-style = {quote_style:?}
magic-trailing-comma = {magic_trailing_comma:?}"#,
indent_style = self.0.indent_style(),
tab_width = self.0.tab_width().value(),
line_width = self.0.line_width().value(),
quote_style = self.0.quote_style(),
magic_trailing_comma = self.0.magic_trailing_comma()

View file

@ -113,6 +113,7 @@ class TabbedIndent:
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 8
quote-style = Double
magic-trailing-comma = Respect
```
@ -225,6 +226,7 @@ class TabbedIndent:
```
indent-style = Spaces, size: 2
line-width = 88
tab-width = 8
quote-style = Double
magic-trailing-comma = Respect
```
@ -337,6 +339,7 @@ class TabbedIndent:
```
indent-style = Tab
line-width = 88
tab-width = 8
quote-style = Double
magic-trailing-comma = Respect
```
@ -445,4 +448,117 @@ class TabbedIndent:
```
### Output 4
```
indent-style = Tab
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
```py
def single_line_backslashes1():
"""content\ """
return
def single_line_backslashes2():
"""content\\"""
return
def single_line_backslashes3():
"""content\\\ """
return
def multiline_backslashes1():
"""This is a docstring with
some lines of text\ """
return
def multiline_backslashes2():
"""This is a docstring with
some lines of text\\"""
return
def multiline_backslashes3():
"""This is a docstring with
some lines of text\\\ """
return
def multiple_negatively_indented_docstring_lines():
"""a
b
c
d
e
"""
def overindentend_docstring():
"""a
over-indented
"""
def comment_before_docstring():
# don't lose this function comment ...
"""Does nothing.
But it has comments
""" # ... neither lose this function comment
class CommentBeforeDocstring:
# don't lose this class comment ...
"""Empty class.
But it has comments
""" # ... neither lose this class comment
class IndentMeSome:
def doc_string_without_linebreak_after_colon(self):
"""This is somewhat strange
a
b
We format this a is the docstring had started properly indented on the next
line if the target indentation. This may we incorrect since source and target
indentation can be incorrect, but this is also an edge case.
"""
class IgnoreImplicitlyConcatenatedStrings:
"""""" ""
def docstring_that_ends_with_quote_and_a_line_break1():
"""
he said "the news of my death have been greatly exaggerated"
"""
def docstring_that_ends_with_quote_and_a_line_break2():
"""he said "the news of my death have been greatly exaggerated" """
def docstring_that_ends_with_quote_and_a_line_break3():
"""he said "the news of my death have been greatly exaggerated" """
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
^^^^^^^^^^
Normal indented line
- autor
"""
```

View file

@ -131,6 +131,7 @@ test_particular = [
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
@ -279,6 +280,7 @@ test_particular = [
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Single
magic-trailing-comma = Respect
```

View file

@ -143,6 +143,7 @@ x = (b"""aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa""" b"""bbbbbbbbbbbbbbbbbbbbbbbbbbb
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
@ -312,6 +313,7 @@ x = (
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Single
magic-trailing-comma = Respect
```

View file

@ -30,6 +30,7 @@ def test():
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
@ -61,6 +62,7 @@ def test():
```
indent-style = Spaces, size: 2
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```

View file

@ -66,6 +66,7 @@ formatted;
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
@ -135,6 +136,7 @@ formatted
```
indent-style = Spaces, size: 1
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
@ -204,6 +206,7 @@ formatted
```
indent-style = Tab
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```

View file

@ -26,6 +26,7 @@ not_fixed
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
@ -52,6 +53,7 @@ not_fixed
```
indent-style = Spaces, size: 2
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
@ -78,6 +80,7 @@ not_fixed
```
indent-style = Tab
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```

View file

@ -44,6 +44,7 @@ with (a,): # magic trailing comma
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
@ -95,6 +96,7 @@ with (
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Ignore
```

View file

@ -0,0 +1,69 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/tab_width.py
---
## Input
```py
# Fits with tab width 2
1 + " 012345678901234567890123456789012345678901234567890123456789012345678901234567890"
# Fits with tab width 4
1 + " 0123456789012345678901234567890123456789012345678901234567890123456789012345678"
# Fits with tab width 8
1 + " 012345678901234567890123456789012345678901234567890123456789012345678901234"
```
## Outputs
### Output 1
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 2
quote-style = Double
magic-trailing-comma = Respect
```
```py
# Fits with tab width 2
(
1
+ " 012345678901234567890123456789012345678901234567890123456789012345678901234567890"
)
# Fits with tab width 4
1 + " 0123456789012345678901234567890123456789012345678901234567890123456789012345678"
# Fits with tab width 8
1 + " 012345678901234567890123456789012345678901234567890123456789012345678901234"
```
### Output 2
```
indent-style = Spaces, size: 4
line-width = 88
tab-width = 4
quote-style = Double
magic-trailing-comma = Respect
```
```py
# Fits with tab width 2
(
1
+ " 012345678901234567890123456789012345678901234567890123456789012345678901234567890"
)
# Fits with tab width 4
(
1
+ " 0123456789012345678901234567890123456789012345678901234567890123456789012345678"
)
# Fits with tab width 8
1 + " 012345678901234567890123456789012345678901234567890123456789012345678901234"
```