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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Workspace grep via ripgrep / git-grep + grep-pane navigation +
//! per-file replace.
//!
//! Sub-extracted from `app/editor_features.rs`. Non-destructive move.
use super::*;
impl App {
/// Release focus on the search input. Other dispatch paths route
/// to the editor again.
pub fn search_section_blur(&mut self) {
self.search_input_focused = false;
}
/// Append `c` to the search query (with simple cursor at end).
/// Live-search would re-run on every keystroke; we wait for Enter
/// to avoid the user paying for half-typed queries.
pub fn search_section_insert_char(&mut self, c: char) {
self.search_query.push(c);
self.search_cursor = self.search_query.chars().count();
}
/// Run the workspace grep on the current query — Enter inside the
/// search input fires here. Populates `search_hits` + `search_used`.
/// Multi-root workspaces are concat'd just like the existing pane
/// grep.
pub fn search_section_run(&mut self) {
let q = self.search_query.trim().to_string();
if q.is_empty() {
self.search_hits.clear();
self.search_used = "";
return;
}
// #polish 2026-07-07 — same scoping fix as run_workspace_grep
// (SEV-2 #3). Search panel now current-workspace-only.
// #1112 (2026-08-20) — pass the section's flag toggles
// through to ripgrep.
let flags = crate::app::GrepFlags {
case_sensitive: self.search_case_sensitive,
whole_word: self.search_whole_word,
regex: self.search_regex,
};
let (hits, used) = crate::app::grep_workspace_with_flags(&self.workspace, &q, flags);
self.search_hits = hits;
self.search_used = used;
self.search_selected = 0;
self.search_scroll = 0;
// #1112 (2026-08-20) — push into MRU history dedup'd. The
// Up/Down input keys walk this list.
self.search_history.retain(|s| s != &q);
self.search_history.insert(0, q);
if self.search_history.len() > 20 {
self.search_history.truncate(20);
}
self.search_history_cursor = None;
}
/// #1112 (2026-08-20) — Flag toggles. Firing any of these
/// re-runs the current query so the results reflect the new
/// mode immediately.
pub fn search_section_toggle_case_sensitive(&mut self) {
self.search_case_sensitive = !self.search_case_sensitive;
self.search_section_run();
}
pub fn search_section_toggle_whole_word(&mut self) {
self.search_whole_word = !self.search_whole_word;
self.search_section_run();
}
pub fn search_section_toggle_regex(&mut self) {
self.search_regex = !self.search_regex;
self.search_section_run();
}
/// #1112 (2026-08-20) — Up/Down inside the input walks the MRU
/// query list. Delta -1 = older (Up), +1 = newer (Down). Bounds:
/// Up past the end sticks at the oldest entry; Down past the
/// front clears the cursor and restores whatever was typed.
/// (This differs from a strict shell history — we don't preserve
/// the pre-navigation buffer separately; the user re-types if
/// they wanted it back.)
pub fn search_section_history_step(&mut self, delta: isize) {
if self.search_history.is_empty() {
return;
}
let len = self.search_history.len() as isize;
let new_idx: isize = match self.search_history_cursor {
None if delta < 0 => 0,
None => return, // Down with no cursor: no-op
Some(i) => (i as isize + delta).clamp(-1, len - 1),
};
if new_idx < 0 {
self.search_history_cursor = None;
self.search_query.clear();
self.search_cursor = 0;
} else {
self.search_history_cursor = Some(new_idx as usize);
self.search_query = self.search_history[new_idx as usize].clone();
self.search_cursor = self.search_query.chars().count();
}
}
/// Move selection in the inline results list by `delta` (positive
/// = down, negative = up). No-op when there are no hits.
pub fn search_section_select(&mut self, delta: isize) {
if self.search_hits.is_empty() {
return;
}
let len = self.search_hits.len() as isize;
let new = (self.search_selected as isize + delta).clamp(0, len - 1);
self.search_selected = new as usize;
}
/// Open the focused hit in the editor (Enter on a result row,
/// while focus is NOT on the input box). Falls back gracefully
/// when no hit is selected.
pub fn search_section_open_selected(&mut self) {
let Some(hit) = self.search_hits.get(self.search_selected).cloned() else {
return;
};
let (path, line, col) = (hit.path, hit.line as usize, hit.col as usize);
self.open_path(&path);
if let Some(b) = self.active_editor_mut() {
b.editor.place_cursor(line, col);
}
}
/// Open a specific hit by index — used by the mouse handler when
/// a result row is clicked. Sets `search_selected` to the hit
/// before opening so the highlight follows the click.
pub fn search_section_open_hit(&mut self, idx: usize) {
if idx >= self.search_hits.len() {
return;
}
self.search_selected = idx;
self.search_section_open_selected();
}
/// `find.grep` (palette) — prompt for a query and grep the workspace.
pub fn open_grep_prompt(&mut self) {
// #polish 2026-07-06 — seed order:
// 1. Current editor selection (immediate context wins)
// 2. Last grep query (remembered across sessions)
// 3. Empty
let seed = match self.active.and_then(|i| self.panes.get(i)) {
Some(Pane::Editor(b)) if b.editor.has_selection() => b
.editor
.selected_text()
.lines()
.next()
.unwrap_or("")
.to_string(),
_ => self.last_grep_query.clone(),
};
self.prompt = Some(crate::prompt::Prompt::seeded(
crate::prompt::PromptKind::Grep,
"Grep workspace",
seed,
));
}
/// Run `rg --vimgrep <q> .` in the workspace (falling back to `git grep`),
/// parse `path:line:col:text` lines, and open the results in a `Pane::Grep`
/// (split below the focused leaf). If a grep pane is already open for an
/// earlier query, *that* pane is refilled in place — only one grep pane at
/// a time.
pub fn run_workspace_grep(&mut self, q: String) {
let q = q.trim().to_string();
if q.is_empty() {
return;
}
// #polish 2026-07-07 (vscode-user SEV-2 #3) — do NOT scan
// extra_workspaces. Was: grep results from every registered
// `[[workspaces]]` entry, so 4 workspace-local matches
// drowned in 2000+ from unrelated integration repos. Scope to
// the current workspace — matches VS Code's Ctrl+Shift+F
// convention.
let (hits, used) = grep_workspace(&self.workspace, &q);
if hits.is_empty() {
if used == "unavailable" {
// keyboard-round-9 SEV-2 2026-07-14 — rg not on PATH
// AND workspace isn't a git repo (git grep also
// failed). Was: silent "rg: no matches" toast that
// flashed and left the user staring at an empty
// screen. Now: persistent chip with the install
// hint until dismissed.
self.toast_persistent(
"grep.unavailable",
format!(
"grep unavailable — install ripgrep (`brew install ripgrep`) or run in a git repo. Query: {q:?}"
),
crate::app::ToastLevel::Warn,
);
} else {
self.toast(format!("{used}: no matches for {q:?}"));
}
return;
}
// Successful grep clears any stale unavailable-hint chip so
// the user isn't nagged after they install ripgrep.
self.toast_dismiss("grep.unavailable");
// Already showing a grep pane somewhere? Refresh it in place.
if let Some(id) = self.panes.iter().position(|p| matches!(p, Pane::Grep(_))) {
if let Some(Pane::Grep(g)) = self.panes.get_mut(id) {
*g = crate::grep_pane::GrepPane::new(q, used, hits);
}
// Right-panel v5 — bring the existing grep tab forward.
if let Some(idx) = self.right_panel_panes.iter().position(|&pid| pid == id) {
self.right_panel_active_idx = idx;
} else {
self.reveal_pane(id);
}
return;
}
let pane = Pane::Grep(crate::grep_pane::GrepPane::new(q, used, hits));
// Right-panel v5 — host grep results in the panel as a tab
// when visible. Each row is path:line:content; at narrow
// widths the path takes most of the cells, but the user can
// drag the column wider for the full hit text.
if self.right_panel_visible {
self.panes.push(pane);
let new_id = self.panes.len() - 1;
self.right_panel_push(new_id);
return;
}
match self.active {
Some(cur) => {
let new_id = self.split_leaf_with(cur, crate::layout::SplitDir::Vertical, pane);
self.active = Some(new_id);
}
None => {
self.panes.push(pane);
let id = self.panes.len() - 1;
*self.layout_mut() = Layout::leaf(id);
self.active = Some(id);
}
}
self.focus = Focus::Pane;
}
/// Re-run the grep that produced the active `Pane::Grep` (the `r` key).
pub fn rerun_active_grep(&mut self) {
let q = match self.active.and_then(|i| self.panes.get(i)) {
Some(Pane::Grep(g)) => g.query.clone(),
_ => return,
};
let (hits, used) = grep_workspace(&self.workspace, &q);
if let Some(Pane::Grep(g)) = self.active.and_then(|i| self.panes.get_mut(i)) {
*g = crate::grep_pane::GrepPane::new(q, used, hits);
}
}
pub fn move_grep_selection(&mut self, delta: isize) {
if let Some(Pane::Grep(g)) = self.active.and_then(|i| self.panes.get_mut(i)) {
g.move_selection(delta);
}
}
/// `y` in a grep pane — copy the selected hit's `path:line` (1-based) to
/// the system clipboard so the user can paste it into a commit message,
/// chat, etc.
pub fn copy_selected_grep_hit(&mut self) {
let s = match self.active.and_then(|i| self.panes.get(i)) {
Some(Pane::Grep(g)) => g
.selected_hit()
.map(|h| format!("{}:{}", h.rel, h.line + 1)),
_ => None,
};
let Some(s) = s else { return };
self.clipboard.set(s.clone(), false);
self.toast(format!("copied {s}"));
}
/// Open the highlighted grep hit's file and place the cursor there.
pub fn jump_to_selected_grep_hit(&mut self) {
let target = match self.active.and_then(|i| self.panes.get(i)) {
Some(Pane::Grep(g)) => g
.selected_hit()
.map(|it| (it.path.clone(), it.line, it.col)),
_ => None,
};
let Some((path, line, col)) = target else {
return;
};
self.open_path(&path);
if let Some(b) = self.active_editor_mut() {
b.editor.place_cursor(line as usize, col as usize);
}
}
/// `find.grep_replace` (the `R` key in a `Pane::Grep`) — prompt for a
/// replacement string. The grep pane's query is the seed, but the input
/// starts empty so the user can type the replacement without first deleting
/// the seed. Requires an active grep pane with at least one hit.
pub fn open_grep_replace_prompt(&mut self) {
let (query, n) = match self.active.and_then(|i| self.panes.get(i)) {
Some(Pane::Grep(g)) if !g.hits.is_empty() => (g.query.clone(), g.hits.len()),
Some(Pane::Grep(_)) => {
self.toast("no grep hits to replace");
return;
}
_ => return,
};
self.prompt = Some(crate::prompt::Prompt::new(
crate::prompt::PromptKind::GrepReplace,
format!("Replace {n}× \"{query}\" with"),
));
}
/// Replace every hit in the active `Pane::Grep` across every file it
/// matched. For each unique file:
/// - **Open + clean** ⇒ apply `EditOp::ReplaceRange`s through the buffer
/// (so undo works + LSP `didChange` fires).
/// - **Not open** ⇒ read the file from disk, splice in reverse, write back.
/// - **Open + dirty** ⇒ skip + toast (refuse to clobber unsaved edits).
///
/// The match positions are re-derived from each file's live text via
/// `crate::buffer::find_all_ci_ascii` (rather than trusting the grep tool's
/// line/col, which might be stale by now). After replacing, the grep query
/// is re-run so the pane reflects the new state.
pub fn run_grep_replace(&mut self, replacement: String) {
// Snapshot the (query, unique-file-paths) from the active grep pane.
// Per-hit toggle: hits whose index is in `g.disabled` are excluded —
// files with NO enabled hits are skipped entirely.
let (query, files, disabled_files) = match self.active.and_then(|i| self.panes.get(i)) {
Some(Pane::Grep(g)) => {
use std::collections::HashSet;
let mut files: Vec<PathBuf> = Vec::new();
let mut disabled_files: HashSet<PathBuf> = HashSet::new();
// Group hit-indices by file so we can see which files
// have at least one enabled hit.
let mut hits_by_file: std::collections::HashMap<PathBuf, Vec<usize>> =
std::collections::HashMap::new();
for (i, h) in g.hits.iter().enumerate() {
hits_by_file.entry(h.path.clone()).or_default().push(i);
}
for (path, idxs) in &hits_by_file {
let any_enabled = idxs.iter().any(|i| !g.disabled.contains(i));
if any_enabled {
if !files.iter().any(|p| p == path) {
files.push(path.clone());
}
} else {
disabled_files.insert(path.clone());
}
}
(g.query.clone(), files, disabled_files)
}
_ => return,
};
let _ = disabled_files; // (currently unused — kept for future per-line replace path)
if query.is_empty() {
return;
}
let mut total_replacements = 0usize;
let mut files_changed = 0usize;
let mut files_skipped: Vec<String> = Vec::new();
let mut io_errors: Vec<String> = Vec::new();
for path in &files {
// Is this file open as an editor pane? (Take the first such pane.)
let open_idx = self.panes.iter().position(
|p| matches!(p, Pane::Editor(b) if b.path.as_deref() == Some(path.as_path())),
);
if let Some(idx) = open_idx {
let is_dirty = matches!(self.panes.get(idx), Some(Pane::Editor(b)) if b.dirty);
if is_dirty {
files_skipped.push(rel_path(&self.workspace, path));
continue;
}
let text = match self.panes.get(idx) {
Some(Pane::Editor(b)) => b.editor.text().to_string(),
_ => continue,
};
let matches = crate::buffer::find_all_ci_ascii(&text, &query);
if matches.is_empty() {
continue;
}
let ops: Vec<crate::edit_op::EditOp> = matches
.iter()
.rev()
.map(|(s, e)| crate::edit_op::EditOp::ReplaceRange {
start: *s,
end: *e,
text: replacement.clone(),
})
.collect();
let n = ops.len();
let clip = &mut self.clipboard;
if let Some(Pane::Editor(b)) = self.panes.get_mut(idx) {
b.apply_edit_ops(ops, clip, 0);
// Persist the change to disk so the grep re-run reflects
// it (and so the user doesn't have to save N files by hand).
match b.save_to_disk() {
Ok(()) => {}
Err(e) => {
io_errors.push(format!("{}: {e}", rel_path(&self.workspace, path)));
continue;
}
}
}
// Push the new text through LSP just like a normal save.
if let Some(Pane::Editor(b)) = self.panes.get(idx) {
let t = b.editor.text().to_string();
self.lsp.did_change(path, &t);
}
total_replacements += n;
files_changed += 1;
} else {
// Not open — splice on disk.
let text = match std::fs::read_to_string(path) {
Ok(s) => s,
Err(e) => {
io_errors.push(format!("{}: {e}", rel_path(&self.workspace, path)));
continue;
}
};
let matches = crate::buffer::find_all_ci_ascii(&text, &query);
if matches.is_empty() {
continue;
}
let mut out = String::with_capacity(text.len());
let mut cursor = 0usize;
for (s, e) in &matches {
out.push_str(&text[cursor..*s]);
out.push_str(&replacement);
cursor = *e;
}
out.push_str(&text[cursor..]);
if let Err(e) = std::fs::write(path, &out) {
io_errors.push(format!("{}: {e}", rel_path(&self.workspace, path)));
continue;
}
total_replacements += matches.len();
files_changed += 1;
}
}
// Toast a summary.
let mut parts = vec![format!(
"replaced {total_replacements} in {files_changed} files"
)];
if !files_skipped.is_empty() {
parts.push(format!(
"skipped {} (unsaved): {}",
files_skipped.len(),
files_skipped.join(", ")
));
}
if !io_errors.is_empty() {
parts.push(format!("{} errored", io_errors.len()));
}
self.toast(parts.join(" · "));
// Refresh the grep pane against the new state.
self.rerun_active_grep();
}
}
#[cfg(test)]
mod grep_tests {
use super::*;
use std::fs;
fn app_with_files() -> (tempfile::TempDir, App) {
let d = tempfile::tempdir().unwrap();
fs::write(d.path().join("a.txt"), "alpha").unwrap();
fs::write(d.path().join("b.txt"), "beta").unwrap();
let app = App::new(d.path().to_path_buf(), Config::default()).unwrap();
(d, app)
}
#[test]
fn grep_pane_jump_opens_file_and_places_cursor() {
// Manually seed a Pane::Grep — the grep tool itself (rg / git grep)
// isn't reliably available in test sandboxes, but the rest of the flow
// (jump-to-hit) is the part we want to cover end-to-end.
let (_d, mut app) = app_with_files();
// `app.workspace` is the *canonicalized* tmp dir; the buffer the editor
// opens will hold the same canonical form, so compare against it.
let abs = app.workspace.join("a.txt");
// a.txt is `alpha`; pretend a tool matched at line 0, col 2.
let pane = Pane::Grep(crate::grep_pane::GrepPane::new(
"alpha".into(),
"rg",
vec![crate::grep_pane::GrepHit {
path: abs.clone(),
rel: "a.txt".into(),
line: 0,
col: 2,
text: "alpha".into(),
}],
));
app.panes.push(pane);
let id = app.panes.len() - 1;
*app.layout_mut() = Layout::leaf(id);
app.active = Some(id);
app.focus = Focus::Pane;
app.jump_to_selected_grep_hit();
// Opening the file added an editor pane and focused it.
assert!(matches!(
app.active.and_then(|i| app.panes.get(i)),
Some(Pane::Editor(b)) if b.is_at(&abs)
));
let buf = app.active_editor().unwrap();
assert_eq!(buf.editor.row_col(), (0, 2));
}
#[test]
fn grep_replace_writes_open_buffer_and_disk() {
// Two files, both contain `foo`. Open one as an editor (clean), leave
// the other on disk only. `run_grep_replace("BAR")` should rewrite
// both, replacing every match.
let d = tempfile::tempdir().unwrap();
fs::write(d.path().join("a.txt"), "foo bar foo").unwrap();
fs::write(d.path().join("b.txt"), "say foo loud").unwrap();
let mut app = App::new(d.path().to_path_buf(), Config::default()).unwrap();
let a = app.workspace.join("a.txt");
let b = app.workspace.join("b.txt");
app.open_path(&a); // a.txt now open as a clean editor
// Seed a Pane::Grep with hits for both files (positions don't need to
// be real — `run_grep_replace` re-derives matches via find_all_ci_ascii).
let mk_hit = |path: &Path, rel: &str| crate::grep_pane::GrepHit {
path: path.to_path_buf(),
rel: rel.into(),
line: 0,
col: 0,
text: "".into(),
};
let pane = Pane::Grep(crate::grep_pane::GrepPane::new(
"foo".into(),
"rg",
vec![mk_hit(&a, "a.txt"), mk_hit(&b, "b.txt")],
));
app.panes.push(pane);
let grep_id = app.panes.len() - 1;
// Make the grep pane the active one (so run_grep_replace targets it).
*app.layout_mut() = Layout::leaf(grep_id);
app.active = Some(grep_id);
app.run_grep_replace("BAR".into());
// a.txt was open + clean ⇒ the buffer + disk both updated.
let a_buf = app
.panes
.iter()
.find_map(|p| match p {
Pane::Editor(b) if b.is_at(&a) => Some(b),
_ => None,
})
.unwrap();
// The open buffer + on-disk file both got the in-memory update.
// Disk version has a trailing `\n` because the open buffer goes
// through `save_to_disk` which honors `ensure_trailing_newline`.
assert_eq!(a_buf.editor.text(), "BAR bar BAR\n");
assert!(!a_buf.dirty); // saved through to disk
assert_eq!(fs::read_to_string(&a).unwrap(), "BAR bar BAR\n");
// b.txt was disk-only ⇒ just the disk got rewritten. The
// disk-write path (grep_replace's direct splice, not `save_to_disk`)
// doesn't apply `ensure_trailing_newline` — that's a save-only step.
assert_eq!(fs::read_to_string(&b).unwrap(), "say BAR loud");
}
#[test]
fn grep_replace_skips_dirty_open_buffer() {
let d = tempfile::tempdir().unwrap();
fs::write(d.path().join("a.txt"), "foo").unwrap();
let mut app = App::new(d.path().to_path_buf(), Config::default()).unwrap();
let a = app.workspace.join("a.txt");
app.open_path(&a);
// Make the buffer dirty (without changing the matched text).
if let Some(Pane::Editor(b)) = app
.panes
.iter_mut()
.find(|p| matches!(p, Pane::Editor(b) if b.is_at(&a)))
{
b.editor.place_cursor(0, 3);
b.apply_edit_ops(
vec![crate::edit_op::EditOp::InsertStr("!".into())],
&mut Clipboard::new(),
0,
);
}
let pane = Pane::Grep(crate::grep_pane::GrepPane::new(
"foo".into(),
"rg",
vec![crate::grep_pane::GrepHit {
path: a.clone(),
rel: "a.txt".into(),
line: 0,
col: 0,
text: "".into(),
}],
));
app.panes.push(pane);
let grep_id = app.panes.len() - 1;
*app.layout_mut() = Layout::leaf(grep_id);
app.active = Some(grep_id);
app.run_grep_replace("BAR".into());
// Disk is untouched (the dirty buffer was skipped).
assert_eq!(fs::read_to_string(&a).unwrap(), "foo");
}
}