Skip to content

Commit f95a1f2

Browse files
committed
Handle final indentation and periods in SummaryFormatter better
1 parent 8c0d01e commit f95a1f2

File tree

5 files changed

+70
-20
lines changed

5 files changed

+70
-20
lines changed

pydocstringformatter/formatting/base.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,25 @@ def _treat_string(
112112
quotes_length: Literal[1, 3],
113113
) -> str:
114114
"""Return a modified string."""
115-
# TODO(#67): Handle identation at the end of a summary a little better
116-
117115
# Split summary and description
118116
if "\n\n" in tokeninfo.string:
119117
summary, description = tokeninfo.string.split("\n\n", maxsplit=1)
118+
119+
# Remove final indentation, ending quotes and new line
120120
description = description[:-quotes_length]
121+
if indent_length and description.endswith(indent_length * " "):
122+
description = description[:-indent_length]
123+
if description.endswith("\n"):
124+
description = description[:-1]
121125
else:
122126
summary, description = tokeninfo.string, None
127+
128+
# Remove final indentation, ending quotes and new line
123129
summary = summary[:-quotes_length]
130+
if indent_length and summary.endswith(indent_length * " "):
131+
summary = summary[:-indent_length]
132+
if summary.endswith("\n"):
133+
summary = summary[:-1]
124134

125135
# Remove opening quotes
126136
summary = summary[quotes_length:]
@@ -131,4 +141,8 @@ def _treat_string(
131141
docstring = f"{quotes}{new_summary}"
132142
if description:
133143
docstring += f"\n\n{description}"
144+
145+
# Determine whether ending quotes were initially on same or new line
146+
if tokeninfo.string.splitlines()[-1] == indent_length * " " + quotes:
147+
return f"{docstring}\n{indent_length * ' '}{quotes}"
134148
return f"{docstring}{quotes}"

pydocstringformatter/formatting/formatter.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -161,29 +161,17 @@ def _treat_summary(self, summary: str, indent_length: int) -> str:
161161
)
162162

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

173167
# Try to split on max length
174168
if not new_summary and summary.count("\n") > self.config.max_summary_lines - 1:
175169
lines = summary.splitlines()
176-
new_summary = "\n".join(lines[: self.config.max_summary_lines])
177-
178-
# Handle summaries without any additional text beyond max lines
179-
if lines[self.config.max_summary_lines] == indent_length * " ":
180-
new_summary += "\n" + lines[self.config.max_summary_lines]
181-
182-
# Split between max lines and rest of docstring
183-
else:
184-
new_summary += "\n\n" + "\n".join(
185-
lines[self.config.max_summary_lines :]
186-
)
170+
new_summary = (
171+
"\n".join(lines[: self.config.max_summary_lines])
172+
+ "\n\n"
173+
+ "\n".join(lines[self.config.max_summary_lines :])
174+
)
187175

188176
return new_summary or summary
189177

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--split-summary-body
2+
--no-closing-quotes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def func():
2+
"""We should keep the position of the quotes
3+
as is.
4+
"""
5+
6+
7+
def func():
8+
"""We should keep the position of the quotes
9+
as is."""
10+
11+
12+
def func():
13+
"""We should keep the position of the quotes
14+
15+
as is.
16+
"""
17+
18+
19+
def func():
20+
"""We should keep the position of the quotes
21+
22+
as is."""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def func():
2+
"""We should keep the position of the quotes.
3+
4+
as is.
5+
"""
6+
7+
8+
def func():
9+
"""We should keep the position of the quotes.
10+
11+
as is."""
12+
13+
14+
def func():
15+
"""We should keep the position of the quotes.
16+
17+
as is.
18+
"""
19+
20+
21+
def func():
22+
"""We should keep the position of the quotes.
23+
24+
as is."""

0 commit comments

Comments
 (0)