Skip to content

Commit

Permalink
Accept a Locator for ast::whitespace::indentation
Browse files Browse the repository at this point in the history
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk and charliermarsh committed Jan 18, 2023
1 parent 6c7e60b commit 715ea2d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/ast/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use std::str::Lines;
use rustpython_ast::{Located, Location};

use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::source_code::Locator;

/// Extract the leading indentation from a line.
pub fn indentation<'a, T>(checker: &'a Checker, located: &'a Located<T>) -> Cow<'a, str> {
pub fn indentation<'a, T>(locator: &'a Locator, located: &'a Located<T>) -> Cow<'a, str> {
let range = Range::from_located(located);
checker.locator.slice_source_code_range(&Range::new(
locator.slice_source_code_range(&Range::new(
Location::new(range.location.row(), 0),
Location::new(range.location.row(), range.location.column()),
))
Expand Down
2 changes: 1 addition & 1 deletion src/rules/flake8_return/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn implicit_return(checker: &mut Checker, last_stmt: &Stmt) {
Diagnostic::new(violations::ImplicitReturn, Range::from_located(last_stmt));
if checker.patch(&RuleCode::RET503) {
let mut content = String::new();
content.push_str(&indentation(checker, last_stmt));
content.push_str(&indentation(&checker.locator, last_stmt));
content.push_str("return None");
content.push('\n');
diagnostic.amend(Fix::insertion(
Expand Down
2 changes: 1 addition & 1 deletion src/rules/pyupgrade/rules/replace_stdout_stderr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn replace_stdout_stderr(checker: &mut Checker, expr: &Expr, kwargs: &[Keywo
if middle.multi_line {
contents.push(',');
contents.push('\n');
contents.push_str(&indentation(checker, first));
contents.push_str(&indentation(&checker.locator, first));
} else {
contents.push(',');
contents.push(' ');
Expand Down
4 changes: 2 additions & 2 deletions src/rules/pyupgrade/rules/rewrite_mock_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub fn rewrite_mock_import(checker: &mut Checker, stmt: &Stmt) {
{
// Generate the fix, if needed, which is shared between all `mock` imports.
let content = if checker.patch(&RuleCode::UP026) {
let indent = indentation(checker, stmt);
let indent = indentation(&checker.locator, stmt);
match format_import(stmt, &indent, checker.locator, checker.stylist) {
Ok(content) => Some(content),
Err(e) => {
Expand Down Expand Up @@ -273,7 +273,7 @@ pub fn rewrite_mock_import(checker: &mut Checker, stmt: &Stmt) {
Range::from_located(stmt),
);
if checker.patch(&RuleCode::UP026) {
let indent = indentation(checker, stmt);
let indent = indentation(&checker.locator, stmt);
match format_import_from(stmt, &indent, checker.locator, checker.stylist) {
Ok(content) => {
diagnostic.amend(Fix::replacement(
Expand Down

0 comments on commit 715ea2d

Please sign in to comment.