Skip to content

Commit

Permalink
New option: extra indent on closing bracket (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 authored May 8, 2023
1 parent 29011fd commit 8ce1cce
Show file tree
Hide file tree
Showing 12 changed files with 660 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Change Log

## [0.1.4] - 2023-05-07

- Added
- A new configurable option: `--closing-bracket-extra-indent`

## [0.1.3] - 2023-05-07

- Added

- A new configurable option: `--collapse-nested-brackets`
- A new configurable option: `--wrap-pragma-comments`
- Some Github workflow actions to make sure CHANGELOG.md is updated

- Changed

- Changed the default quote to single quote
- Changed the default line length to 79 characters

Expand Down
92 changes: 86 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ _Cercis_ offers the following configurable options:
1. [Line length](#31-line-length)
2. [Single quote vs double quote](#32-single-quote-vs-double-quote)
3. [Extra indentation at function definition](#33-extra-indentation-at-function-definition)
4. ["Simple" lines with long strings](#34-simple-lines-with-long-strings)
5. [Collapse nested brackets](#35-collapse-nested-brackets)
6. [Wrap pragma comments](#36-wrapping-long-lines-ending-with-pragma-comments)
4. [Extra indentation at closing brackets](#34-closing-bracket-indentation)
5. ["Simple" lines with long strings](#35-simple-lines-with-long-strings)
6. [Collapse nested brackets](#36-collapse-nested-brackets)
7. [Wrap pragma comments](#37-wrapping-long-lines-ending-with-pragma-comments)

The next section ([How to configure _Cercis_](#4-how-to-configure-cercis))
contains detailed instructions of how to configure these options.
Expand Down Expand Up @@ -184,7 +185,86 @@ You can override this default if necessary.
| `pyproject.toml` usage | `function-definition-extra-indent = true` under `[tool.cercis]` |
| `pre-commit` usage | `args: [--function-definition-extra-indent=False]` |

### 3.4. "Simple" lines with long strings
## 3.4. Closing bracket indentation

This option lets people customize where the closing bracket should be. Note
that both styles are OK according to
[PEP8](https://peps.python.org/pep-0008/#indentation).

<table>
<tr>
<td>

```python
# --closing-bracket-extra-indent=False
def function(
arg1: int,
arg2: float,
arg3_with_long_name: list,
) -> None:
print('Hello world')
result = func2(
12345,
3.1415926,
[1, 2, 3],
)
something = {
'a': 1,
'b': 2,
'c': 3,
}
```

</td>

<td>

```python
# --closing-bracket-extra-indent=True
def function(
arg1: int,
arg2: float,
arg3_with_long_name: list,
) -> None:
print('Hello world')
result = func2(
12345,
3.1415926,
[1, 2, 3],
)
something = {
'a': 1,
'b': 2,
'c': 3,
}
```

</td>

</tr>
</table>

| Option | |
| ---------------------- | ----------------------------------------------------------- |
| Name | `--closing-bracket-extra-indent` |
| Abbreviation | `-cbei` |
| Default | `False` |
| Black's default | `False` |
| Command line usage | `cercis -cbei=True myScript.py` |
| `pyproject.toml` usage | `closing-bracket-extra-indent = true` under `[tool.cercis]` |
| `pre-commit` usage | `args: [--closing-bracket-extra-indent=False]` |

### 3.5. "Simple" lines with long strings

By default, Black wraps lines that exceed length limit. But for very simple
lines (such as assigning a long string to a variable), line wrapping is not
Expand Down Expand Up @@ -251,7 +331,7 @@ var3 = (
| `pyproject.toml` usage | `wrap-line-with-long-string = true` under `[tool.cercis]` |
| `pre-commit` usage | `args: [--wrap-line-with-long-string=False]` |

### 3.5. Collapse nested brackets
### 3.6. Collapse nested brackets

_Cercis_ by default collapses nested brackets to make the code more compact.

Expand Down Expand Up @@ -327,7 +407,7 @@ value = function(
The code implementation of this option comes from
[Pyink](https://github.com/google/pyink), another forked project from Black.

### 3.6. Wrapping long lines ending with pragma comments [^](#3-cerciss-code-style)
### 3.7. Wrapping long lines ending with pragma comments [^](#3-cerciss-code-style)

"Pragma comments", in this context, mean the directives for Python linters
usually to tell them to ignore certain errors. Pragma comments that _Cercis_
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ build-backend = "hatchling.build"

[project]
name = "cercis"
version = "0.1.3"
version = "0.1.4"
description = "A more configurable Python code formatter"
license = { text = "MIT" }
requires-python = ">=3.7"
Expand Down
14 changes: 14 additions & 0 deletions src/cercis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from cercis.cache import Cache, get_cache_info, read_cache, write_cache
from cercis.comments import normalize_fmt_off
from cercis.const import (
DEFAULT_CLOSING_BRACKET_EXTRA_INDENT,
DEFAULT_COLLAPSE_NESTED_BRACKETS,
DEFAULT_EXCLUDES,
DEFAULT_FUNCTION_DEFINITION_EXTRA_INDENT,
Expand Down Expand Up @@ -228,6 +229,17 @@ def validate_regex(
" otherwise, use 8 (Black's default)."
),
)
@click.option(
"-cbei",
"--closing-bracket-extra-indent",
type=bool,
show_default=True,
default=DEFAULT_CLOSING_BRACKET_EXTRA_INDENT,
help=(
"If True, add an extra indentation level (4 or 8, depending on"
" --function-definition-extra-indent) to the closing bracket."
),
)
@click.option(
"-sq",
"--single-quote",
Expand Down Expand Up @@ -489,6 +501,7 @@ def main( # noqa: C901
code: Optional[str],
line_length: int,
function_definition_extra_indent: bool,
closing_bracket_extra_indent: bool,
single_quote: bool,
wrap_line_with_long_string: bool,
collapse_nested_brackets: bool,
Expand Down Expand Up @@ -616,6 +629,7 @@ def main( # noqa: C901
preview=preview,
python_cell_magics=set(python_cell_magics),
function_definition_extra_indent=function_definition_extra_indent,
closing_bracket_extra_indent=closing_bracket_extra_indent,
single_quote=single_quote,
wrap_line_with_long_string=wrap_line_with_long_string,
collapse_nested_brackets=collapse_nested_brackets,
Expand Down
1 change: 1 addition & 0 deletions src/cercis/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
DEFAULT_INCLUDES = r"(\.pyi?|\.ipynb)$"
STDIN_PLACEHOLDER = "__BLACK_STDIN_FILENAME__"
DEFAULT_FUNCTION_DEFINITION_EXTRA_INDENT = True
DEFAULT_CLOSING_BRACKET_EXTRA_INDENT = False
DEFAULT_SINGLE_QUOTE = True
DEFAULT_WRAP_LINE_WITH_LONG_STRING = False
DEFAULT_COLLAPSE_NESTED_BRACKETS = True
Expand Down
16 changes: 10 additions & 6 deletions src/cercis/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,14 +945,14 @@ def bracket_split_build_line(
expected.
"""
result = Line(mode=original.mode, depth=original.depth)
if component is _BracketSplitComponent.body:
result.inside_brackets = True

if mode.function_definition_extra_indent and original.is_def:
additional_depth = 2
else:
additional_depth = 1
if mode.function_definition_extra_indent and original.is_def:
additional_depth = 2
else:
additional_depth = 1

if component is _BracketSplitComponent.body:
result.inside_brackets = True
result.depth += additional_depth

if leaves:
Expand Down Expand Up @@ -993,6 +993,10 @@ def bracket_split_build_line(
leaves_to_track: Set[LeafID] = set()
if component is _BracketSplitComponent.head:
leaves_to_track = get_leaves_inside_matching_brackets(leaves)

if mode.closing_bracket_extra_indent and component is _BracketSplitComponent.tail:
result.depth += additional_depth

# Populate the line
for leaf in leaves:
result.append(
Expand Down
2 changes: 2 additions & 0 deletions src/cercis/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import Final

from cercis.const import (
DEFAULT_CLOSING_BRACKET_EXTRA_INDENT,
DEFAULT_COLLAPSE_NESTED_BRACKETS,
DEFAULT_FUNCTION_DEFINITION_EXTRA_INDENT,
DEFAULT_LINE_LENGTH,
Expand Down Expand Up @@ -192,6 +193,7 @@ class Mode:
python_cell_magics: Set[str] = field(default_factory=set)
preview: bool = False
function_definition_extra_indent: bool = DEFAULT_FUNCTION_DEFINITION_EXTRA_INDENT
closing_bracket_extra_indent: bool = DEFAULT_CLOSING_BRACKET_EXTRA_INDENT
single_quote: bool = DEFAULT_SINGLE_QUOTE
wrap_line_with_long_string: bool = DEFAULT_WRAP_LINE_WITH_LONG_STRING
collapse_nested_brackets: bool = DEFAULT_COLLAPSE_NESTED_BRACKETS
Expand Down
114 changes: 114 additions & 0 deletions tests/data/configurable_cases/closing_bracket_indent/extra_indent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Line length limit: 30 chars,
# which is right here: |


def function(arg1: int,
arg2: float,
arg3_with_very_long_name: list):
print('Hello world')


def func2(arg1: int,
arg2: float,
arg3_with_very_long_name: list) -> None:
print('Hello world')


result = func2(12345, 3.1415926, [1, 2, 3])

result = [123, 456, 789, -123, -456]

something = {'a': 1, 'b': 2, 'c': 3}

some_tuple = (1, 2, 3, 4, 5, 6, 7, 8)


class A:
def __init__(self, a, b, c, d):
print('hello world')


class B:
def __init__(self, a, b, c, d,):
print('hello world')


# Line length limit: 30 chars,
# which is right here: |


# output


# Line length limit: 30 chars,
# which is right here: |


def function(
arg1: int,
arg2: float,
arg3_with_very_long_name: list,
):
print('Hello world')


def func2(
arg1: int,
arg2: float,
arg3_with_very_long_name: list,
) -> None:
print('Hello world')


result = func2(
12345,
3.1415926,
[1, 2, 3],
)

result = [
123,
456,
789,
-123,
-456,
]

something = {
'a': 1,
'b': 2,
'c': 3,
}

some_tuple = (
1,
2,
3,
4,
5,
6,
7,
8,
)


class A:
def __init__(
self, a, b, c, d
):
print('hello world')


class B:
def __init__(
self,
a,
b,
c,
d,
):
print('hello world')


# Line length limit: 30 chars,
# which is right here: |
Loading

0 comments on commit 8ce1cce

Please sign in to comment.