mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
Perform insertions before replacements (#7739)
## Summary If we have, e.g.: ```python sum(( factor.dims for factor in bases ), []) ``` We generate three edits: two insertions (for the `operator` and `functools` imports), and then one replacement (for the `sum` call itself). We need to ensure that the insertions come before the replacement; otherwise, the edits will appear overlapping and out-of-order. Closes https://github.com/astral-sh/ruff/issues/7718.
This commit is contained in:
parent
e91ffe3e93
commit
1cf3b5676f
6 changed files with 30 additions and 11 deletions
|
@ -88,7 +88,7 @@ impl Fix {
|
|||
/// Create a new [`Fix`] with [automatic applicability](Applicability::Automatic) from multiple [`Edit`] elements.
|
||||
pub fn automatic_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
|
||||
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
|
||||
edits.sort_by_key(Ranged::start);
|
||||
edits.sort_by_key(|edit| (edit.start(), edit.end()));
|
||||
Self {
|
||||
edits,
|
||||
applicability: Applicability::Automatic,
|
||||
|
@ -108,7 +108,7 @@ impl Fix {
|
|||
/// Create a new [`Fix`] with [suggested applicability](Applicability::Suggested) from multiple [`Edit`] elements.
|
||||
pub fn suggested_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
|
||||
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
|
||||
edits.sort_by_key(Ranged::start);
|
||||
edits.sort_by_key(|edit| (edit.start(), edit.end()));
|
||||
Self {
|
||||
edits,
|
||||
applicability: Applicability::Suggested,
|
||||
|
@ -128,7 +128,7 @@ impl Fix {
|
|||
/// Create a new [`Fix`] with [manual applicability](Applicability::Manual) from multiple [`Edit`] elements.
|
||||
pub fn manual_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
|
||||
let mut edits: Vec<Edit> = std::iter::once(edit).chain(rest).collect();
|
||||
edits.sort_by_key(Ranged::start);
|
||||
edits.sort_by_key(|edit| (edit.start(), edit.end()));
|
||||
Self {
|
||||
edits,
|
||||
applicability: Applicability::Manual,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue