strop-engine 0.29.0

strop editor engine: documents, grammar dispatch, services, sessions — no terminal
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Reviewable prepared changes (0049 §8): rename/code-action replies that
//! touch more than one document — or name refused targets — open a
//! real-buffer unified-diff review with explicit Apply/Cancel instead of
//! mutating immediately. Single-document, nothing-refused plans (the
//! `:format` case) keep applying directly with the existing receipt.
//!
//! A proposal is immutable once presented: identity, provenance, the
//! complete target inventory (planned documents with pinned base revisions
//! plus every refused target with its reason), and the rendered diff. Apply
//! re-checks each base revision and refuses moved targets BY NAME — it
//! never recomputes from newer text. After Apply/Cancel the review buffer
//! stays around as the receipt: outcomes per target, refusals with reasons.
//!
use strop_core::id::DocumentId;
use strop_core::Buffer;

use super::{ChangePlan, ChangeReceipt, PlannedDocument};
use crate::editor::transact::ChangeSet;
use crate::editor::Editor;
mod render;
mod save;
use render::ReviewBuffer;

#[derive(Debug, Clone, Copy)]
pub enum ReviewRow {
    Heading,
    File,
    Hunk,
    Context,
    Removed,
    Added,
    Warning,
}

/// The command names the review buffer advertises. Integration wires these
/// exact names to `review_apply_pub` / `review_cancel_pub`.
pub(crate) const APPLY_COMMAND: &str = ":apply-change";
pub(crate) const CANCEL_COMMAND: &str = ":cancel-change";

/// Unified-diff context lines around each changed span.
const CONTEXT: usize = 3;

/// Pending-proposal state. One proposal is reviewable at a time; a newer
/// proposal supersedes the older one's buffer with a visible note.
#[derive(Default)]
pub(crate) struct ReviewState {
    /// Monotone proposal identity; receipt headers name it.
    seq: usize,
    /// The proposal awaiting Apply/Cancel, if any.
    pending: Option<ChangeProposal>,
    /// A global replace assembling while its unopened targets load
    /// (0051 §6 R04); the review presents when the last target resolves.
    pub(crate) replace: Option<PendingReplace>,
    /// Monotone replace identity: a newer Enter supersedes, and opens
    /// issued for the older one merge nowhere.
    replace_seq: usize,
    pub(crate) replace_context: Option<ReplaceContext>,
    rows: std::collections::HashMap<DocumentId, Vec<ReviewRow>>,
    saves: std::collections::HashMap<DocumentId, save::PendingChangeSave>,
}

impl ReviewState {
    pub(crate) fn forget(&mut self, document: DocumentId) {
        self.rows.remove(&document);
        self.saves.retain(|_, pending| pending.report != document);
        if self
            .pending
            .as_ref()
            .is_some_and(|proposal| proposal.buffer == document)
        {
            self.pending = None;
            if self.replace.is_none() {
                self.replace_context = None;
            }
        }
    }
}

pub(crate) struct ReplaceContext {
    pub picker: strop_picker::Picker,
    pub origin: crate::editor::jumps::JumpRecord,
}

/// A global replace (Space R Enter) assembling its prepared review:
/// open buffers planned at Enter, unopened targets joining as their
/// owned open jobs land. Nothing here mutates a buffer.
#[derive(Debug)]
pub(crate) struct PendingReplace {
    /// Generation from `replace_seq`; late opens for a superseded
    /// replace are ignored.
    pub id: usize,
    /// Targets prepared so far, each with its pinned base revision.
    pub documents: Vec<PlannedDocument>,
    /// Every target that will not change, named with its reason:
    /// stale witnesses, read-only buffers, failed opens.
    pub refused: Vec<(strop_workspace::ResourceLocation, String)>,
    /// Owned opens still in flight; the review presents at zero.
    pub pending_opens: usize,
}

/// An immutable prepared proposal (0049 §8): identity, provenance, the
/// complete intended target inventory with pinned base revisions, and the
/// review buffer presenting it. Nothing here recomputes at apply time.
#[derive(Debug)]
pub(crate) struct ChangeProposal {
    /// Proposal number, stable across the review buffer and its receipt.
    pub id: usize,
    /// The prepared plan: `documents` carry their base revisions, `refused`
    /// names every target that will not change, with its reason.
    pub plan: ChangePlan,
    /// The real (read-only) buffer showing the diff review; it becomes the
    /// receipt after Apply/Cancel.
    pub buffer: DocumentId,
    pub view_revision: strop_core::id::BufferRevision,
}

impl Editor {
    /// Present a prepared plan: apply single-document, nothing-refused
    /// plans directly (the existing receipt path), otherwise open the
    /// diff-review buffer and hold the proposal for Apply/Cancel.
    pub(crate) fn present_change_plan(&mut self, plan: ChangePlan) {
        if plan.documents.len() <= 1 && plan.refused.is_empty() {
            self.apply_change_plan(plan);
            return;
        }
        self.review_change_plan(plan);
    }

    /// Apply the reviewed proposal. Each target's base revision is checked
    /// first: a source edited since the proposal (or closed) is refused by
    /// name — never recomputed from newer text. Applied targets go through
    /// the revision-checked gateway exactly as prepared. The review buffer
    /// becomes the receipt and stays open; the receipt is recorded for
    /// grouped undo (`:undo-change`).
    pub(crate) fn review_apply_pub(&mut self) {
        let Some(proposal) = self.review.pending.take() else {
            self.message = "no change proposal awaiting review".into();
            return;
        };
        if self
            .docs
            .get(proposal.buffer)
            .is_none_or(|doc| doc.buf.revision() != proposal.view_revision)
        {
            self.review.pending = Some(proposal);
            self.message = "review changed; prepare a new proposal before applying".into();
            return;
        }
        self.review.replace_context = None;
        let producer = proposal.plan.producer.label().to_string();
        let mut receipt = ChangeReceipt {
            producer: producer.clone(),
            applied: Vec::new(),
            applied_positions: Vec::new(),
            refused: proposal.plan.refused.clone(),
            redo_positions: None,
        };
        let mut lines: Vec<String> = proposal
            .plan
            .refused
            .iter()
            .map(|(location, reason)| format!("refused: {}{reason}", location.label()))
            .collect();
        for target in proposal.plan.documents {
            let label = target.location.label();
            let current = self.docs.get(target.document).map(|doc| doc.buf.revision());
            match current {
                None => {
                    let reason = "document closed since the proposal".to_string();
                    receipt.refused.push((target.location, reason.clone()));
                    lines.push(format!("refused: {label}{reason}"));
                }
                Some(revision) if revision != target.base => {
                    let reason = format!(
                        "edited since the proposal (base revision {}, now {revision}) — re-run the {producer}",
                        target.base
                    );
                    receipt.refused.push((target.location, reason.clone()));
                    lines.push(format!("refused: {label}{reason}"));
                }
                Some(_) => {
                    let changes = ChangeSet {
                        edits: target.edits,
                        undo_open: false,
                    };
                    match self.apply(target.document, target.base, changes) {
                        Ok(committed) => {
                            receipt.applied.push((
                                target.document,
                                target.base,
                                committed.revision,
                            ));
                            if let Some(position) = self
                                .docs
                                .get(target.document)
                                .and_then(|doc| doc.buf.history().committed_position())
                            {
                                receipt.applied_positions.push(position);
                            }
                            lines.push(format!(
                                "applied: {label} (revision {} -> {})",
                                target.base, committed.revision
                            ));
                        }
                        Err(error) => {
                            let reason = format!("changed since plan: {error}");
                            receipt.refused.push((target.location, reason.clone()));
                            lines.push(format!("refused: {label}{reason}"));
                        }
                    }
                }
            }
        }
        let status = if receipt.refused.is_empty() {
            "APPLIED"
        } else if receipt.applied.is_empty() {
            "REFUSED"
        } else {
            "PARTIAL"
        };
        let mut text = format!(
            "strop change proposal {}: {producer}{status}\n{} buffer(s) applied, {} target(s) refused\n:undo-change reverts the applied group; :save-change saves changed files\n\n",
            proposal.id,
            receipt.applied.len(),
            receipt.refused.len()
        );
        for line in &lines {
            text.push_str(line);
            text.push('\n');
        }
        let publish = self.replace_system(proposal.buffer, &text);
        self.review.rows.insert(
            proposal.buffer,
            text.lines()
                .enumerate()
                .map(|(line, _)| {
                    if line == 0 && !receipt.refused.is_empty() {
                        ReviewRow::Warning
                    } else if line == 0 {
                        ReviewRow::Heading
                    } else {
                        ReviewRow::Context
                    }
                })
                .collect(),
        );
        if self.current() == proposal.buffer {
            self.set_head(0);
            self.view_mut().view_top = 0;
        }
        self.message = match (receipt.applied.len(), receipt.refused.len()) {
            (applied, 0) => format!("{producer}: applied to {applied} buffer(s)"),
            (applied, refused) => {
                format!("{producer}: {applied} buffer(s) applied, {refused} target(s) refused")
            }
        };
        if let Err(error) = publish {
            self.message = format!("change receipt publish failed: {error}");
        }
        self.changes.record(receipt);
    }

    /// Cancel the reviewed proposal: nothing is applied, ever. The review
    /// buffer becomes a cancelled receipt and stays open.
    pub(crate) fn review_cancel_pub(&mut self) {
        if self.review.replace.take().is_some() {
            self.restore_replace_context();
            self.message = "replace cancelled while loading; nothing applied".into();
            return;
        }
        let Some(proposal) = self.review.pending.take() else {
            self.message = "no change proposal awaiting review".into();
            return;
        };
        let producer = proposal.plan.producer.label();
        let mut text = format!(
            "strop change proposal {}: {producer} — CANCELLED\nnothing applied; {} file(s) had been proposed\n",
            proposal.id,
            proposal.plan.documents.len()
        );
        if !proposal.plan.refused.is_empty() {
            text.push_str("\nrefused targets (would have been skipped):\n");
            for (location, reason) in &proposal.plan.refused {
                text.push_str(&format!("  {}{reason}\n", location.label()));
            }
        }
        let publish = self.replace_system(proposal.buffer, &text);
        self.review.rows.insert(
            proposal.buffer,
            text.lines()
                .enumerate()
                .map(|(line, _)| {
                    if line == 0 {
                        ReviewRow::Heading
                    } else {
                        ReviewRow::Context
                    }
                })
                .collect(),
        );
        if self.current() == proposal.buffer {
            self.set_head(0);
            self.view_mut().view_top = 0;
        }
        self.message = format!(
            "{producer}: proposal {} cancelled — nothing applied",
            proposal.id
        );
        if let Err(error) = publish {
            self.message = format!("change receipt publish failed: {error}");
        }
        self.restore_replace_context();
    }

    /// Render the review buffer: header, per-file unified diffs computed
    /// from each target's pinned base, and every refused target named with
    /// its reason.
    fn render_proposal(&self, id: usize, plan: &ChangePlan) -> ReviewBuffer {
        let producer = plan.producer.label();
        let mut view = ReviewBuffer::default();
        view.line(
            &format!("strop change proposal {id}: {producer}"),
            ReviewRow::Heading,
        );
        view.line(
            &format!(
                "{} file(s) to change, {} target(s) refused",
                plan.documents.len(),
                plan.refused.len()
            ),
            ReviewRow::Context,
        );
        view.line(
            &format!("{APPLY_COMMAND} applies exactly what is shown; {CANCEL_COMMAND} discards it"),
            ReviewRow::Context,
        );
        view.line(
            "bases are pinned — editing a source invalidates that file at apply",
            ReviewRow::Context,
        );
        for target in &plan.documents {
            view.line("", ReviewRow::Context);
            let label = match target.location.filesystem {
                strop_workspace::Filesystem::Local => target
                    .location
                    .path
                    .strip_prefix(&self.cwd)
                    .unwrap_or(&target.location.path)
                    .display()
                    .to_string(),
                _ => target.location.label(),
            };
            match self.docs.get(target.document) {
                Some(document) => {
                    match render::file_diff(&label, &document.buf, target.base, &target.edits) {
                        Ok(diff) => view.append(diff),
                        Err(error) => {
                            view.line(&format!("refused: {label}{error}"), ReviewRow::Warning)
                        }
                    }
                }
                None => view.line(
                    &format!("refused: {label} — document closed"),
                    ReviewRow::Warning,
                ),
            }
        }
        if !plan.refused.is_empty() {
            view.line("", ReviewRow::Context);
            view.line("refused targets:", ReviewRow::Warning);
            for (location, reason) in &plan.refused {
                view.line(
                    &format!("  {}{reason}", location.label()),
                    ReviewRow::Warning,
                );
            }
        }
        view
    }
}
impl Editor {
    /// Begin a replace review assembly (0051 §6 R04); returns the
    /// generation the owned opens carry. A newer call supersedes the
    /// old assembly — its late opens merge nowhere.
    pub(crate) fn begin_replace(
        &mut self,
        documents: Vec<PlannedDocument>,
        refused: Vec<(strop_workspace::ResourceLocation, String)>,
        pending_opens: usize,
    ) -> usize {
        self.review.replace_seq += 1;
        let id = self.review.replace_seq;
        self.review.replace = Some(PendingReplace {
            id,
            documents,
            refused,
            pending_opens,
        });
        id
    }

    /// Present a prepared plan as a review ALWAYS (0051 §6 R04): a
    /// global replace never applies straight from the picker's text
    /// fields, even when the plan is a single file. Same review shape
    /// as `present_change_plan` — kept separate so that method's
    /// direct-apply fast path stays untouched for LSP producers.
    pub(crate) fn review_change_plan(&mut self, plan: ChangePlan) {
        if let Some(old) = self.review.pending.take() {
            let note = format!(
                "strop change proposal {}: {} — SUPERSEDED by a newer proposal\n",
                old.id,
                old.plan.producer.label()
            );
            if let Err(error) = self.replace_system(old.buffer, &note) {
                self.message = format!("could not retire proposal {}: {error}", old.id);
            }
            self.review
                .rows
                .insert(old.buffer, vec![ReviewRow::Heading]);
        }
        self.review.seq += 1;
        let id = self.review.seq;
        let text = self.render_proposal(id, &plan);
        let producer = plan.producer.label().to_string();
        let files = plan.documents.len();
        let refused = plan.refused.len();
        let mut buf = Buffer::from_text(&text.text);
        buf.name = Some(format!("change proposal {id}"));
        let view_revision = buf.revision();
        let buffer = self.open_temporary_output(buf);
        self.review.rows.insert(buffer, text.rows);
        self.review.pending = Some(ChangeProposal {
            id,
            plan,
            buffer,
            view_revision,
        });
        self.message = match refused {
            0 => format!(
                "{producer}: proposal {id} reviews {files} file(s) — {APPLY_COMMAND} or {CANCEL_COMMAND}"
            ),
            _ => format!(
                "{producer}: proposal {id} reviews {files} file(s), {refused} refused — {APPLY_COMMAND} or {CANCEL_COMMAND}"
            ),
        };
    }
}

#[cfg(test)]
mod tests;

impl Editor {
    pub fn review_row(&self, document: DocumentId, row: usize) -> Option<ReviewRow> {
        self.review.rows.get(&document)?.get(row).copied()
    }
}