Skip to content

Commit

Permalink
Use over_indented_size for adjusting indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ukyen8 committed Sep 20, 2024
1 parent f90a8b7 commit 14dcae1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
13 changes: 13 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pydocstyle/D208.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ def under_indent_quotes_same_line(a: int) -> None:
A parameter."""
pass



def right_indent_quotes_same_line_with_multiple_empty_lines(a: int) -> None:
"""Foo.
Parameters
----------
a : int
A parameter."""
pass
28 changes: 18 additions & 10 deletions 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,21 +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.text_len();
// When a non-space character is on the last line, the trailing quotes are also on the same line.
// No diagnostic is added if the indentation difference equals the indent size.
if last.chars().any(|c| !c.is_whitespace()) {
let prev_line_indent = leading_space(&lines[lines.len() - 2]);
let prev_line_indent_size = prev_line_indent.chars().count();
if line_indent_size - prev_line_indent_size == docstring_indent_size {
return;
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;
}
}
offset_len = line_indent.text_len() - prev_line_indent.text_len();
}
let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(last.start()));
let indent = clean_space(docstring.indentation);
let range = TextRange::at(last.start(), offset_len);
let range =
TextRange::at(last.start(), TextSize::new(offset_len.try_into().unwrap()));
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 @@ -115,4 +115,5 @@ D208.py:45:1: D208 [*] Docstring is over-indented
45 |- A parameter."""
45 |+ A parameter."""
46 46 | pass
47 47 |
47 47 |
48 48 |

0 comments on commit 14dcae1

Please sign in to comment.