Skip to content

Commit 2ff2a54

Browse files
committed
test: update a few indentation related diagnostics
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.
1 parent 17f01a4 commit 2ff2a54

4 files changed

+84
-60
lines changed

crates/ruff_linter/src/rules/pydocstyle/rules/indent.rs

+33-6
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,20 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
223223
// We report under-indentation on every line. This isn't great, but enables
224224
// fix.
225225
if (is_last || !is_blank) && line_indent_size < docstring_indent_size {
226-
let mut diagnostic =
227-
Diagnostic::new(UnderIndentation, TextRange::empty(line.start()));
226+
// Previously, this used `TextRange::empty(line.start())`,
227+
// but this creates an offset immediately after the line
228+
// terminator. Probably, our renderer should create an
229+
// annotation that points to the beginning of the following
230+
// line. But it doesn't at present and this have proved
231+
// difficult to fix without regressing other cases. So for now,
232+
// we work around this by creating a range that points at the
233+
// first codepoint in the corresponding line. This makes the
234+
// renderer do what we want. ---AG
235+
let start = line.start();
236+
let end = checker
237+
.locator()
238+
.ceil_char_boundary(start + TextSize::from(1));
239+
let mut diagnostic = Diagnostic::new(UnderIndentation, TextRange::new(start, end));
228240
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
229241
clean_space(docstring.indentation),
230242
TextRange::at(line.start(), line_indent.text_len()),
@@ -281,8 +293,16 @@ pub(crate) fn indent(checker: &mut Checker, docstring: &Docstring) {
281293

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

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

325345
let is_indent_only = line_indent.len() == last.len();
326346
if last_line_over_indent > 0 && is_indent_only {
327-
let mut diagnostic =
328-
Diagnostic::new(OverIndentation, TextRange::empty(last.start()));
347+
// We ensure that our range points to the first character of
348+
// the line instead of the empty spance immediately preceding
349+
// the line. See above for how we handle under indentation for
350+
// more explanation. ---AG
351+
let start = last.start();
352+
let end = checker
353+
.locator()
354+
.ceil_char_boundary(start + TextSize::from(1));
355+
let mut diagnostic = Diagnostic::new(OverIndentation, TextRange::new(start, end));
329356
let indent = clean_space(docstring.indentation);
330357
let range = TextRange::at(last.start(), line_indent.text_len());
331358
let edit = if indent.is_empty() {

crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D207_D.py.snap

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
---
22
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
3-
snapshot_kind: text
43
---
54
D.py:232:1: D207 [*] Docstring is under-indented
65
|
76
230 | """Summary.
8-
231 |
7+
231 |
98
232 | Description.
10-
| D207
11-
233 |
9+
| ^ D207
10+
233 |
1211
234 | """
1312
|
1413
= help: Increase indentation
@@ -26,9 +25,9 @@ D.py:232:1: D207 [*] Docstring is under-indented
2625
D.py:244:1: D207 [*] Docstring is under-indented
2726
|
2827
242 | Description.
29-
243 |
28+
243 |
3029
244 | """
31-
| D207
30+
| ^ D207
3231
|
3332
= help: Increase indentation
3433

@@ -45,9 +44,9 @@ D.py:244:1: D207 [*] Docstring is under-indented
4544
D.py:440:1: D207 [*] Docstring is under-indented
4645
|
4746
438 | def docstring_start_in_same_line(): """First Line.
48-
439 |
47+
439 |
4948
440 | Second Line
50-
| D207
49+
| ^ D207
5150
441 | """
5251
|
5352
= help: Increase indentation
@@ -66,7 +65,7 @@ D.py:441:1: D207 [*] Docstring is under-indented
6665
|
6766
440 | Second Line
6867
441 | """
69-
| D207
68+
| ^ D207
7069
|
7170
= help: Increase indentation
7271

crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__D208_D.py.snap

+31-32
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
---
22
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
3-
snapshot_kind: text
43
---
54
D.py:252:1: D208 [*] Docstring is over-indented
65
|
76
250 | """Summary.
8-
251 |
7+
251 |
98
252 | Description.
10-
| D208
11-
253 |
9+
| ^ D208
10+
253 |
1211
254 | """
1312
|
1413
= help: Remove over-indentation
@@ -26,9 +25,9 @@ D.py:252:1: D208 [*] Docstring is over-indented
2625
D.py:264:1: D208 [*] Docstring is over-indented
2726
|
2827
262 | Description.
29-
263 |
28+
263 |
3029
264 | """
31-
| D208
30+
| ^ D208
3231
|
3332
= help: Remove over-indentation
3433

@@ -45,10 +44,10 @@ D.py:264:1: D208 [*] Docstring is over-indented
4544
D.py:272:1: D208 [*] Docstring is over-indented
4645
|
4746
270 | """Summary.
48-
271 |
47+
271 |
4948
272 | Description.
50-
| D208
51-
273 |
49+
| ^ D208
50+
273 |
5251
274 | """
5352
|
5453
= help: Remove over-indentation
@@ -66,9 +65,9 @@ D.py:272:1: D208 [*] Docstring is over-indented
6665
D.py:673:1: D208 [*] Docstring is over-indented
6766
|
6867
671 | """Summary.
69-
672 |
68+
672 |
7069
673 | This is overindented
71-
| D208
70+
| ^ D208
7271
674 | And so is this, but it we should preserve the extra space on this line relative
7372
675 | to the one before
7473
|
@@ -88,7 +87,7 @@ D.py:674:1: D208 [*] Docstring is over-indented
8887
|
8988
673 | This is overindented
9089
674 | And so is this, but it we should preserve the extra space on this line relative
91-
| D208
90+
| ^ D208
9291
675 | to the one before
9392
676 | """
9493
|
@@ -109,7 +108,7 @@ D.py:675:1: D208 [*] Docstring is over-indented
109108
673 | This is overindented
110109
674 | And so is this, but it we should preserve the extra space on this line relative
111110
675 | to the one before
112-
| D208
111+
| ^ D208
113112
676 | """
114113
|
115114
= help: Remove over-indentation
@@ -127,9 +126,9 @@ D.py:675:1: D208 [*] Docstring is over-indented
127126
D.py:682:1: D208 [*] Docstring is over-indented
128127
|
129128
680 | """Summary.
130-
681 |
129+
681 |
131130
682 | This is overindented
132-
| D208
131+
| ^ D208
133132
683 | And so is this, but it we should preserve the extra space on this line relative
134133
684 | to the one before
135134
|
@@ -149,7 +148,7 @@ D.py:683:1: D208 [*] Docstring is over-indented
149148
|
150149
682 | This is overindented
151150
683 | And so is this, but it we should preserve the extra space on this line relative
152-
| D208
151+
| ^ D208
153152
684 | to the one before
154153
685 | This is also overindented
155154
|
@@ -170,7 +169,7 @@ D.py:684:1: D208 [*] Docstring is over-indented
170169
682 | This is overindented
171170
683 | And so is this, but it we should preserve the extra space on this line relative
172171
684 | to the one before
173-
| D208
172+
| ^ D208
174173
685 | This is also overindented
175174
686 | And so is this, but it we should preserve the extra space on this line relative
176175
|
@@ -191,7 +190,7 @@ D.py:685:1: D208 [*] Docstring is over-indented
191190
683 | And so is this, but it we should preserve the extra space on this line relative
192191
684 | to the one before
193192
685 | This is also overindented
194-
| D208
193+
| ^ D208
195194
686 | And so is this, but it we should preserve the extra space on this line relative
196195
687 | to the one before
197196
|
@@ -212,7 +211,7 @@ D.py:686:1: D208 [*] Docstring is over-indented
212211
684 | to the one before
213212
685 | This is also overindented
214213
686 | And so is this, but it we should preserve the extra space on this line relative
215-
| D208
214+
| ^ D208
216215
687 | to the one before
217216
688 | """
218217
|
@@ -233,7 +232,7 @@ D.py:687:1: D208 [*] Docstring is over-indented
233232
685 | This is also overindented
234233
686 | And so is this, but it we should preserve the extra space on this line relative
235234
687 | to the one before
236-
| D208
235+
| ^ D208
237236
688 | """
238237
|
239238
= help: Remove over-indentation
@@ -251,9 +250,9 @@ D.py:687:1: D208 [*] Docstring is over-indented
251250
D.py:695:1: D208 [*] Docstring is over-indented
252251
|
253252
693 | """Summary.
254-
694 |
253+
694 |
255254
695 | This is overindented
256-
| D208
255+
| ^ D208
257256
696 | And so is this, but it we should preserve the extra space on this line relative
258257
697 | to the one before
259258
|
@@ -273,7 +272,7 @@ D.py:696:1: D208 [*] Docstring is over-indented
273272
|
274273
695 | This is overindented
275274
696 | And so is this, but it we should preserve the extra space on this line relative
276-
| D208
275+
| ^ D208
277276
697 | to the one before
278277
698 | And the relative indent here should be preserved too
279278
|
@@ -294,7 +293,7 @@ D.py:697:1: D208 [*] Docstring is over-indented
294293
695 | This is overindented
295294
696 | And so is this, but it we should preserve the extra space on this line relative
296295
697 | to the one before
297-
| D208
296+
| ^ D208
298297
698 | And the relative indent here should be preserved too
299298
699 | """
300299
|
@@ -315,7 +314,7 @@ D.py:698:1: D208 [*] Docstring is over-indented
315314
696 | And so is this, but it we should preserve the extra space on this line relative
316315
697 | to the one before
317316
698 | And the relative indent here should be preserved too
318-
| D208
317+
| ^ D208
319318
699 | """
320319
|
321320
= help: Remove over-indentation
@@ -333,9 +332,9 @@ D.py:698:1: D208 [*] Docstring is over-indented
333332
D.py:704:1: D208 [*] Docstring is over-indented
334333
|
335334
702 | """Summary.
336-
703 |
335+
703 |
337336
704 | This is overindented
338-
| D208
337+
| ^ D208
339338
705 | And so is this, but it we should preserve the extra space on this line relative
340339
706 | This is overindented
341340
|
@@ -355,7 +354,7 @@ D.py:705:1: D208 [*] Docstring is over-indented
355354
|
356355
704 | This is overindented
357356
705 | And so is this, but it we should preserve the extra space on this line relative
358-
| D208
357+
| ^ D208
359358
706 | This is overindented
360359
707 | This is overindented
361360
|
@@ -376,7 +375,7 @@ D.py:706:1: D208 [*] Docstring is over-indented
376375
704 | This is overindented
377376
705 | And so is this, but it we should preserve the extra space on this line relative
378377
706 | This is overindented
379-
| D208
378+
| ^ D208
380379
707 | This is overindented
381380
708 | """
382381
|
@@ -397,7 +396,7 @@ D.py:707:1: D208 [*] Docstring is over-indented
397396
705 | And so is this, but it we should preserve the extra space on this line relative
398397
706 | This is overindented
399398
707 | This is overindented
400-
| D208
399+
| ^ D208
401400
708 | """
402401
|
403402
= help: Remove over-indentation
@@ -415,9 +414,9 @@ D.py:707:1: D208 [*] Docstring is over-indented
415414
D.py:723:1: D208 [*] Docstring is over-indented
416415
|
417416
721 | """There's a non-breaking space (2-bytes) after 3 spaces (https://github.com/astral-sh/ruff/issues/9080).
418-
722 |
417+
722 |
419418
723 |     Returns:
420-
| D208
419+
| ^ D208
421420
724 | """
422421
|
423422
= help: Remove over-indentation

0 commit comments

Comments
 (0)