mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Refactor Fix
and Edit
API (#4198)
This commit is contained in:
parent
edaf891042
commit
0801f14046
102 changed files with 529 additions and 398 deletions
|
@ -5,26 +5,23 @@ use serde::{Deserialize, Serialize};
|
|||
use crate::edit::Edit;
|
||||
|
||||
/// A collection of [`Edit`] elements to be applied to a source file.
|
||||
#[derive(Default, Debug, PartialEq, Eq, Clone)]
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct Fix {
|
||||
edits: Vec<Edit>,
|
||||
}
|
||||
|
||||
impl Fix {
|
||||
/// Create a new [`Fix`] from a vector of [`Edit`] elements.
|
||||
pub fn new(edits: Vec<Edit>) -> Self {
|
||||
Self { edits }
|
||||
/// Create a new [`Fix`] with an unspecified applicability from an [`Edit`] element.
|
||||
pub fn unspecified(edit: Edit) -> Self {
|
||||
Self { edits: vec![edit] }
|
||||
}
|
||||
|
||||
/// Create an empty [`Fix`].
|
||||
pub const fn empty() -> Self {
|
||||
Self { edits: Vec::new() }
|
||||
}
|
||||
|
||||
/// Return `true` if the [`Fix`] contains no [`Edit`] elements.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.edits.is_empty()
|
||||
/// Create a new [`Fix`] with unspecified applicability from multiple [`Edit`] elements.
|
||||
pub fn unspecified_edits(edit: Edit, rest: impl IntoIterator<Item = Edit>) -> Self {
|
||||
Self {
|
||||
edits: std::iter::once(edit).chain(rest.into_iter()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the [`TextSize`] of the first [`Edit`] in the [`Fix`].
|
||||
|
@ -42,14 +39,6 @@ impl Fix {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromIterator<Edit> for Fix {
|
||||
fn from_iter<T: IntoIterator<Item = Edit>>(iter: T) -> Self {
|
||||
Self {
|
||||
edits: Vec::from_iter(iter),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Edit> for Fix {
|
||||
fn from(edit: Edit) -> Self {
|
||||
Self { edits: vec![edit] }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue