strop-editor 0.3.3

strop — a modal text editor in Rust: see the cut before you make it
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Picker glue: the editor side of strop-picker. Workers post onto the
//! event loop (0001 §5.6); the editor drains them between keystrokes.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::mpsc::{channel, Receiver, Sender};

use strop_picker::{spawn_files, GrepWorker, Item, Kind, Payload, Picker, PickerMsg};
use strop_syntax::Highlighter;

use super::{Editor, Key};

impl PickerGlue {
    /// A picker over editor-computed items (diagnostics; 0009 §3 Space d).
    pub fn diagnostics(picker: Picker) -> Self {
        Self {
            picker,
            tx: None,
            rx: None,
            grep_worker: None,
        }
    }
}

pub struct PickerGlue {
    pub picker: Picker,
    /// Sender stays alive for grep respawns (kill + respawn per keystroke).
    tx: Option<Sender<PickerMsg>>,
    rx: Option<Receiver<PickerMsg>>,
    grep_worker: Option<GrepWorker>,
}

impl Editor {
    pub fn open_picker(&mut self, kind: Kind) {
        let (tx, rx) = channel();
        let mut tx = Some(tx);
        let (items, streaming, rx) = match kind {
            Kind::Buffers => {
                // MRU-ordered (0003 §2): most-recent *other* buffer first,
                // vim's alternate-file instinct.
                let items = self
                    .mru
                    .iter()
                    .map(|&i| {
                        let name = self.buffers[i]
                            .path
                            .clone()
                            .unwrap_or_else(|| "[scratch]".into());
                        Item {
                            text: name,
                            payload: Payload::Buffer(i),
                        }
                    })
                    .collect();
                (items, false, None)
            }
            Kind::Files => {
                spawn_files(self.cwd.clone(), tx.take().expect("fresh channel"));
                (vec![], true, Some(rx))
            }
            Kind::Grep | Kind::Replace => (vec![], false, Some(rx)),
            Kind::Diagnostics => unreachable!("diagnostics use PickerGlue::diagnostics"),
        };
        let picker = Picker::new(kind, items, streaming);
        self.picker = Some(PickerGlue {
            picker,
            tx,
            rx,
            grep_worker: None,
        });
    }

    pub fn close_picker(&mut self) {
        self.picker = None;
    }

    pub fn picker_open(&self) -> bool {
        self.picker.is_some()
    }

    /// Keystrokes while a picker is open. The input line is insert-mode
    /// semantics (0003 §1); nav is arrows / ctrl-n,p / tab.
    pub(crate) fn feed_picker(&mut self, key: Key) {
        let Some(glue) = &mut self.picker else { return };
        let replace = glue.picker.kind == Kind::Replace;
        match key {
            Key::Esc => self.close_picker(),
            Key::Enter if replace => self.apply_replace(),
            Key::Enter => {
                let payload = glue.picker.current().map(|i| i.payload.clone());
                self.picker = None;
                if let Some(p) = payload {
                    self.accept_picker(p);
                }
            }
            // 0007 §2: Tab cycles the two fields; results nav is
            // arrows / ctrl-n,p while a field has focus
            Key::Tab | Key::Backtab if replace => glue.picker.toggle_field(),
            Key::CtrlX if replace => glue.picker.toggle_excluded(),
            Key::CtrlX => {}
            Key::Backspace => {
                if replace && glue.picker.field == strop_picker::Field::Replace {
                    glue.picker.pop_replace_char();
                } else {
                    glue.picker.pop_char();
                    self.picker_input_changed();
                }
            }
            Key::CtrlR | Key::CtrlW => {}
            Key::Up => glue.picker.move_by(-1),
            Key::Down => glue.picker.move_by(1),
            Key::Tab => glue.picker.move_by(1),
            Key::Backtab => glue.picker.move_by(-1),
            Key::Char(c) => {
                if replace && glue.picker.field == strop_picker::Field::Replace {
                    glue.picker.push_replace_char(c);
                } else {
                    glue.picker.push_char(c);
                    self.picker_input_changed();
                }
            }
        }
    }

    fn picker_input_changed(&mut self) {
        let Some(glue) = &mut self.picker else { return };
        if matches!(glue.picker.kind, Kind::Grep | Kind::Replace) {
            // rg filters; kill + respawn per keystroke (worker is cheap).
            // A fresh channel per respawn: the old worker's messages (incl.
            // its trailing Done) fail to send on the dropped receiver, so
            // stale generations can't race the new one.
            let pattern = glue.picker.input.clone();
            let cwd = self.cwd.clone();
            glue.grep_worker = None; // drop kills the old rg
            glue.picker.items.clear();
            glue.picker.excluded.clear(); // item indices die with the respawn
            let (tx, rx) = channel();
            glue.tx = Some(tx.clone());
            glue.rx = Some(rx);
            glue.grep_worker = GrepWorker::spawn(&pattern, &cwd, tx);
            glue.picker.streaming = glue.grep_worker.is_some();
        } else {
            glue.picker.refilter();
        }
    }

    /// Drain worker messages (called from the event loop each tick).
    pub fn drain_picker(&mut self) {
        let mut done = false;
        if let Some(glue) = &mut self.picker {
            if let Some(rx) = &glue.rx {
                let mut items = Vec::new();
                while let Ok(msg) = rx.try_recv() {
                    match msg {
                        PickerMsg::Items(batch) => items.extend(batch),
                        PickerMsg::Done => done = true,
                    }
                }
                if !items.is_empty() {
                    glue.picker.append(items);
                }
            }
            if done {
                glue.picker.streaming = false;
            }
        }
        self.drain_previews();
    }

    /// Drain preview worker results (file reads happen off the render
    /// path — 0001 §3).
    fn drain_previews(&mut self) {
        while let Ok((path, text)) = self.preview_rx.try_recv() {
            self.preview_inflight.remove(&path);
            if let Some(text) = text {
                let rope = ropey::Rope::from_str(&text);
                let hl = Highlighter::for_path(&path.display().to_string());
                self.previews.insert(path, PreviewEntry { rope, hl });
            } else {
                // unreadable: cache the miss so we don't respawn per frame
                self.previews.insert(
                    path,
                    PreviewEntry {
                        rope: ropey::Rope::from_str(""),
                        hl: None,
                    },
                );
            }
        }
    }

    fn accept_picker(&mut self, payload: Payload) {
        match payload {
            Payload::File(rel) => {
                let path = self.cwd.join(&rel);
                match self.open_buffer(&path.display().to_string()) {
                    Ok(()) => {}
                    Err(e) => self.message = format!("open {}: {e}", rel.display()),
                }
            }
            Payload::Buffer(i) => {
                if i < self.buffers.len() {
                    self.current = i;
                    self.touch_mru(i);
                    self.cursor = 0;
                    self.view_top = 0;
                }
            }
            Payload::Grep {
                path, line, col, ..
            } => {
                let full = self.cwd.join(&path);
                if let Err(e) = self.open_buffer(&full.display().to_string()) {
                    self.message = format!("open {}: {e}", path.display());
                    return;
                }
                let start = self.buf().line_start(line.saturating_sub(1));
                self.cursor = self.buf().clamp_boundary(start + col.saturating_sub(1));
                self.clamp_cursor();
            }
        }
    }

    /// Replace mode Enter: apply every accepted hit. One undo revision
    /// per touched buffer (0007 §4); lines that drifted since the search
    /// are skipped and counted, never silently rewritten.
    fn apply_replace(&mut self) {
        let Some(glue) = self.picker.take() else {
            return;
        };
        let replacement = glue.picker.replace_input.clone();
        let mut by_path: HashMap<PathBuf, Vec<(usize, usize, usize, String)>> = HashMap::new();
        for it in glue.picker.accepted() {
            if let Payload::Grep {
                path,
                line,
                col,
                match_len,
                line_text,
            } = &it.payload
            {
                by_path.entry(path.clone()).or_default().push((
                    *line,
                    *col,
                    *match_len,
                    line_text.clone(),
                ));
            }
        }
        if by_path.is_empty() {
            self.message = "replace: no matches".into();
            return;
        }
        let mut files = 0usize;
        let mut applied = 0usize;
        let mut stale = 0usize;
        for (rel, mut hits) in by_path {
            // bottom-up: earlier hits' offsets stay valid while applying
            hits.sort_by(|a, b| b.0.cmp(&a.0).then(b.1.cmp(&a.1)));
            let full = self.cwd.join(&rel);
            let (f, a, s) = if let Some(bi) = self.buffer_index_of(&full) {
                self.replace_in_buffer(bi, &hits, &replacement)
            } else {
                Self::replace_in_file(&full, &hits, &replacement)
            };
            files += f;
            applied += a;
            stale += s;
        }
        let stale_msg = if stale > 0 {
            format!(" · {stale} stale skipped")
        } else {
            String::new()
        };
        self.message =
            format!("replaced {applied} in {files} files (u per buffer to undo){stale_msg}");
    }

    /// Open-buffer index for an absolute path, if loaded.
    fn buffer_index_of(&self, abs: &std::path::Path) -> Option<usize> {
        self.buffers.iter().position(|b| {
            b.path
                .as_deref()
                .map(|p| {
                    let p = std::path::Path::new(p);
                    let buf_abs = if p.is_absolute() {
                        p.to_path_buf()
                    } else {
                        self.cwd.join(p)
                    };
                    buf_abs == abs
                        || buf_abs.canonicalize().ok().as_ref() == Some(&abs.to_path_buf())
                })
                .unwrap_or(false)
        })
    }

    /// Verified, bottom-up replacement in an open buffer: one history
    /// transaction → one `u` reverts this buffer's replacements.
    fn replace_in_buffer(
        &mut self,
        bi: usize,
        hits: &[(usize, usize, usize, String)],
        replacement: &str,
    ) -> (usize, usize, usize) {
        let mut applied = 0;
        let mut stale = 0;
        let buf = &mut self.buffers[bi];
        if buf.readonly {
            return (0, 0, hits.len());
        }
        buf.history.begin();
        for (line, col, match_len, expected) in hits {
            if *line == 0 || *line > buf.len_lines() {
                stale += 1;
                continue;
            }
            // verify the matched *span*, not the whole line: same-line
            // hits stay verifiable as earlier (rightward) ones apply
            let (s, e) = strop_picker::replace_span(expected, *col, *match_len);
            let ls = buf.line_start(line - 1);
            let abs_s = ls + s;
            let abs_e = (ls + e).min(buf.len_bytes());
            if abs_s > abs_e || buf.rope.slice(abs_s..abs_e) != expected[s..e] {
                stale += 1;
                continue;
            }
            buf.delete(strop_core::Range::charwise(abs_s, abs_e));
            buf.insert(abs_s, replacement);
            applied += 1;
        }
        buf.history.commit();
        ((applied > 0) as usize, applied, stale)
    }

    /// Replace hits in a file that isn't open: verified line-by-line,
    /// mtime-guarded, written atomically (temp + rename) — never a silent
    /// partial write (0007 §4). Returns (touched, applied, stale).
    fn replace_in_file(
        path: &std::path::Path,
        hits: &[(usize, usize, usize, String)],
        replacement: &str,
    ) -> (usize, usize, usize) {
        let Ok(meta) = std::fs::metadata(path) else {
            return (0, 0, hits.len());
        };
        let Ok(text) = std::fs::read_to_string(path) else {
            return (0, 0, hits.len());
        };
        let line_offsets: Vec<usize> = std::iter::once(0)
            .chain(text.match_indices('\n').map(|(i, _)| i + 1))
            .collect();
        let mut content = text.clone();
        let mut applied = 0;
        let mut stale = 0;
        for (line, col, match_len, expected) in hits {
            let Some(&ls) = line_offsets.get(line.saturating_sub(1)) else {
                stale += 1;
                continue;
            };
            // verify the matched *span* (same-line hits stay verifiable
            // as rightward ones apply), in content: bottom-up order keeps
            // smaller offsets valid
            let (s, e) = strop_picker::replace_span(expected, *col, *match_len);
            let abs_s = ls + s;
            let abs_e = ls + e;
            if content.get(abs_s..abs_e) != expected.get(s..e) {
                stale += 1;
                continue;
            }
            content.replace_range(abs_s..abs_e, replacement);
            applied += 1;
        }
        if applied == 0 {
            return (0, 0, stale);
        }
        // mtime guard: somebody rewrote the file under the search — skip
        let moved =
            std::fs::metadata(path).ok().and_then(|m| m.modified().ok()) != meta.modified().ok();
        if moved {
            return (0, 0, applied + stale);
        }
        let tmp = path.with_file_name(format!(
            "{}.strop-tmp",
            path.file_name().and_then(|n| n.to_str()).unwrap_or("strop")
        ));
        if std::fs::write(&tmp, &content).is_err() || std::fs::rename(&tmp, path).is_err() {
            let _ = std::fs::remove_file(&tmp);
            return (0, 0, applied + stale);
        }
        (1, applied, stale)
    }
    /// Preview payload for the render layer: (title, focus line, rope).
    /// Files are read once and cached with a highlighter; buffers render
    /// from the live rope.
    pub fn picker_preview(&mut self) -> Option<(String, Option<usize>, PreviewSource<'_>)> {
        let item = self.picker.as_ref()?.picker.current()?.clone();
        match item.payload {
            Payload::Buffer(i) => {
                let name = self
                    .buffers
                    .get(i)?
                    .path
                    .clone()
                    .unwrap_or_else(|| "[scratch]".into());
                let _ = i;
                Some((name, None, PreviewSource::Buffer(i)))
            }
            Payload::File(rel) => {
                let full = self.cwd.join(&rel);
                if !self.preview_ready(&full) {
                    return Some((rel.display().to_string(), None, PreviewSource::Loading));
                }
                let entry = self.previews.get_mut(&full)?;
                Some((
                    rel.display().to_string(),
                    None,
                    PreviewSource::Cached(entry),
                ))
            }
            Payload::Grep { path, line, .. } => {
                let full = self.cwd.join(&path);
                if !self.preview_ready(&full) {
                    return Some((path.display().to_string(), None, PreviewSource::Loading));
                }
                let entry = self.previews.get_mut(&full)?;
                Some((
                    path.display().to_string(),
                    Some(line),
                    PreviewSource::Cached(entry),
                ))
            }
        }
    }

    /// True when the preview is cached; otherwise kicks a worker thread
    /// (size-capped read) and reports false — the next tick picks it up.
    fn preview_ready(&mut self, path: &PathBuf) -> bool {
        if self.previews.contains_key(path) {
            return true;
        }
        if self.preview_inflight.insert(path.clone()) {
            let tx = self.preview_tx.clone();
            let p = path.clone();
            std::thread::spawn(move || {
                let text = std::fs::metadata(&p)
                    .ok()
                    .filter(|m| m.len() <= 512 * 1024)
                    .and_then(|_| std::fs::read_to_string(&p).ok());
                let _ = tx.send((p, text));
            });
        }
        false
    }
}

pub struct PreviewEntry {
    pub rope: ropey::Rope,
    pub hl: Option<Highlighter>,
}

pub enum PreviewSource<'a> {
    /// Live buffer, highlighted with its own per-buffer highlighter.
    Buffer(usize),
    Cached(&'a mut PreviewEntry),
    /// Worker read still in flight (or unreadable); render shows a
    /// placeholder, never blocks.
    Loading,
}

pub type Previews = HashMap<PathBuf, PreviewEntry>;

#[cfg(test)]
mod replace_tests {
    use super::*;
    use strop_core::Buffer;

    /// One hit tuple: (line, col, match_len, expected line text).
    fn hit(line: usize, col: usize, len: usize, text: &str) -> (usize, usize, usize, String) {
        (line, col, len, text.to_string())
    }

    #[test]
    fn buffer_replace_applies_bottom_up_and_verifies() {
        let mut e = Editor::new(Buffer::from_text("foo bar foo\n"));
        let hits = vec![
            hit(1, 9, 3, "foo bar foo"),
            hit(1, 1, 3, "foo bar foo"),
            hit(1, 5, 3, "WRONG — stale line"),
        ];
        let (touched, applied, stale) = e.replace_in_buffer(0, &hits, "baz");
        assert_eq!((touched, applied, stale), (1, 2, 1));
        assert_eq!(e.buf().rope.to_string(), "baz bar baz\n");
        // one undo revision for the whole apply (0007 §4)
        e.undo();
        assert_eq!(e.buf().rope.to_string(), "foo bar foo\n");
    }

    #[test]
    fn file_replace_writes_atomically_and_verifies() {
        let dir = std::env::temp_dir().join(format!("strop-replace-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let file = dir.join("a.txt");
        std::fs::write(&file, "alpha foo\nbeta foo\ngamma\n").unwrap();
        let hits = vec![
            hit(2, 6, 3, "beta foo"),
            hit(1, 7, 3, "alpha foo"),
            hit(3, 1, 5, "drifted"),
        ];
        let (touched, applied, stale) = Editor::replace_in_file(&file, &hits, "bar");
        assert_eq!((touched, applied, stale), (1, 2, 1));
        assert_eq!(
            std::fs::read_to_string(&file).unwrap(),
            "alpha bar\nbeta bar\ngamma\n"
        );
        assert!(
            !dir.join("a.txt.strop-tmp").exists(),
            "temp file renamed away"
        );
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn excluded_rows_stay_out_of_the_apply_set() {
        let mut p = Picker::new(
            Kind::Replace,
            vec![
                Item {
                    text: "a".into(),
                    payload: Payload::Grep {
                        path: PathBuf::from("a"),
                        line: 1,
                        col: 1,
                        match_len: 1,
                        line_text: "x".into(),
                    },
                },
                Item {
                    text: "b".into(),
                    payload: Payload::Grep {
                        path: PathBuf::from("b"),
                        line: 1,
                        col: 1,
                        match_len: 1,
                        line_text: "y".into(),
                    },
                },
            ],
            false,
        );
        p.toggle_excluded(); // excludes row 0
        assert_eq!(p.accepted().count(), 1);
        assert_eq!(p.accepted().next().unwrap().text, "b");
        p.toggle_excluded(); // toggles back
        assert_eq!(p.accepted().count(), 2);
    }

    #[test]
    fn space_r_replace_field_flow() {
        let mut e = Editor::new(Buffer::from_text("x\n"));
        e.feed_text(" Rfoo");
        assert_eq!(e.picker.as_ref().unwrap().picker.kind, Kind::Replace);
        e.feed(crate::editor::Key::Tab);
        assert_eq!(
            e.picker.as_ref().unwrap().picker.field,
            strop_picker::Field::Replace
        );
        e.feed_text("bar");
        assert_eq!(e.picker.as_ref().unwrap().picker.replace_input, "bar");
        assert_eq!(e.picker.as_ref().unwrap().picker.input, "foo");
    }
}