1use vvv_core::RelPath;
5
6use serde::{Deserialize, Serialize};
7use vvv_core::{Address, Edit, Query};
8
9use super::diff::UnifiedDiff;
10use super::{
11 BatchIntent, Intent, Match, MoveIntent, MoveSymbolIntent, Notice, Occurrence, RenameIntent,
12 Respelling, RewriteIntent, Skipped,
13};
14use crate::plan::{FilePreview, Plan};
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct Search {
19 pub query: Query,
20 pub matches: Vec<Match>,
21 #[serde(default, skip_serializing_if = "Vec::is_empty")]
24 pub skipped: Vec<Skipped>,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct Rewrite {
30 pub intent: RewriteIntent,
31 pub applied: bool,
33 #[serde(default, skip_serializing_if = "Option::is_none")]
35 pub history_id: Option<u64>,
36 pub files: Vec<FileChange>,
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct Rename {
42 pub intent: RenameIntent,
43 pub applied: bool,
44 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub history_id: Option<u64>,
47 pub declarations: Vec<Match>,
49 pub occurrences: Vec<Occurrence>,
52 pub files: Vec<FileChange>,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct Move {
58 pub intent: MoveIntent,
59 pub applied: bool,
60 #[serde(default, skip_serializing_if = "Option::is_none")]
62 pub history_id: Option<u64>,
63 pub from: RelPath,
65 pub to: RelPath,
66 #[serde(default, skip_serializing_if = "Option::is_none")]
68 pub from_address: Option<Address>,
69 #[serde(default, skip_serializing_if = "Option::is_none")]
70 pub to_address: Option<Address>,
71 #[serde(default, skip_serializing_if = "Vec::is_empty")]
73 pub notices: Vec<Notice>,
74 #[serde(default, skip_serializing_if = "Vec::is_empty")]
77 pub respellings: Vec<Respelling>,
78 pub files: Vec<FileChange>,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct MoveSymbol {
84 pub intent: MoveSymbolIntent,
85 pub applied: bool,
86 #[serde(default, skip_serializing_if = "Option::is_none")]
88 pub history_id: Option<u64>,
89 pub from: Address,
91 pub to: Address,
92 #[serde(default, skip_serializing_if = "Vec::is_empty")]
93 pub notices: Vec<Notice>,
94 #[serde(default, skip_serializing_if = "Vec::is_empty")]
97 pub respellings: Vec<Respelling>,
98 pub files: Vec<FileChange>,
99}
100
101#[derive(Debug, Clone, Serialize, Deserialize)]
103pub struct Batch {
104 pub intents: Vec<Intent>,
105 pub applied: bool,
107 #[serde(default, skip_serializing_if = "Option::is_none")]
109 pub history_id: Option<u64>,
110 #[serde(default, skip_serializing_if = "Vec::is_empty")]
111 pub notices: Vec<Notice>,
112 pub files: Vec<FileChange>,
116}
117
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
120pub struct HistoryEntry {
121 pub id: u64,
122 pub at: u64,
124 pub intent: Intent,
126 pub files: usize,
128 #[serde(default, skip_serializing_if = "Vec::is_empty")]
130 pub paths: Vec<RelPath>,
131 #[serde(default, skip_serializing_if = "Vec::is_empty")]
133 pub moves: Vec<(RelPath, RelPath)>,
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
138pub struct History {
139 pub entries: Vec<HistoryEntry>,
141}
142
143#[derive(Debug, Clone, Serialize, Deserialize)]
145pub struct Undo {
146 pub undone: HistoryEntry,
147 pub restored: Vec<RelPath>,
149 #[serde(default, skip_serializing_if = "Vec::is_empty")]
151 pub moves_reverted: Vec<(RelPath, RelPath)>,
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize)]
156pub struct FileChange {
157 pub path: RelPath,
159 #[serde(default, skip_serializing_if = "Option::is_none")]
161 pub moved_to: Option<RelPath>,
162 pub edits: Vec<Edit>,
163 pub diff: UnifiedDiff,
164}
165
166impl FileChange {
167 pub(crate) fn all(plan: Option<&Plan>, preview: &[FilePreview]) -> Vec<Self> {
170 preview
171 .iter()
172 .map(|file| FileChange {
173 path: file.path.clone(),
174 moved_to: file.moved_to.clone(),
175 edits: plan
176 .map_or_else(Vec::new, |p| p.change_set().edits_for(&file.path).to_vec()),
177 diff: UnifiedDiff::between(
178 &file.path,
179 file.moved_to.as_deref().unwrap_or(&file.path),
180 &file.before,
181 &file.after,
182 ),
183 })
184 .collect()
185 }
186}
187
188pub trait Mutation {
191 fn intent(&self) -> Intent;
193 fn applied(&mut self, id: u64);
195}
196
197macro_rules! mutation {
198 ($t:ty, $variant:ident) => {
199 impl Mutation for $t {
200 fn intent(&self) -> Intent {
201 Intent::$variant(self.intent.clone())
202 }
203 fn applied(&mut self, id: u64) {
204 self.applied = true;
205 self.history_id = Some(id);
206 }
207 }
208 };
209}
210
211mutation!(Rewrite, Rewrite);
212mutation!(Rename, Rename);
213mutation!(Move, Move);
214mutation!(MoveSymbol, MoveSymbol);
215
216impl Mutation for Batch {
217 fn intent(&self) -> Intent {
218 Intent::Batch(BatchIntent {
219 intents: self.intents.clone(),
220 })
221 }
222 fn applied(&mut self, id: u64) {
223 self.applied = true;
224 self.history_id = Some(id);
225 }
226}