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
//! Interactive `:s/pat/rep/c` confirm-mode handler.
//!
//! When [`App::confirming_substitute`] is `Some`, key presses are routed here
//! instead of the editor engine. The user is prompted once per candidate match:
//!
//! - `y` — accept current match, advance.
//! - `n` — skip current match, advance.
//! - `a` — accept this and all remaining; finish.
//! - `q` / `Esc` — abort (keep any already-accepted matches).
//! - `l` — accept current then finish ("last").
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use hjkl_engine::{Query, apply_collected_matches};
use super::{App, ConfirmingSubstitute};
impl App {
/// Route a keypress to the confirm-substitute prompt.
///
/// Called from `handle_keypress` when `confirming_substitute.is_some()`.
/// Returns `true` when the key was consumed (always true for this handler).
pub(crate) fn handle_confirm_substitute_key(&mut self, key: KeyEvent) -> bool {
let action = match (key.code, key.modifiers) {
(KeyCode::Char('y'), KeyModifiers::NONE) => ConfirmAction::Accept,
(KeyCode::Char('n'), KeyModifiers::NONE) => ConfirmAction::Skip,
(KeyCode::Char('a'), KeyModifiers::NONE) => ConfirmAction::AcceptAll,
(KeyCode::Char('l'), KeyModifiers::NONE) => ConfirmAction::Last,
(KeyCode::Char('q'), KeyModifiers::NONE) | (KeyCode::Esc, _) => ConfirmAction::Quit,
_ => {
// Any other key: re-show the prompt and consume the key.
return true;
}
};
// Borrow the session state.
let cs = match self.confirming_substitute.as_mut() {
Some(s) => s,
None => return true,
};
match action {
ConfirmAction::Accept => {
cs.accepted[cs.idx] = true;
advance_or_finish(cs);
}
ConfirmAction::Skip => {
advance_or_finish(cs);
}
ConfirmAction::AcceptAll => {
let idx = cs.idx;
for a in &mut cs.accepted[idx..] {
*a = true;
}
// Mark as done by pushing idx past end.
cs.idx = cs.matches.len();
}
ConfirmAction::Last => {
cs.accepted[cs.idx] = true;
cs.idx = cs.matches.len(); // Force finish.
}
ConfirmAction::Quit => {
cs.idx = cs.matches.len(); // Force finish.
}
}
let done = self
.confirming_substitute
.as_ref()
.is_none_or(|cs| cs.idx >= cs.matches.len());
if done {
// Session is done — apply accepted matches.
self.finish_confirm_substitute();
} else {
// Advance cursor to next match.
self.jump_to_current_confirm_match();
}
true
}
/// Apply all accepted matches and clear the session.
fn finish_confirm_substitute(&mut self) {
let cs = match self.confirming_substitute.take() {
Some(s) => s,
None => return,
};
let accepted_count = cs.accepted.iter().filter(|&&b| b).count();
if accepted_count == 0 {
self.bus.info("0 substitutions on 0 lines");
return;
}
let idx = self.focused_slot_idx();
self.slots[idx].editor.push_undo();
let applied =
apply_collected_matches(&mut self.slots[idx].editor, &cs.matches, &cs.accepted);
// Count distinct lines changed.
let lines_changed = {
let mut rows: Vec<u32> = cs
.matches
.iter()
.zip(cs.accepted.iter())
.filter_map(|(m, &ok)| if ok { Some(m.row) } else { None })
.collect();
rows.sort_unstable();
rows.dedup();
rows.len()
};
// Propagate dirty state through the usual pipeline.
if self.slots[idx].editor.take_dirty() {
let elapsed = self.slots[idx].refresh_dirty_against_saved();
self.last_signature_us = elapsed;
let buffer_id = self.slots[idx].buffer_id;
if self.slots[idx].editor.take_content_reset() {
self.syntax.reset(buffer_id);
}
let edits = self.slots[idx].editor.take_content_edits();
if !edits.is_empty() {
self.syntax.apply_edits(buffer_id, &edits);
}
self.recompute_and_install();
}
self.bus
.info(format!("{applied} substitutions on {lines_changed} lines"));
}
/// Move the cursor to the current confirm match and sync the viewport.
pub(crate) fn jump_to_current_confirm_match(&mut self) {
let (row, col) = match self.confirming_substitute.as_ref() {
Some(cs) if cs.idx < cs.matches.len() => {
let m = &cs.matches[cs.idx];
let r = m.row as usize;
let col = {
let rope = Query::rope(self.active().editor.buffer());
let line = hjkl_buffer::rope_line_str(&rope, r);
line[..m.byte_start as usize].chars().count()
};
(r, col)
}
_ => return,
};
self.active_mut().editor.jump_cursor(row, col);
self.sync_after_engine_mutation();
}
}
/// Which action the user took for the current match.
enum ConfirmAction {
Accept,
Skip,
AcceptAll,
Last,
Quit,
}
/// Advance `cs.idx` to the next match, or mark session as done.
fn advance_or_finish(cs: &mut ConfirmingSubstitute) {
cs.idx += 1;
// idx >= len signals "done" — checked by the caller.
}