From 5fd9a74d787077c1c924450a7cda107c2462d4bb Mon Sep 17 00:00:00 2001 From: Zhengping Che Date: Tue, 29 Aug 2023 11:56:52 +0800 Subject: [PATCH] fix: indentation of multiline string (#10) closes: #8 --- .pre-commit-config.yaml | 2 +- .pylintrc | 2 +- README.md | 2 +- src/asort/api.py | 2 +- tests/integration/__snapshots__/test_cli.ambr | 42 +++++++++++++++---- .../fixtures/multi_line_string_indentation.py | 20 +++++++++ 6 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 tests/integration/fixtures/multi_line_string_indentation.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 803a07c..10a9c14 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ repos: hooks: - id: black - repo: https://github.com/pycqa/isort - rev: 5.10.1 + rev: 5.12.0 hooks: - id: isort - repo: https://github.com/pre-commit/mirrors-mypy diff --git a/.pylintrc b/.pylintrc index 883e30d..760e401 100644 --- a/.pylintrc +++ b/.pylintrc @@ -13,7 +13,7 @@ ignore=CVS # Add files or directories matching the regex patterns to the ignore-list. The # regex matches against paths and can be in Posix or Windows format. -ignore-paths= +ignore-paths=tests,docs # Files or directories matching the regex patterns are skipped. The regex # matches against base names, not paths. diff --git a/README.md b/README.md index 09b6a5d..6db47e5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ asort for `__all__` your `__all__` lists asort is a Python utility / library to sort imports items in an `__all__` list alphabetically It provides a command line utility, Python library and pre-commit support to -quickly sort all your imports. It requires Python 3.7+ to run but supports formatting +quickly sort all your imports. It requires Python 3.7+ to run but supports formatting any version of Python code. Works seemlessly with [black](https://github.com/psf/black) and [isort](https://github.com/PyCQA/isort) with no configuration needed diff --git a/src/asort/api.py b/src/asort/api.py index 925bb28..3d1a29b 100644 --- a/src/asort/api.py +++ b/src/asort/api.py @@ -228,7 +228,7 @@ def write_stream(processed_tokens: Iterable[TokenInfo], output_stream: TextIO) - whitespace_len = token.start[1] - prev_token.end[1] else: whitespace_len = len(token.line) - len(token.line.lstrip()) - if token.string.isspace() or token.start[0] != token.end[0]: + if token.string.isspace(): whitespace_len = 0 output_stream.write(" " * whitespace_len) diff --git a/tests/integration/__snapshots__/test_cli.ambr b/tests/integration/__snapshots__/test_cli.ambr index 37f39e7..18017c2 100644 --- a/tests/integration/__snapshots__/test_cli.ambr +++ b/tests/integration/__snapshots__/test_cli.ambr @@ -35,6 +35,34 @@ True # --- # name: test_cli_output.2 + ''' + class SingleLine: + """Single line of docstring.""" + + var = ["1", "2"] + + + class Multiline: + """Multiple lines of docstring. + + Used to test the docstring with multiple lines. + """ + + var = ["1", "2"] + """Test string in + multiple lines.""" + + """Test string in a single line.""" + + """Test \ + string in a single line.""" + + ''' +# --- +# name: test_cli_output.3 + False +# --- +# name: test_cli_output.4 ''' __all__ = [ # comment "Today", # comment @@ -44,33 +72,33 @@ ''' # --- -# name: test_cli_output.3 +# name: test_cli_output.5 True # --- -# name: test_cli_output.4 +# name: test_cli_output.6 ''' __all__ = ["1", "2", "5", "9"] ; __all__ = ["2", "3", "9"] # fmt: skip ''' # --- -# name: test_cli_output.5 +# name: test_cli_output.7 True # --- -# name: test_cli_output.6 +# name: test_cli_output.8 ''' __all__ = ["One", "Zed"] ''' # --- -# name: test_cli_output.7 +# name: test_cli_output.9 False # --- -# name: test_cli_output.8 +# name: test_cli_output.10 ''' __all__ = ["Today", "Yesterday", "_Thursday"] # comment ''' # --- -# name: test_cli_output.9 +# name: test_cli_output.11 True # --- diff --git a/tests/integration/fixtures/multi_line_string_indentation.py b/tests/integration/fixtures/multi_line_string_indentation.py new file mode 100644 index 0000000..874c79d --- /dev/null +++ b/tests/integration/fixtures/multi_line_string_indentation.py @@ -0,0 +1,20 @@ +class SingleLine: + """Single line of docstring.""" + + var = ["1", "2"] + + +class Multiline: + """Multiple lines of docstring. + + Used to test the docstring with multiple lines. + """ + + var = ["1", "2"] + """Test string in + multiple lines.""" + + """Test string in a single line.""" + + """Test \ + string in a single line."""