Skip to content

Commit

Permalink
Adjust for new TextRange-based locations
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Apr 27, 2023
1 parent 17db2e2 commit d296b26
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions crates/ruff/src/docstrings/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ struct SectionContextData {
summary_full_end: TextSize,
}

#[derive(Clone)]
pub struct SectionContext<'a> {
data: &'a SectionContextData,
docstring_body: DocstringBody<'a>,
Expand Down
52 changes: 39 additions & 13 deletions crates/ruff/src/rules/pydocstyle/rules/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,12 @@ fn blanks_and_section_underline(
}
}

fn common_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
fn common_section(
checker: &mut Checker,
docstring: &Docstring,
context: &SectionContext,
next_context: Option<&SectionContext>,
) {
if checker.settings.rules.enabled(Rule::CapitalizeSectionName) {
let capitalized_section_name = context.kind().as_str();
if context.section_name() != capitalized_section_name {
Expand Down Expand Up @@ -626,7 +631,7 @@ fn common_section(checker: &mut Checker, docstring: &Docstring, context: &Sectio
let line_end = checker.stylist.line_ending().as_str();
let last_line = context.following_lines().last();
if last_line.map_or(true, |line| !line.trim().is_empty()) {
if context.is_last() {
if next_context.is_none() {
if checker
.settings
.rules
Expand Down Expand Up @@ -660,9 +665,12 @@ fn common_section(checker: &mut Checker, docstring: &Docstring, context: &Sectio
docstring.range(),
);
if checker.patch(diagnostic.kind.rule()) {
// Add a newline after the section.
diagnostic
.set_fix(Edit::insertion(line_end.to_string(), context.range().end()));
// Add a newline at the beginning of the next section to deduplicate autofixes.
diagnostic.set_fix(Edit::insertion(
line_end.to_string(),
// Unwrap() is safe because next_context must be Some() to get here.
next_context.unwrap().range().start(),
));
}
checker.diagnostics.push(diagnostic);
}
Expand Down Expand Up @@ -861,8 +869,13 @@ fn parameters_section(checker: &mut Checker, docstring: &Docstring, context: &Se
missing_args(checker, docstring, &docstring_args);
}

fn numpy_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
common_section(checker, docstring, context);
fn numpy_section(
checker: &mut Checker,
docstring: &Docstring,
context: &SectionContext,
next_context: Option<&SectionContext>,
) {
common_section(checker, docstring, context, next_context);

if checker
.settings
Expand Down Expand Up @@ -897,8 +910,13 @@ fn numpy_section(checker: &mut Checker, docstring: &Docstring, context: &Section
}
}

fn google_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
common_section(checker, docstring, context);
fn google_section(
checker: &mut Checker,
docstring: &Docstring,
context: &SectionContext,
next_context: Option<&SectionContext>,
) {
common_section(checker, docstring, context, next_context);

if checker.settings.rules.enabled(Rule::SectionNameEndsInColon) {
let suffix = context.summary_after_section_name();
Expand Down Expand Up @@ -927,8 +945,12 @@ fn parse_numpy_sections(
docstring: &Docstring,
section_contexts: &SectionContexts,
) {
for section_context in section_contexts {
numpy_section(checker, docstring, &section_context);
for (curr, next) in section_contexts.iter().tuple_windows() {
numpy_section(checker, docstring, &curr, Some(&next));

if next.is_last() {
numpy_section(checker, docstring, &next, None);
}
}
}

Expand All @@ -937,8 +959,12 @@ fn parse_google_sections(
docstring: &Docstring,
section_contexts: &SectionContexts,
) {
for section_context in section_contexts {
google_section(checker, docstring, &section_context);
for (curr, next) in section_contexts.iter().tuple_windows() {
google_section(checker, docstring, &curr, Some(&next));

if next.is_last() {
google_section(checker, docstring, &next, None);
}
}

if checker.settings.rules.enabled(Rule::UndocumentedParam) {
Expand Down

0 comments on commit d296b26

Please sign in to comment.