Skip to content

Commit

Permalink
Handle final indentation and periods in SummaryFormatter better
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Mar 15, 2022
1 parent 8c0d01e commit f95a1f2
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
18 changes: 16 additions & 2 deletions pydocstringformatter/formatting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,25 @@ def _treat_string(
quotes_length: Literal[1, 3],
) -> str:
"""Return a modified string."""
# TODO(#67): Handle identation at the end of a summary a little better

# Split summary and description
if "\n\n" in tokeninfo.string:
summary, description = tokeninfo.string.split("\n\n", maxsplit=1)

# Remove final indentation, ending quotes and new line
description = description[:-quotes_length]
if indent_length and description.endswith(indent_length * " "):
description = description[:-indent_length]
if description.endswith("\n"):
description = description[:-1]
else:
summary, description = tokeninfo.string, None

# Remove final indentation, ending quotes and new line
summary = summary[:-quotes_length]
if indent_length and summary.endswith(indent_length * " "):
summary = summary[:-indent_length]
if summary.endswith("\n"):
summary = summary[:-1]

# Remove opening quotes
summary = summary[quotes_length:]
Expand All @@ -131,4 +141,8 @@ def _treat_string(
docstring = f"{quotes}{new_summary}"
if description:
docstring += f"\n\n{description}"

# Determine whether ending quotes were initially on same or new line
if tokeninfo.string.splitlines()[-1] == indent_length * " " + quotes:
return f"{docstring}\n{indent_length * ' '}{quotes}"
return f"{docstring}{quotes}"
24 changes: 6 additions & 18 deletions pydocstringformatter/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,29 +161,17 @@ def _treat_summary(self, summary: str, indent_length: int) -> str:
)

# Handle summaries that end with a period and a direct new line
# but not a double new line.
elif summary[index + 1] == "\n":
# If this is the end of the docstring, don't do anything
if summary[index + 2 :] == indent_length * " ":
new_summary = summary
# Split between period and rest of docstring
else:
new_summary = summary[:index] + ".\n\n" + summary[index + 2 :]
new_summary = summary[:index] + ".\n\n" + summary[index + 2 :]

# Try to split on max length
if not new_summary and summary.count("\n") > self.config.max_summary_lines - 1:
lines = summary.splitlines()
new_summary = "\n".join(lines[: self.config.max_summary_lines])

# Handle summaries without any additional text beyond max lines
if lines[self.config.max_summary_lines] == indent_length * " ":
new_summary += "\n" + lines[self.config.max_summary_lines]

# Split between max lines and rest of docstring
else:
new_summary += "\n\n" + "\n".join(
lines[self.config.max_summary_lines :]
)
new_summary = (
"\n".join(lines[: self.config.max_summary_lines])
+ "\n\n"
+ "\n".join(lines[self.config.max_summary_lines :])
)

return new_summary or summary

Expand Down
2 changes: 2 additions & 0 deletions tests/data/format/summary_splitter/ending_quotes.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--split-summary-body
--no-closing-quotes
22 changes: 22 additions & 0 deletions tests/data/format/summary_splitter/ending_quotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def func():
"""We should keep the position of the quotes
as is.
"""


def func():
"""We should keep the position of the quotes
as is."""


def func():
"""We should keep the position of the quotes
as is.
"""


def func():
"""We should keep the position of the quotes
as is."""
24 changes: 24 additions & 0 deletions tests/data/format/summary_splitter/ending_quotes.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def func():
"""We should keep the position of the quotes.

as is.
"""


def func():
"""We should keep the position of the quotes.

as is."""


def func():
"""We should keep the position of the quotes.

as is.
"""


def func():
"""We should keep the position of the quotes.

as is."""

0 comments on commit f95a1f2

Please sign in to comment.