Skip to content

Commit

Permalink
test: update a few indentation related diagnostics
Browse files Browse the repository at this point in the history
Previously, these were pointing to the right place, but were missing the
`^`. With the `annotate-snippets` upgrade, the `^` was added, but they
started pointing to the end of the previous line instead of the
beginning of the following line. In this case, we really want it to
point to the beginning of the following line since we're calling out
indentation issues.

As in a prior commit, we fix this by tweaking the offsets emitted by the
lint itself. Instead of an empty range at the beginning of the line, we
point to the first character in the line. This "forces" the renderer to
point to the beginning of the line instead of the end of the preceding
line.

The end effect here is that the rendering is fixed by adding `^` in the
proper location.
  • Loading branch information
BurntSushi committed Jan 15, 2025
1 parent 17f01a4 commit 2ff2a54
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 60 deletions.
39 changes: 33 additions & 6 deletions crates/ruff_linter/src/rules/pydocstyle/rules/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,20 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
// We report under-indentation on every line. This isn't great, but enables
// fix.
if (is_last || !is_blank) && line_indent_size < docstring_indent_size {
let mut diagnostic =
Diagnostic::new(UnderIndentation, TextRange::empty(line.start()));
// Previously, this used `TextRange::empty(line.start())`,
// but this creates an offset immediately after the line
// terminator. Probably, our renderer should create an
// annotation that points to the beginning of the following
// line. But it doesn't at present and this have proved
// difficult to fix without regressing other cases. So for now,
// we work around this by creating a range that points at the
// first codepoint in the corresponding line. This makes the
// renderer do what we want. ---AG
let start = line.start();
let end = checker
.locator()
.ceil_char_boundary(start + TextSize::from(1));
let mut diagnostic = Diagnostic::new(UnderIndentation, TextRange::new(start, end));
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
clean_space(docstring.indentation),
TextRange::at(line.start(), line_indent.text_len()),
Expand Down Expand Up @@ -281,8 +293,16 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {

// We report over-indentation on every line. This isn't great, but
// enables the fix capability.
let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(line.start()));
//
// Also, we ensure that our range points to the first character
// of the line instead of the empty spance immediately
// preceding the line. See above for how we handle under
// indentation for more explanation. ---AG
let start = line.start();
let end = checker
.locator()
.ceil_char_boundary(start + TextSize::from(1));
let mut diagnostic = Diagnostic::new(OverIndentation, TextRange::new(start, end));

let edit = if indent.is_empty() {
// Delete the entire indent.
Expand Down Expand Up @@ -324,8 +344,15 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {

let is_indent_only = line_indent.len() == last.len();
if last_line_over_indent > 0 && is_indent_only {
let mut diagnostic =
Diagnostic::new(OverIndentation, TextRange::empty(last.start()));
// We ensure that our range points to the first character of
// the line instead of the empty spance immediately preceding
// the line. See above for how we handle under indentation for
// more explanation. ---AG
let start = last.start();
let end = checker
.locator()
.ceil_char_boundary(start + TextSize::from(1));
let mut diagnostic = Diagnostic::new(OverIndentation, TextRange::new(start, end));
let indent = clean_space(docstring.indentation);
let range = TextRange::at(last.start(), line_indent.text_len());
let edit = if indent.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
snapshot_kind: text
---
D.py:232:1: D207 [*] Docstring is under-indented
|
230 | """Summary.
231 |
231 |
232 | Description.
| D207
233 |
| ^ D207
233 |
234 | """
|
= help: Increase indentation
Expand All @@ -26,9 +25,9 @@ D.py:232:1: D207 [*] Docstring is under-indented
D.py:244:1: D207 [*] Docstring is under-indented
|
242 | Description.
243 |
243 |
244 | """
| D207
| ^ D207
|
= help: Increase indentation

Expand All @@ -45,9 +44,9 @@ D.py:244:1: D207 [*] Docstring is under-indented
D.py:440:1: D207 [*] Docstring is under-indented
|
438 | def docstring_start_in_same_line(): """First Line.
439 |
439 |
440 | Second Line
| D207
| ^ D207
441 | """
|
= help: Increase indentation
Expand All @@ -66,7 +65,7 @@ D.py:441:1: D207 [*] Docstring is under-indented
|
440 | Second Line
441 | """
| D207
| ^ D207
|
= help: Increase indentation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
snapshot_kind: text
---
D.py:252:1: D208 [*] Docstring is over-indented
|
250 | """Summary.
251 |
251 |
252 | Description.
| D208
253 |
| ^ D208
253 |
254 | """
|
= help: Remove over-indentation
Expand All @@ -26,9 +25,9 @@ D.py:252:1: D208 [*] Docstring is over-indented
D.py:264:1: D208 [*] Docstring is over-indented
|
262 | Description.
263 |
263 |
264 | """
| D208
| ^ D208
|
= help: Remove over-indentation

Expand All @@ -45,10 +44,10 @@ D.py:264:1: D208 [*] Docstring is over-indented
D.py:272:1: D208 [*] Docstring is over-indented
|
270 | """Summary.
271 |
271 |
272 | Description.
| D208
273 |
| ^ D208
273 |
274 | """
|
= help: Remove over-indentation
Expand All @@ -66,9 +65,9 @@ D.py:272:1: D208 [*] Docstring is over-indented
D.py:673:1: D208 [*] Docstring is over-indented
|
671 | """Summary.
672 |
672 |
673 | This is overindented
| D208
| ^ D208
674 | And so is this, but it we should preserve the extra space on this line relative
675 | to the one before
|
Expand All @@ -88,7 +87,7 @@ D.py:674:1: D208 [*] Docstring is over-indented
|
673 | This is overindented
674 | And so is this, but it we should preserve the extra space on this line relative
| D208
| ^ D208
675 | to the one before
676 | """
|
Expand All @@ -109,7 +108,7 @@ D.py:675:1: D208 [*] Docstring is over-indented
673 | This is overindented
674 | And so is this, but it we should preserve the extra space on this line relative
675 | to the one before
| D208
| ^ D208
676 | """
|
= help: Remove over-indentation
Expand All @@ -127,9 +126,9 @@ D.py:675:1: D208 [*] Docstring is over-indented
D.py:682:1: D208 [*] Docstring is over-indented
|
680 | """Summary.
681 |
681 |
682 | This is overindented
| D208
| ^ D208
683 | And so is this, but it we should preserve the extra space on this line relative
684 | to the one before
|
Expand All @@ -149,7 +148,7 @@ D.py:683:1: D208 [*] Docstring is over-indented
|
682 | This is overindented
683 | And so is this, but it we should preserve the extra space on this line relative
| D208
| ^ D208
684 | to the one before
685 | This is also overindented
|
Expand All @@ -170,7 +169,7 @@ D.py:684:1: D208 [*] Docstring is over-indented
682 | This is overindented
683 | And so is this, but it we should preserve the extra space on this line relative
684 | to the one before
| D208
| ^ D208
685 | This is also overindented
686 | And so is this, but it we should preserve the extra space on this line relative
|
Expand All @@ -191,7 +190,7 @@ D.py:685:1: D208 [*] Docstring is over-indented
683 | And so is this, but it we should preserve the extra space on this line relative
684 | to the one before
685 | This is also overindented
| D208
| ^ D208
686 | And so is this, but it we should preserve the extra space on this line relative
687 | to the one before
|
Expand All @@ -212,7 +211,7 @@ D.py:686:1: D208 [*] Docstring is over-indented
684 | to the one before
685 | This is also overindented
686 | And so is this, but it we should preserve the extra space on this line relative
| D208
| ^ D208
687 | to the one before
688 | """
|
Expand All @@ -233,7 +232,7 @@ D.py:687:1: D208 [*] Docstring is over-indented
685 | This is also overindented
686 | And so is this, but it we should preserve the extra space on this line relative
687 | to the one before
| D208
| ^ D208
688 | """
|
= help: Remove over-indentation
Expand All @@ -251,9 +250,9 @@ D.py:687:1: D208 [*] Docstring is over-indented
D.py:695:1: D208 [*] Docstring is over-indented
|
693 | """Summary.
694 |
694 |
695 | This is overindented
| D208
| ^ D208
696 | And so is this, but it we should preserve the extra space on this line relative
697 | to the one before
|
Expand All @@ -273,7 +272,7 @@ D.py:696:1: D208 [*] Docstring is over-indented
|
695 | This is overindented
696 | And so is this, but it we should preserve the extra space on this line relative
| D208
| ^ D208
697 | to the one before
698 | And the relative indent here should be preserved too
|
Expand All @@ -294,7 +293,7 @@ D.py:697:1: D208 [*] Docstring is over-indented
695 | This is overindented
696 | And so is this, but it we should preserve the extra space on this line relative
697 | to the one before
| D208
| ^ D208
698 | And the relative indent here should be preserved too
699 | """
|
Expand All @@ -315,7 +314,7 @@ D.py:698:1: D208 [*] Docstring is over-indented
696 | And so is this, but it we should preserve the extra space on this line relative
697 | to the one before
698 | And the relative indent here should be preserved too
| D208
| ^ D208
699 | """
|
= help: Remove over-indentation
Expand All @@ -333,9 +332,9 @@ D.py:698:1: D208 [*] Docstring is over-indented
D.py:704:1: D208 [*] Docstring is over-indented
|
702 | """Summary.
703 |
703 |
704 | This is overindented
| D208
| ^ D208
705 | And so is this, but it we should preserve the extra space on this line relative
706 | This is overindented
|
Expand All @@ -355,7 +354,7 @@ D.py:705:1: D208 [*] Docstring is over-indented
|
704 | This is overindented
705 | And so is this, but it we should preserve the extra space on this line relative
| D208
| ^ D208
706 | This is overindented
707 | This is overindented
|
Expand All @@ -376,7 +375,7 @@ D.py:706:1: D208 [*] Docstring is over-indented
704 | This is overindented
705 | And so is this, but it we should preserve the extra space on this line relative
706 | This is overindented
| D208
| ^ D208
707 | This is overindented
708 | """
|
Expand All @@ -397,7 +396,7 @@ D.py:707:1: D208 [*] Docstring is over-indented
705 | And so is this, but it we should preserve the extra space on this line relative
706 | This is overindented
707 | This is overindented
| D208
| ^ D208
708 | """
|
= help: Remove over-indentation
Expand All @@ -415,9 +414,9 @@ D.py:707:1: D208 [*] Docstring is over-indented
D.py:723:1: D208 [*] Docstring is over-indented
|
721 | """There's a non-breaking space (2-bytes) after 3 spaces (https://github.com/astral-sh/ruff/issues/9080).
722 |
722 |
723 |     Returns:
| D208
| ^ D208
724 | """
|
= help: Remove over-indentation
Expand Down
Loading

0 comments on commit 2ff2a54

Please sign in to comment.