use vvv_core::RelPath;
use serde::{Deserialize, Serialize};
use vvv_core::{Address, Edit, Query};
use super::diff::UnifiedDiff;
use super::{
BatchIntent, Intent, Match, MoveIntent, MoveSymbolIntent, Notice, Occurrence, RenameIntent,
Respelling, RewriteIntent, Skipped,
};
use crate::plan::{FilePreview, Plan};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Search {
pub query: Query,
pub matches: Vec<Match>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub skipped: Vec<Skipped>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rewrite {
pub intent: RewriteIntent,
pub applied: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub history_id: Option<u64>,
pub files: Vec<FileChange>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rename {
pub intent: RenameIntent,
pub applied: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub history_id: Option<u64>,
pub declarations: Vec<Match>,
pub occurrences: Vec<Occurrence>,
pub files: Vec<FileChange>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Move {
pub intent: MoveIntent,
pub applied: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub history_id: Option<u64>,
pub from: RelPath,
pub to: RelPath,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from_address: Option<Address>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub to_address: Option<Address>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub notices: Vec<Notice>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub respellings: Vec<Respelling>,
pub files: Vec<FileChange>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MoveSymbol {
pub intent: MoveSymbolIntent,
pub applied: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub history_id: Option<u64>,
pub from: Address,
pub to: Address,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub notices: Vec<Notice>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub respellings: Vec<Respelling>,
pub files: Vec<FileChange>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Batch {
pub intents: Vec<Intent>,
pub applied: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub history_id: Option<u64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub notices: Vec<Notice>,
pub files: Vec<FileChange>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HistoryEntry {
pub id: u64,
pub at: u64,
pub intent: Intent,
pub files: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub paths: Vec<RelPath>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub moves: Vec<(RelPath, RelPath)>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct History {
pub entries: Vec<HistoryEntry>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Undo {
pub undone: HistoryEntry,
pub restored: Vec<RelPath>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub moves_reverted: Vec<(RelPath, RelPath)>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileChange {
pub path: RelPath,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub moved_to: Option<RelPath>,
pub edits: Vec<Edit>,
pub diff: UnifiedDiff,
}
impl FileChange {
pub(crate) fn all(plan: Option<&Plan>, preview: &[FilePreview]) -> Vec<Self> {
preview
.iter()
.map(|file| FileChange {
path: file.path.clone(),
moved_to: file.moved_to.clone(),
edits: plan
.map_or_else(Vec::new, |p| p.change_set().edits_for(&file.path).to_vec()),
diff: UnifiedDiff::between(
&file.path,
file.moved_to.as_deref().unwrap_or(&file.path),
&file.before,
&file.after,
),
})
.collect()
}
}
pub trait Mutation {
fn intent(&self) -> Intent;
fn applied(&mut self, id: u64);
}
macro_rules! mutation {
($t:ty, $variant:ident) => {
impl Mutation for $t {
fn intent(&self) -> Intent {
Intent::$variant(self.intent.clone())
}
fn applied(&mut self, id: u64) {
self.applied = true;
self.history_id = Some(id);
}
}
};
}
mutation!(Rewrite, Rewrite);
mutation!(Rename, Rename);
mutation!(Move, Move);
mutation!(MoveSymbol, MoveSymbol);
impl Mutation for Batch {
fn intent(&self) -> Intent {
Intent::Batch(BatchIntent {
intents: self.intents.clone(),
})
}
fn applied(&mut self, id: u64) {
self.applied = true;
self.history_id = Some(id);
}
}