Skip to content

Commit

Permalink
Convert byte offset to character offset and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ukyen8 committed Sep 23, 2024
1 parent 4bd9266 commit be9f2c4
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions crates/ruff_linter/src/rules/pydocstyle/rules/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,28 +292,58 @@ 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;
let offset_len = if line_indent.text_len() == last.text_len() {
line_indent_size
// The trailing quotes are not on a separate line.
if line_indent.text_len() != last.text_len() {
over_indented_size =
} else {
let over_indented_size =
std::cmp::min(line_indent_size - docstring_indent_size, over_indented_size);
match over_indented_size.cmp(&docstring_indent_size) {
// If indentation is exactly the same, no changes are needed.
// Example:
// ```
// def f():
// """A docstring.
// Args:
// ^^^^This line is correctly indented (4 spaces)."""
// ```
Ordering::Equal => {
return;
}
Ordering::Less => {
offset_len = over_indented_size;
}
// If the indentation is less then docstring_indent_size,
// replace the over-indentation with `docstring.indentation`.
// Example:
// ```
// def f():
// """A docstring.
// ^^This line is slightly over-indented (2 spaces)."""
// ```
Ordering::Less => over_indented_size,
// If the indentation is significantly larger than needed,
// reduce the extra spaces down to match `docstring.indentation`.
// Example:
// ```
// def f():
// """A docstring.
// ^^^^^^^^This line is over-indented by 8 spaces; it needs fixing."""
// ```
Ordering::Greater => {
offset_len -= over_indented_size - docstring_indent_size;
line_indent_size - (over_indented_size - docstring_indent_size)
}
}
}
};

let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(last.start()));
let indent = clean_space(docstring.indentation);
let range =
TextRange::at(last.start(), TextSize::new(offset_len.try_into().unwrap()));
let offset = checker
.locator()
.after(last.start())
.chars()
.take(offset_len)
.map(TextLen::text_len)
.sum::<TextSize>();
let range = TextRange::at(last.start(), offset);
let edit = if indent.is_empty() {
Edit::range_deletion(range)
} else {
Expand Down

0 comments on commit be9f2c4

Please sign in to comment.