Skip to main content

vvv_engine/
batch.rs

1//! `batch`: several intents as one plan. Each is planned against the tree
2//! as the previous one leaves it (an overlay the real files never see), and
3//! all are applied in order as one transaction with one receipt — one
4//! preview, one apply, one undo.
5
6use crate::command::{Command, Context};
7use crate::{Batch, BatchIntent, EngineError, FileChange, FilePreview, Planned, Receipt, VfsError};
8
9/// Plan several intents as one. Each is planned against a staging copy of
10/// the workspace onto which the previous steps have been applied, so a
11/// rename may follow a move of the file it touches. Nothing real is
12/// written; see [`Apply`](crate::Apply).
13impl Command for BatchIntent {
14    type Output = Planned<Batch>;
15
16    fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
17        let (engine, staging) = cx.staged();
18        let mut steps = Vec::new();
19        let mut notices = Vec::new();
20        let mut receipt: Option<Receipt> = None;
21        for intent in &self.intents {
22            let planned = engine.run(intent.clone())?;
23            notices.extend_from_slice(planned.notices());
24            for plan in planned.into_plans() {
25                let applied = plan.clone().apply(&staging)?;
26                receipt = Some(match receipt.take() {
27                    Some(so_far) => so_far.then(applied),
28                    None => applied,
29                });
30                steps.push(plan);
31            }
32        }
33        // The combined view: each touched file as it is now against how the
34        // staging tree leaves it, at its final path.
35        let preview = match &receipt {
36            Some(receipt) => receipt
37                .paths()
38                .map(|path| {
39                    let mut final_path = path.to_path_buf();
40                    for (from, to) in receipt.moves() {
41                        if final_path == *from {
42                            final_path = to.clone();
43                        }
44                    }
45                    let before = cx.workspace.vfs().read(&cx.workspace.absolute(path))?;
46                    let after = staging.vfs().read(&staging.absolute(&final_path))?;
47                    Ok(FilePreview {
48                        path: path.into(),
49                        moved_to: (final_path != path).then_some(final_path.into()),
50                        before,
51                        after,
52                    })
53                })
54                .collect::<Result<_, VfsError>>()?,
55            None => Vec::new(),
56        };
57        let files = FileChange::all(None, &preview);
58        Ok(Planned::new(
59            Batch {
60                intents: self.intents,
61                applied: false,
62                history_id: None,
63                notices,
64                files,
65            },
66            steps,
67            preview,
68        ))
69    }
70}