Skip to content

Commit

Permalink
Use section name range for all section-related docstring diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 2, 2024
1 parent 159bad7 commit 4cab597
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 947 deletions.
42 changes: 23 additions & 19 deletions crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ fn blanks_and_section_underline(
SectionUnderlineAfterName {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
let range = TextRange::new(context.following_range().start(), blank_lines_end);
// Delete any blank lines between the header and the underline.
Expand All @@ -1407,7 +1407,7 @@ fn blanks_and_section_underline(
SectionUnderlineMatchesSectionLength {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
// Replace the existing underline with a line of the appropriate length.
let content = format!(
Expand All @@ -1432,7 +1432,7 @@ fn blanks_and_section_underline(
SectionUnderlineNotOverIndented {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
// Replace the existing indentation with whitespace of the appropriate length.
let range = TextRange::at(
Expand Down Expand Up @@ -1467,15 +1467,15 @@ fn blanks_and_section_underline(
EmptyDocstringSection {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
));
}
} else if checker.enabled(Rule::BlankLinesBetweenHeaderAndContent) {
let mut diagnostic = Diagnostic::new(
BlankLinesBetweenHeaderAndContent {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
// Delete any blank lines between the header and content.
diagnostic.set_fix(Fix::safe_edit(Edit::deletion(
Expand All @@ -1491,7 +1491,7 @@ fn blanks_and_section_underline(
EmptyDocstringSection {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
));
}
}
Expand All @@ -1505,7 +1505,7 @@ fn blanks_and_section_underline(
DashedUnderlineAfterSection {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
// Add a dashed line (of the appropriate length) under the section header.
let content = format!(
Expand Down Expand Up @@ -1539,7 +1539,7 @@ fn blanks_and_section_underline(
BlankLinesBetweenHeaderAndContent {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
let range = TextRange::new(context.following_range().start(), blank_lines_end);
// Delete any blank lines between the header and content.
Expand All @@ -1556,7 +1556,7 @@ fn blanks_and_section_underline(
DashedUnderlineAfterSection {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
// Add a dashed line (of the appropriate length) under the section header.
let content = format!(
Expand All @@ -1577,7 +1577,7 @@ fn blanks_and_section_underline(
EmptyDocstringSection {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
));
}
}
Expand All @@ -1592,15 +1592,15 @@ fn common_section(
if checker.enabled(Rule::CapitalizeSectionName) {
let capitalized_section_name = context.kind().as_str();
if context.section_name() != capitalized_section_name {
let section_range = context.section_name_range();
let mut diagnostic = Diagnostic::new(
CapitalizeSectionName {
name: context.section_name().to_string(),
},
docstring.range(),
section_range,
);
// Replace the section title with the capitalized variant. This requires
// locating the start and end of the section name.
let section_range = context.section_name_range();
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
capitalized_section_name.to_string(),
section_range,
Expand All @@ -1612,16 +1612,17 @@ fn common_section(
if checker.enabled(Rule::SectionNotOverIndented) {
let leading_space = leading_space(context.summary_line());
if leading_space.len() > docstring.indentation.len() {
let section_range = context.section_name_range();
let mut diagnostic = Diagnostic::new(
SectionNotOverIndented {
name: context.section_name().to_string(),
},
docstring.range(),
section_range,
);

// Replace the existing indentation with whitespace of the appropriate length.
let content = clean_space(docstring.indentation);
let fix_range = TextRange::at(context.start(), leading_space.text_len());

diagnostic.set_fix(Fix::safe_edit(if content.is_empty() {
Edit::range_deletion(fix_range)
} else {
Expand All @@ -1641,11 +1642,12 @@ fn common_section(
.take_while(|line| line.trim().is_empty())
.count();
if num_blank_lines < 2 {
let section_range = context.section_name_range();
let mut diagnostic = Diagnostic::new(
NoBlankLineAfterSection {
name: context.section_name().to_string(),
},
docstring.range(),
section_range,
);
// Add a newline at the beginning of the next section.
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
Expand Down Expand Up @@ -1682,11 +1684,12 @@ fn common_section(
context.end(),
);

let section_range = context.section_name_range();
let mut diagnostic = Diagnostic::new(
BlankLineAfterLastSection {
name: context.section_name().to_string(),
},
docstring.range(),
section_range,
);
diagnostic.set_fix(Fix::safe_edit(edit));
checker.diagnostics.push(diagnostic);
Expand All @@ -1699,11 +1702,12 @@ fn common_section(
.previous_line()
.is_some_and(|line| line.trim().is_empty())
{
let section_range = context.section_name_range();
let mut diagnostic = Diagnostic::new(
NoBlankLineBeforeSection {
name: context.section_name().to_string(),
},
docstring.range(),
section_range,
);
// Add a blank line before the section.
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
Expand Down Expand Up @@ -1897,7 +1901,7 @@ fn numpy_section(
NewLineAfterSectionName {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
let section_range = context.section_name_range();
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(TextRange::at(
Expand Down Expand Up @@ -1931,7 +1935,7 @@ fn google_section(
SectionNameEndsInColon {
name: context.section_name().to_string(),
},
docstring.range(),
context.section_name_range(),
);
// Replace the suffix.
let section_name_range = context.section_name_range();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
---
D214_module.py:1:1: D214 [*] Section is over-indented ("Returns")
|
1 | / """A module docstring with D214 violations
2 | |
3 | | Returns
4 | | -----
5 | | valid returns
6 | |
7 | | Args
8 | | -----
9 | | valid args
10 | | """
| |___^ D214
11 |
12 | import os
|
= help: Remove over-indentation from "Returns"
D214_module.py:3:5: D214 [*] Section is over-indented ("Returns")
|
1 | """A module docstring with D214 violations
2 |
3 | Returns
| ^^^^^^^ D214
4 | -----
5 | valid returns
|
= help: Remove over-indentation from "Returns"

Safe fix
1 1 | """A module docstring with D214 violations
Expand All @@ -28,23 +21,16 @@ D214_module.py:1:1: D214 [*] Section is over-indented ("Returns")
5 5 | valid returns
6 6 |

D214_module.py:1:1: D214 [*] Section is over-indented ("Args")
|
1 | / """A module docstring with D214 violations
2 | |
3 | | Returns
4 | | -----
5 | | valid returns
6 | |
7 | | Args
8 | | -----
9 | | valid args
10 | | """
| |___^ D214
11 |
12 | import os
|
= help: Remove over-indentation from "Args"
D214_module.py:7:5: D214 [*] Section is over-indented ("Args")
|
5 | valid returns
6 |
7 | Args
| ^^^^ D214
8 | -----
9 | valid args
|
= help: Remove over-indentation from "Args"

Safe fix
4 4 | -----
Expand All @@ -55,5 +41,3 @@ D214_module.py:1:1: D214 [*] Section is over-indented ("Args")
8 8 | -----
9 9 | valid args
10 10 | """
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
---
sections.py:144:5: D214 [*] Section is over-indented ("Returns")
sections.py:146:9: D214 [*] Section is over-indented ("Returns")
|
142 | @expect("D214: Section is over-indented ('Returns')")
143 | def section_overindented(): # noqa: D416
144 | """Toggle the gizmo.
| _____^
145 | |
146 | | Returns
147 | | -------
148 | | A value of some sort.
149 | |
150 | | """
| |_______^ D214
144 | """Toggle the gizmo.
145 |
146 | Returns
| ^^^^^^^ D214
147 | -------
148 | A value of some sort.
|
= help: Remove over-indentation from "Returns"

Expand All @@ -27,18 +22,13 @@ sections.py:144:5: D214 [*] Section is over-indented ("Returns")
148 148 | A value of some sort.
149 149 |

sections.py:558:5: D214 [*] Section is over-indented ("Returns")
sections.py:563:9: D214 [*] Section is over-indented ("Returns")
|
557 | def titlecase_sub_section_header():
558 | """Below, `Returns:` should be considered a section header.
| _____^
559 | |
560 | | Args:
561 | | Here's a note.
562 | |
563 | | Returns:
564 | | """
| |_______^ D214
561 | Here's a note.
562 |
563 | Returns:
| ^^^^^^^ D214
564 | """
|
= help: Remove over-indentation from "Returns"

Expand All @@ -50,6 +40,4 @@ sections.py:558:5: D214 [*] Section is over-indented ("Returns")
563 |+ Returns:
564 564 | """
565 565 |
566 566 |


566 566 |
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
---
D215.py:1:1: D215 [*] Section underline is over-indented ("TODO")
D215.py:2:1: D215 [*] Section underline is over-indented ("TODO")
|
1 | / """
2 | | TODO:
3 | | -
4 | | """
| |___^ D215
1 | """
2 | TODO:
| ^^^^ D215
3 | -
4 | """
|
= help: Remove over-indentation from "TODO" underline

Expand All @@ -17,5 +17,3 @@ D215.py:1:1: D215 [*] Section underline is over-indented ("TODO")
3 |- -
3 |+
4 4 | """
Loading

0 comments on commit 4cab597

Please sign in to comment.