Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indentation error when trailing quotes are not on separate line (… #13372

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pydocstyle/D208.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,47 @@ def memory_test():
"""
   参数含义:precision:精确到小数点后几位
"""


def right_indent_quotes_same_line(a: int) -> None:
"""Foo.

Parameters
----------
a : int
A parameter."""
pass


def over_indent_quotes_same_line(a: int) -> None:
"""Foo.

Parameters
----------
a : int
A parameter."""
pass


def under_indent_quotes_same_line(a: int) -> None:
"""Foo.

Parameters
----------
a : int
A parameter."""
pass



def right_indent_quotes_same_line_with_multiple_empty_lines(a: int) -> None:
"""Foo.
Parameters
----------
a : int




A parameter."""
pass
21 changes: 20 additions & 1 deletion crates/ruff_linter/src/rules/pydocstyle/rules/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ruff_python_ast::docstrings::{clean_space, leading_space};
use ruff_source_file::NewlineWithTrailingNewline;
use ruff_text_size::{Ranged, TextSize};
use ruff_text_size::{TextLen, TextRange};
use std::cmp::Ordering;

use crate::checkers::ast::Checker;
use crate::docstrings::Docstring;
Expand Down Expand Up @@ -291,10 +292,28 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
let line_indent = leading_space(last);
let line_indent_size = line_indent.chars().count();
if line_indent_size > docstring_indent_size {
let mut offset_len = line_indent_size;
// The trailing quotes are not on a separate line.
if line_indent.text_len() != last.text_len() {
over_indented_size =
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size);
Comment on lines +298 to +299
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid overriding the outer variable and instead shadow it

Suggested change
over_indented_size =
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size);
let over_indented_size =
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size);

match over_indented_size.cmp(&docstring_indent_size) {
Ordering::Equal => {
return;
}
Comment on lines +301 to +303
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here that explains when the indent sizes are equal and why we return in that case? I'm having a hard time understanding what's happening, and a comment might also be useful for future readers.

Ordering::Less => {
offset_len = over_indented_size;
}
Ordering::Greater => {
offset_len -= over_indented_size - docstring_indent_size;
}
}
}
Comment on lines +295 to +311
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We can avoid mutating the variable by using let

Suggested change
let mut offset_len = line_indent_size;
// The trailing quotes are not on a separate line.
if line_indent.text_len() != last.text_len() {
over_indented_size =
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size);
match over_indented_size.cmp(&docstring_indent_size) {
Ordering::Equal => {
return;
}
Ordering::Less => {
offset_len = over_indented_size;
}
Ordering::Greater => {
offset_len -= over_indented_size - docstring_indent_size;
}
}
}
// The trailing quotes are not on a separate line.
let offset_len = if line_indent.text_len() != last.text_len() {
over_indented_size =
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size);
match over_indented_size.cmp(&docstring_indent_size) {
Ordering::Equal => {
return;
}
Ordering::Less => over_indented_size,
Ordering::Greater => line_indent_size - over_indented_size - docstring_indent_size
}
} else {
line_indent_size
};

let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(last.start()));
let indent = clean_space(docstring.indentation);
let range = TextRange::at(last.start(), line_indent.text_len());
let range =
TextRange::at(last.start(), TextSize::new(offset_len.try_into().unwrap()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of offset_len here as a byte offset is incorrect because line_indent_size and docstring_indent_size are both character offsets.

We'll need to do something similar to

let offset = checker
.locator()
.after(line.start())
.chars()
.take(docstring.indentation.chars().count() + over_indented_size)
.map(TextLen::text_len)
.sum::<TextSize>();

let edit = if indent.is_empty() {
Edit::range_deletion(range)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,42 @@ D208.py:10:1: D208 [*] Docstring is over-indented
12 12 |
13 13 | def memory_test():

D208.py:35:1: D208 [*] Docstring is over-indented
|
33 | ----------
34 | a : int
35 | A parameter."""
| D208
36 | pass
|
= help: Remove over-indentation

ℹ Safe fix
32 32 | Parameters
33 33 | ----------
34 34 | a : int
35 |- A parameter."""
35 |+ A parameter."""
36 36 | pass
37 37 |
38 38 |

D208.py:45:1: D208 [*] Docstring is over-indented
|
43 | ----------
44 | a : int
45 | A parameter."""
| D208
46 | pass
|
= help: Remove over-indentation

ℹ Safe fix
42 42 | Parameters
43 43 | ----------
44 44 | a : int
45 |- A parameter."""
45 |+ A parameter."""
46 46 | pass
47 47 |
48 48 |
Loading