Skip to content

Commit

Permalink
Remove labels
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 29, 2023
1 parent 9445248 commit 8a554f4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
49 changes: 23 additions & 26 deletions crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn apply_fixes<'a>(
let mut fixed = FxHashMap::default();
let mut source_map = SourceMap::default();

'outer: for (rule, fix) in diagnostics
for (rule, fix) in diagnostics
.filter_map(|diagnostic| {
diagnostic
.fix
Expand All @@ -59,34 +59,30 @@ fn apply_fixes<'a>(
})
.sorted_by(|(rule1, fix1), (rule2, fix2)| cmp_fix(*rule1, *rule2, fix1, fix2))
{
let mut first = true;
'inner: for edit in fix.edits() {
// Skip any edits that were already applied.
if applied.contains(&edit) {
continue 'inner;
}

// Lazily enforce any isolation and positional requirements (e.g., avoid applying
// overlapping fixes, but avoid applying this requirement if all fixes in the edit were
// already applied).
if first {
// If this fix requires isolation, and we've already applied another fix in the
// same isolation group, skip it.
if let IsolationLevel::Group(id) = fix.isolation() {
if !isolated.insert(id) {
continue 'outer;
}
}

// Best-effort approach: if this fix overlaps with a fix we've already applied,
// skip it.
if last_pos.map_or(false, |last_pos| last_pos >= edit.start()) {
continue 'outer;
let mut edits = fix
.edits()
.iter()
.filter(|edit| !applied.contains(edit))
.peekable();

// If the fix contains at least one new edit, enforce isolation and positional requirements.
if let Some(first) = edits.peek() {
// If this fix requires isolation, and we've already applied another fix in the
// same isolation group, skip it.
if let IsolationLevel::Group(id) = fix.isolation() {
if !isolated.insert(id) {
continue;
}
}

first = false;
// If this fix overlaps with a fix we've already applied, skip it.
if last_pos.map_or(false, |last_pos| last_pos >= first.start()) {
continue;
}
}

let mut applied_edits = Vec::with_capacity(fix.edits().len());
for edit in edits {
// Add all contents from `last_pos` to `fix.location`.
let slice = locator.slice(TextRange::new(last_pos.unwrap_or_default(), edit.start()));
output.push_str(slice);
Expand All @@ -102,9 +98,10 @@ fn apply_fixes<'a>(

// Track that the edit was applied.
last_pos = Some(edit.end());
applied.insert(edit);
applied_edits.push(edit);
}

applied.extend(applied_edits.drain(..));
*fixed.entry(rule).or_default() += 1;
}

Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ pub(crate) fn lint_path(
.enumerate()
.zip(dest_notebook.cells().iter())
{
let (Cell::Code(src_code_cell), Cell::Code(dest_code_cell)) = (src_cell, dest_cell) else {
let (Cell::Code(src_code_cell), Cell::Code(dest_code_cell)) =
(src_cell, dest_cell)
else {
continue;
};
TextDiff::from_lines(
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_diagnostics/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub enum IsolationLevel {
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Fix {
/// The [`Edit`] elements to be applied, sorted by [`TextSize`].
/// The [`Edit`] elements to be applied, sorted by [`Edit::start`] in ascending order.
edits: Vec<Edit>,
/// The [`Applicability`] of the fix.
applicability: Applicability,
Expand Down Expand Up @@ -141,7 +141,7 @@ impl Fix {
self.edits.first().map(Edit::start)
}

/// Return a slice of the [`Edit`] elements in the [`Fix`], sorted by their [`TextSize`].
/// Return a slice of the [`Edit`] elements in the [`Fix`], sorted by [`Edit::start`] in ascending order.
pub fn edits(&self) -> &[Edit] {
&self.edits
}
Expand Down

0 comments on commit 8a554f4

Please sign in to comment.