mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +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
|
@ -24,7 +24,7 @@ pub struct DiagnosticKind {
|
|||
pub struct Diagnostic {
|
||||
pub kind: DiagnosticKind,
|
||||
pub range: TextRange,
|
||||
pub fix: Fix,
|
||||
pub fix: Option<Fix>,
|
||||
pub parent: Option<TextSize>,
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ impl Diagnostic {
|
|||
Self {
|
||||
kind: kind.into(),
|
||||
range,
|
||||
fix: Fix::empty(),
|
||||
fix: None,
|
||||
parent: None,
|
||||
}
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ impl Diagnostic {
|
|||
/// Set the [`Fix`] used to fix the diagnostic.
|
||||
#[inline]
|
||||
pub fn set_fix<T: Into<Fix>>(&mut self, fix: T) {
|
||||
self.fix = fix.into();
|
||||
self.fix = Some(fix.into());
|
||||
}
|
||||
|
||||
/// Consumes `self` and returns a new `Diagnostic` with the given `fix`.
|
||||
|
@ -57,7 +57,7 @@ impl Diagnostic {
|
|||
#[inline]
|
||||
pub fn try_set_fix<T: Into<Fix>>(&mut self, func: impl FnOnce() -> Result<T>) {
|
||||
match func() {
|
||||
Ok(fix) => self.fix = fix.into(),
|
||||
Ok(fix) => self.fix = Some(fix.into()),
|
||||
Err(err) => error!("Failed to create fix: {}", err),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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