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 3, 2022
1 parent 8ded4cd commit e6290d6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
6 changes: 6 additions & 0 deletions pydocstringformatter/formatting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
from pydocstringformatter.formatting.formatter import (
BeginningQuotesFormatter,
ClosingQuotesFormatter,
PrettierDocstringFormatter,
RemoveClosingWhitespaces,
RemoveLeadingWhitespaces,
)

FORMATTERS = [
PrettierDocstringFormatter(),
RemoveLeadingWhitespaces(),
RemoveClosingWhitespaces(),
BeginningQuotesFormatter(),
ClosingQuotesFormatter(),
]
33 changes: 33 additions & 0 deletions pydocstringformatter/formatting/formatter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import abc
import re
import textwrap
import tokenize


Expand Down Expand Up @@ -38,6 +39,38 @@ def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
return new_string


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

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


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

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
"""

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).lstrip().rstrip()


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 @@ -49,6 +49,7 @@ def _format_file(self, filename: Path) -> bool:

if utils._is_docstring(new_tokeninfo, tokens[index - 1]):
for formatter in formatting.FORMATTERS:
# 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"""

0 comments on commit e6290d6

Please sign in to comment.