Strip whitespace when injecting D209 newline (#1967)

Closes #1963.
This commit is contained in:
Charlie Marsh 2023-01-18 12:09:17 -05:00 committed by GitHub
parent 5a7d8c25f4
commit 1ab0273aa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 2 deletions

View file

@ -579,3 +579,12 @@ def multiline_trailing_and_leading_space():
"or exclamation point (not '\"')") "or exclamation point (not '\"')")
def endswith_quote(): def endswith_quote():
"""Whitespace at the end, but also a quote" """ """Whitespace at the end, but also a quote" """
@expect('D209: Multi-line docstring closing quotes should be on a separate '
'line')
@expect('D213: Multi-line docstring summary should start at the second line')
def asdfljdjgf24():
"""Summary.
Description. """

View file

@ -528,12 +528,25 @@ pub fn newline_after_last_paragraph(checker: &mut Checker, docstring: &Docstring
); );
if checker.patch(diagnostic.kind.code()) { if checker.patch(diagnostic.kind.code()) {
// Insert a newline just before the end-quote(s). // Insert a newline just before the end-quote(s).
let num_trailing_quotes = "'''".len();
let num_trailing_spaces = last_line
.chars()
.rev()
.skip(num_trailing_quotes)
.take_while(|c| c.is_whitespace())
.count();
let content = format!("\n{}", whitespace::clean(docstring.indentation)); let content = format!("\n{}", whitespace::clean(docstring.indentation));
diagnostic.amend(Fix::insertion( diagnostic.amend(Fix::replacement(
content, content,
Location::new( Location::new(
docstring.expr.end_location.unwrap().row(), docstring.expr.end_location.unwrap().row(),
docstring.expr.end_location.unwrap().column() - "\"\"\"".len(), docstring.expr.end_location.unwrap().column()
- num_trailing_spaces
- num_trailing_quotes,
),
Location::new(
docstring.expr.end_location.unwrap().row(),
docstring.expr.end_location.unwrap().column() - num_trailing_quotes,
), ),
)); ));
} }

View file

@ -19,4 +19,21 @@ expression: diagnostics
row: 283 row: 283
column: 16 column: 16
parent: ~ parent: ~
- kind:
NewLineAfterLastParagraph: ~
location:
row: 588
column: 4
end_location:
row: 590
column: 21
fix:
content: "\n "
location:
row: 590
column: 16
end_location:
row: 590
column: 18
parent: ~

View file

@ -192,4 +192,14 @@ expression: diagnostics
column: 7 column: 7
fix: ~ fix: ~
parent: ~ parent: ~
- kind:
MultiLineSummarySecondLine: ~
location:
row: 588
column: 4
end_location:
row: 590
column: 21
fix: ~
parent: ~