Skip to content

Commit

Permalink
Work in progress for #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Jan 8, 2022
1 parent 0ded93a commit fa9e092
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 23 deletions.
6 changes: 6 additions & 0 deletions pydocstringformatter/formatting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
BeginningQuotesFormatter,
ClosingQuotesFormatter,
FinalPeriodFormatter,
PrettierDocstringFormatter,
RemoveClosingWhitespaces,
RemoveLeadingWhitespaces,
)

FORMATTERS: List[Formatter] = [
PrettierDocstringFormatter(),
RemoveLeadingWhitespaces(),
RemoveClosingWhitespaces(),
BeginningQuotesFormatter(),
ClosingQuotesFormatter(),
FinalPeriodFormatter(),
Expand Down
42 changes: 42 additions & 0 deletions pydocstringformatter/formatting/formatter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import textwrap
import tokenize

from pydocstringformatter.formatting.base import StringFormatter
Expand All @@ -16,6 +17,47 @@ def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
return new_string


class RemoveLeadingWhitespaces(StringFormatter):
"""Remove leading whitespace at the beginning of a docstring"""

name = "remove-leading-whitespaces"
optional = True

def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
return tokeninfo.string.lstrip()


class RemoveClosingWhitespaces(StringFormatter):
"""Remove closing whitespace at the beginning of a docstring"""

name = "remove-closing-whitespaces"
optional = True

def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
return tokeninfo.string.rstrip()


class PrettierDocstringFormatter(StringFormatter):
"""Return the docstring formatted on multiple line at ~=80 char/line"
Count the number of character, until 80
At 80 line break when the current word end and start another comment,
Or reset the counter if there is already a line break in the comment
"""

name = "rewrapped-docstring"
optional = True

def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
result = [
" ".join(w.split())
for w in textwrap.TextWrapper(
width=88, expand_tabs=False, break_on_hyphens=False, tabsize=0
).wrap(tokeninfo.string)
]
return "\n".join(result)


class ClosingQuotesFormatter(StringFormatter):
"""Fix the position of the closing quotes."""

Expand Down
1 change: 1 addition & 0 deletions pydocstringformatter/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def _format_file(self, filename: Path) -> bool:
if utils._is_docstring(new_tokeninfo, tokens[index - 1]):
for formatter in formatting.FORMATTERS:
if getattr(self.config, formatter.name):
# print(f'{new_tokeninfo=} before {formatter.__class__.__name__}')
new_tokeninfo = formatter.treat_token(new_tokeninfo)
changed_tokens.append(new_tokeninfo)

Expand Down
16 changes: 4 additions & 12 deletions tests/data/format/beginning_quote/class_docstring.py.out
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
class MyClass:
"""A multi-line
docstring
"""
"""A multi-line docstring"""

class InnerClass:
"""A multi-line
docstring
"""
"""A multi-line docstring"""


class MyClass:
"""A multi-line
docstring
"""
"""A multi-line docstring"""

class InnerClass:
"""A multi-line
docstring
"""
"""A multi-line docstring"""


class MyClass:
Expand Down
8 changes: 2 additions & 6 deletions tests/data/format/beginning_quote/function_docstrings.py.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ def func():


def func():
"""A multi-line
docstring
"""
"""A multi-line docstring"""

def inner_func():
"""A multi-line
docstring
"""
"""A multi-line docstring"""
4 changes: 1 addition & 3 deletions tests/data/format/beginning_quote/variable_docstring.py.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
MYVAR = 1
"""A multi-line
docstring
"""
"""A multi-line docstring"""
4 changes: 2 additions & 2 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def test_output_message_nothing_done(
) -> None:
"""Test that we emit the correct message when nothing was done"""
with open(test_file, "w", encoding="utf-8") as file:
file.write('"""A multi-line\ndocstring\n"""')
file.write('"""A multi-line docstring"""')
with open(test_file + "2", "w", encoding="utf-8") as file:
file.write('"""A multi-line\ndocstring\n"""')
file.write('"""A multi-line docstring"""')

pydocstringformatter.run_docstring_formatter(
[str(Path(test_file).parent), "--write"]
Expand Down

0 comments on commit fa9e092

Please sign in to comment.