Skip to content

Commit

Permalink
Add methods on Edit that accept TextRange
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Apr 14, 2023
1 parent 72f4c11 commit af24c5b
Show file tree
Hide file tree
Showing 83 changed files with 244 additions and 475 deletions.
27 changes: 8 additions & 19 deletions crates/ruff/src/autofix/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,23 @@ pub fn delete_stmt(
{
// If removing this node would lead to an invalid syntax tree, replace
// it with a `pass`.
Ok(Edit::replacement(
"pass".to_string(),
stmt.start(),
stmt.end(),
))
Ok(Edit::range_replacement("pass".to_string(), stmt.range()))
} else {
Ok(if let Some(semicolon) = trailing_semicolon(stmt, locator) {
let next = next_stmt_break(semicolon, locator);
Edit::deletion(stmt.start(), next)
} else if helpers::has_leading_content(stmt, locator) {
Edit::deletion(stmt.start(), stmt.end())
Edit::range_deletion(stmt.range())
} else if helpers::preceded_by_continuation(stmt, indexer, locator) {
if is_end_of_file(stmt, locator) && locator.is_at_start_of_line(stmt.start()) {
// Special-case: a file can't end in a continuation.
Edit::replacement(stylist.line_ending().to_string(), stmt.start(), stmt.end())
Edit::range_replacement(stylist.line_ending().to_string(), stmt.range())
} else {
Edit::deletion(stmt.start(), stmt.end())
Edit::range_deletion(stmt.range())
}
} else {
let range = locator.full_lines_range(stmt.range());
Edit::deletion(range.start(), range.end())
Edit::range_deletion(range)
})
}
}
Expand Down Expand Up @@ -328,11 +324,7 @@ pub fn remove_unused_imports<'a>(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
stmt.start(),
stmt.end(),
))
Ok(Edit::range_replacement(state.to_string(), stmt.range()))
}
}

Expand Down Expand Up @@ -473,11 +465,8 @@ pub fn get_or_import_symbol(
//
// By adding this no-op edit, we force the `unused-imports` fix to conflict with the
// `sys-exit-alias` fix, and thus will avoid applying both fixes in the same pass.
let import_edit = Edit::replacement(
locator.slice(source.range()).to_string(),
source.start(),
source.end(),
);
let import_edit =
Edit::range_replacement(locator.slice(source.range()).to_string(), source.range());
Ok((import_edit, binding))
} else {
if let Some(stmt) = importer.get_import_from(module) {
Expand Down
5 changes: 2 additions & 3 deletions crates/ruff/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ pub fn check_noqa(
locator,
));
} else {
diagnostic.set_fix(Edit::replacement(
diagnostic.set_fix(Edit::range_replacement(
format!("# noqa: {}", valid_codes.join(", ")),
range.start(),
range.end(),
*range,
));
}
}
Expand Down
6 changes: 1 addition & 5 deletions crates/ruff/src/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ impl<'a> Importer<'a> {
..CodegenState::default()
};
tree.codegen(&mut state);
Ok(Edit::replacement(
state.to_string(),
stmt.start(),
stmt.end(),
))
Ok(Edit::range_replacement(state.to_string(), stmt.range()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/eradicate/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn commented_out_code(
let mut diagnostic = Diagnostic::new(CommentedOutCode, TextRange::new(start, end));
if autofix.into() && settings.rules.should_fix(Rule::CommentedOutCode) {
let range = locator.full_lines_range(TextRange::new(start, end));
diagnostic.set_fix(Edit::deletion(range.start(), range.end()));
diagnostic.set_fix(Edit::range_deletion(range));
}
Some(diagnostic)
} else {
Expand Down
5 changes: 2 additions & 3 deletions crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option

let mut diagnostic = Diagnostic::new(AssertFalse, test.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Edit::replacement(
diagnostic.set_fix(Edit::range_replacement(
unparse_stmt(&assertion_error(msg), checker.stylist),
stmt.start(),
stmt.end(),
stmt.range(),
));
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ fn duplicate_handler_exceptions<'a>(
expr.range(),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Edit::replacement(
diagnostic.set_fix(Edit::range_replacement(
if unique_elts.len() == 1 {
unparse_expr(unique_elts[0], checker.stylist)
} else {
unparse_expr(&type_pattern(unique_elts), checker.stylist)
},
expr.start(),
expr.end(),
expr.range(),
));
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar
let mut diagnostic = Diagnostic::new(GetAttrWithConstant, expr.range());

if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Edit::replacement(
diagnostic.set_fix(Edit::range_replacement(
unparse_expr(&attribute(obj, value), checker.stylist),
expr.start(),
expr.end(),
expr.range(),
));
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ pub fn redundant_tuple_in_exception_handler(checker: &mut Checker, handlers: &[E
type_.range(),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Edit::replacement(
diagnostic.set_fix(Edit::range_replacement(
unparse_expr(elt, checker.stylist),
type_.start(),
type_.end(),
type_.range(),
));
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ pub fn setattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar
let mut diagnostic = Diagnostic::new(SetAttrWithConstant, expr.range());

if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Edit::replacement(
diagnostic.set_fix(Edit::range_replacement(
assignment(obj, name, value, checker.stylist),
expr.start(),
expr.end(),
expr.range(),
));
}
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub fn unused_loop_control_variable(
if let Some(binding) = binding {
if binding.kind.is_loop_var() {
if !binding.used() {
diagnostic.set_fix(Edit::replacement(rename, expr.start(), expr.end()));
diagnostic.set_fix(Edit::range_replacement(rename, expr.range()));
}
}
}
Expand Down
16 changes: 5 additions & 11 deletions crates/ruff/src/rules/flake8_commas/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub fn trailing_commas(
let mut diagnostic =
Diagnostic::new(ProhibitedTrailingComma, TextRange::new(comma.0, comma.2));
if autofix.into() && settings.rules.should_fix(Rule::ProhibitedTrailingComma) {
diagnostic.set_fix(Edit::deletion(comma.0, comma.2));
diagnostic.set_fix(Edit::range_deletion(diagnostic.range()));
}
diagnostics.push(diagnostic);
}
Expand Down Expand Up @@ -289,21 +289,15 @@ pub fn trailing_commas(
);
if comma_required {
let missing_comma = prev_prev.spanned.unwrap();
let mut diagnostic = Diagnostic::new(
MissingTrailingComma,
TextRange::new(missing_comma.2, missing_comma.2),
);
let range = TextRange::new(missing_comma.2, missing_comma.2);
let mut diagnostic = Diagnostic::new(MissingTrailingComma, range);
if autofix.into() && settings.rules.should_fix(Rule::MissingTrailingComma) {
// Create a replacement that includes the final bracket (or other token),
// rather than just inserting a comma at the end. This prevents the UP034 autofix
// removing any brackets in the same linter pass - doing both at the same time could
// lead to a syntax error.
let contents = locator.slice(TextRange::new(missing_comma.0, missing_comma.2));
diagnostic.set_fix(Edit::replacement(
format!("{contents},"),
missing_comma.0,
missing_comma.2,
));
let contents = locator.slice(range);
diagnostic.set_fix(Edit::range_replacement(format!("{contents},"), range));
}
diagnostics.push(diagnostic);
}
Expand Down
72 changes: 12 additions & 60 deletions crates/ruff/src/rules/flake8_comprehensions/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ pub fn fix_unnecessary_generator_list(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C401) Convert `set(x for x in y)` to `{x for x in y}`.
Expand Down Expand Up @@ -235,11 +231,7 @@ pub fn fix_unnecessary_list_comprehension_set(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C404) Convert `dict([(i, i) for i in range(3)])` to `{i: i for i in
Expand Down Expand Up @@ -291,11 +283,7 @@ pub fn fix_unnecessary_list_comprehension_dict(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// Drop a trailing comma from a list of tuple elements.
Expand Down Expand Up @@ -385,11 +373,7 @@ pub fn fix_unnecessary_literal_set(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C406) Convert `dict([(1, 2)])` to `{1: 2}`.
Expand Down Expand Up @@ -458,11 +442,7 @@ pub fn fix_unnecessary_literal_dict(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C408)
Expand Down Expand Up @@ -574,11 +554,7 @@ pub fn fix_unnecessary_collection_call(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C409) Convert `tuple([1, 2])` to `tuple(1, 2)`
Expand Down Expand Up @@ -633,11 +609,7 @@ pub fn fix_unnecessary_literal_within_tuple_call(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C410) Convert `list([1, 2])` to `[1, 2]`
Expand Down Expand Up @@ -694,11 +666,7 @@ pub fn fix_unnecessary_literal_within_list_call(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C411) Convert `list([i * i for i in x])` to `[i * i for i in x]`.
Expand All @@ -723,11 +691,7 @@ pub fn fix_unnecessary_list_call(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C413) Convert `list(sorted([2, 3, 1]))` to `sorted([2, 3, 1])`.
Expand Down Expand Up @@ -852,11 +816,7 @@ pub fn fix_unnecessary_call_around_sorted(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C414) Convert `sorted(list(foo))` to `sorted(foo)`
Expand Down Expand Up @@ -893,11 +853,7 @@ pub fn fix_unnecessary_double_cast_or_process(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C416) Convert `[i for i in x]` to `list(x)`.
Expand Down Expand Up @@ -989,11 +945,7 @@ pub fn fix_unnecessary_comprehension(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}

/// (C417) Convert `map(lambda x: x * 2, bar)` to `(x * 2 for x in bar)`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ pub fn shebang_whitespace(
TextRange::at(range.start(), *n_spaces),
);
if autofix {
diagnostic.set_fix(Edit::deletion(range.start(), range.start() + n_spaces));
diagnostic.set_fix(Edit::range_deletion(TextRange::at(
range.start(),
*n_spaces,
)));
}
Some(diagnostic)
} else {
Expand Down
5 changes: 2 additions & 3 deletions crates/ruff/src/rules/flake8_logging_format/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,9 @@ pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords:
{
let mut diagnostic = Diagnostic::new(LoggingWarn, level_call_range);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Edit::replacement(
diagnostic.set_fix(Edit::range_replacement(
"warning".to_string(),
level_call_range.start(),
level_call_range.end(),
level_call_range,
));
}
checker.diagnostics.push(diagnostic);
Expand Down
6 changes: 1 addition & 5 deletions crates/ruff/src/rules/flake8_pie/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,5 @@ pub fn fix_unnecessary_comprehension_any_all(
};
tree.codegen(&mut state);

Ok(Edit::replacement(
state.to_string(),
expr.start(),
expr.end(),
))
Ok(Edit::range_replacement(state.to_string(), expr.range()))
}
Loading

0 comments on commit af24c5b

Please sign in to comment.