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

Convert over-indentation rule to use number of characters #8983

Merged
merged 1 commit into from
Dec 3, 2023
Merged
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
6 changes: 6 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pydocstyle/D208.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ class Platform:
Args:
    Returns:
"""


def memory_test():
"""
   参数含义:precision:精确到小数点后几位
"""
28 changes: 13 additions & 15 deletions crates/ruff_linter/src/rules/pydocstyle/rules/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
let mut has_seen_tab = docstring.indentation.contains('\t');
let mut is_over_indented = true;
let mut over_indented_lines = vec![];
let mut over_indented_offset = usize::MAX;
let mut over_indented_size = usize::MAX;

let docstring_indent_size = docstring.indentation.chars().count();
for i in 0..lines.len() {
// First lines and continuations doesn't need any indentation.
if i == 0 || lines[i - 1].ends_with('\\') {
Expand All @@ -189,6 +190,7 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
}

let line_indent = leading_space(line);
let line_indent_size = line_indent.chars().count();

// We only report tab indentation once, so only check if we haven't seen a tab
// yet.
Expand All @@ -197,9 +199,7 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
if checker.enabled(Rule::UnderIndentation) {
// We report under-indentation on every line. This isn't great, but enables
// fix.
if (i == lines.len() - 1 || !is_blank)
&& line_indent.len() < docstring.indentation.len()
{
if (i == lines.len() - 1 || !is_blank) && line_indent_size < docstring_indent_size {
let mut diagnostic =
Diagnostic::new(UnderIndentation, TextRange::empty(line.start()));
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
Expand All @@ -217,14 +217,12 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
// until we've viewed all the lines, so for now, just track
// the over-indentation status of every line.
if i < lines.len() - 1 {
if line_indent.len() > docstring.indentation.len() {
if line_indent_size > docstring_indent_size {
over_indented_lines.push(line);

// Track the _smallest_ offset we see, in terms of characters.
over_indented_offset = std::cmp::min(
line_indent.chars().count() - docstring.indentation.chars().count(),
over_indented_offset,
);
over_indented_size =
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size);
} else {
is_over_indented = false;
}
Expand All @@ -250,6 +248,7 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
// enables the fix capability.
let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(line.start()));

let edit = if indent.is_empty() {
// Delete the entire indent.
Edit::range_deletion(TextRange::at(line.start(), line_indent.text_len()))
Expand All @@ -259,13 +258,11 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
.locator()
.after(line.start() + indent.text_len())
.chars()
.take(over_indented_offset)
.take(over_indented_size)
.map(TextLen::text_len)
.sum::<TextSize>();
Edit::range_replacement(
indent.clone(),
TextRange::at(line.start(), indent.text_len() + offset),
)
let range = TextRange::at(line.start(), indent.text_len() + offset);
Edit::range_replacement(indent, range)
};
diagnostic.set_fix(Fix::safe_edit(edit));
checker.diagnostics.push(diagnostic);
Expand All @@ -275,7 +272,8 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
// If the last line is over-indented...
if let Some(last) = lines.last() {
let line_indent = leading_space(last);
if line_indent.len() > docstring.indentation.len() {
let line_indent_size = line_indent.chars().count();
if line_indent_size > docstring_indent_size {
let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(last.start()));
let indent = clean_space(docstring.indentation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ D208.py:8:1: D208 [*] Docstring is over-indented
8 |+ Args:
9 9 |     Returns:
10 10 | """
11 11 |

D208.py:9:1: D208 [*] Docstring is over-indented
|
Expand All @@ -55,6 +56,8 @@ D208.py:9:1: D208 [*] Docstring is over-indented
9 |-     Returns:
9 |+ Returns:
10 10 | """
11 11 |
12 12 |

D208.py:10:1: D208 [*] Docstring is over-indented
|
Expand All @@ -71,5 +74,8 @@ D208.py:10:1: D208 [*] Docstring is over-indented
9 9 |     Returns:
10 |- """
10 |+ """
11 11 |
12 12 |
13 13 | def memory_test():


Loading