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
//! Line-scan and search-scan orchestrators on `Editor`.
//!
//! Drive the chunked-scan subsystems extracted in phase 2 (LineScan,
//! SearchScan) one batch per render frame. Coordinate with
//! self.buffers (to read leaves and apply scan results), the tokio
//! runtime (for concurrent filesystem I/O), and the status message
//! (for progress reporting).
use rust_i18n::t;
use crate::model::event::BufferId;
use crate::view::prompt::PromptType;
use super::Editor;
impl Editor {
/// Start an incremental line-feed scan for the active buffer.
///
/// Shared by the `Action::ScanLineIndex` command and the Go to Line scan
/// confirmation prompt. Seeds `LineScan` so that `process_line_scan`
/// will advance the scan one batch per frame.
///
/// When `open_goto_line` is true (Go to Line flow), the Go to Line prompt
/// opens automatically when the scan completes.
pub fn start_incremental_line_scan(&mut self, open_goto_line: bool) {
let buffer_id = self.active_buffer();
if let Some(state) = self
.windows
.get_mut(&self.active_window)
.map(|w| &mut w.buffers)
.expect("active window present")
.get_mut(&buffer_id)
{
let (chunks, total_bytes) = state.buffer.prepare_line_scan();
let leaves = state.buffer.piece_tree_leaves();
self.active_window_mut().line_scan.start(
buffer_id,
leaves,
chunks,
total_bytes,
open_goto_line,
);
self.set_status_message(t!("goto.scanning_progress", percent = 0).to_string());
}
}
/// Process chunks for the incremental line-feed scan.
/// Returns `true` if the UI should re-render (progress updated or scan finished).
pub fn process_line_scan(&mut self) -> bool {
let _span = tracing::info_span!("process_line_scan").entered();
let Some(buffer_id) = self.active_window_mut().line_scan.buffer_id() else {
return false;
};
if let Err(e) = self.process_line_scan_batch(buffer_id) {
tracing::warn!("Line scan error: {e}");
self.finish_line_scan_with_error(e);
return true;
}
if self.active_window_mut().line_scan.is_done() {
self.finish_line_scan_ok();
} else {
let pct = self.active_window_mut().line_scan.progress_percent();
self.set_status_message(t!("goto.scanning_progress", percent = pct).to_string());
}
true
}
/// Process leaves concurrently, yielding for a render after each batch.
///
/// For loaded leaves, delegates to `TextBuffer::scan_leaf` (shared counting
/// logic). For unloaded leaves, extracts I/O parameters and runs them
/// concurrently using `tokio::task::spawn_blocking` — each task calls
/// `count_line_feeds_in_range` on the filesystem, which remote implementations
/// override to count on the server without transferring data.
fn process_line_scan_batch(&mut self, buffer_id: BufferId) -> std::io::Result<()> {
let _span = tracing::info_span!("process_line_scan_batch").entered();
let concurrency = self.config.editor.read_concurrency.max(1);
let mut results: Vec<(usize, usize)> = Vec::new();
let mut io_work: Vec<(usize, std::path::PathBuf, u64, usize)> = Vec::new();
let active_id = self.active_window;
let has_state = self
.windows
.get(&active_id)
.expect("active window present")
.buffers
.contains_key(&buffer_id);
if !has_state {
return Ok(());
}
// Pull chunks up to the concurrency budget, skipping already-known
// leaves. The budget is in terms of actual work items, so we keep
// asking for more chunks until we fill it or run out.
'outer: while results.len() + io_work.len() < concurrency {
let batch = self
.windows
.get_mut(&active_id)
.expect("active window present")
.line_scan
.take_next_chunks(concurrency - (results.len() + io_work.len()));
if batch.is_empty() {
break;
}
for chunk in batch {
if chunk.already_known {
continue;
}
let leaf = self
.windows
.get(&active_id)
.expect("active window present")
.line_scan
.leaves()[chunk.leaf_index]
.clone();
let leaf = &leaf;
let win = self.windows.get(&active_id).expect("active window present");
let Some(state) = win.buffers.get(&buffer_id) else {
break 'outer;
};
match state.buffer.leaf_io_params(leaf) {
None => {
// Loaded: count in-memory via scan_leaf
let count = state.buffer.scan_leaf(leaf)?;
results.push((chunk.leaf_index, count));
}
Some((path, offset, len)) => {
// Unloaded: batch for concurrent I/O
io_work.push((chunk.leaf_index, path, offset, len));
}
}
}
}
// Run I/O concurrently using tokio::task::spawn_blocking
if !io_work.is_empty() {
let fs = match self
.windows
.get(&active_id)
.expect("active window present")
.buffers
.get(&buffer_id)
{
Some(s) => s.buffer.filesystem().clone(),
None => return Ok(()),
};
let rt = self
.tokio_runtime
.as_ref()
.ok_or_else(|| std::io::Error::other("async runtime not available"))?;
let io_results: Vec<std::io::Result<(usize, usize)>> = rt.block_on(async {
let mut handles = Vec::with_capacity(io_work.len());
for (leaf_idx, path, offset, len) in io_work {
let fs = fs.clone();
handles.push(tokio::task::spawn_blocking(move || {
let count = fs.count_line_feeds_in_range(&path, offset, len)?;
Ok((leaf_idx, count))
}));
}
let mut results = Vec::with_capacity(handles.len());
for handle in handles {
results.push(handle.await.unwrap());
}
results
});
for result in io_results {
results.push(result?);
}
}
for (leaf_idx, count) in results {
self.active_window_mut()
.line_scan
.append_update(leaf_idx, count);
}
Ok(())
}
fn finish_line_scan_ok(&mut self) {
let _span = tracing::info_span!("finish_line_scan_ok").entered();
let Some(finished) = self.active_window_mut().line_scan.take_finished() else {
return;
};
if let Some(state) = self
.windows
.get_mut(&self.active_window)
.map(|w| &mut w.buffers)
.expect("active window present")
.get_mut(&finished.buffer_id)
{
let _span = tracing::info_span!(
"rebuild_with_pristine_saved_root",
updates = finished.updates.len()
)
.entered();
state
.buffer
.rebuild_with_pristine_saved_root(&finished.updates);
}
self.set_status_message(t!("goto.scan_complete").to_string());
if finished.open_goto_line {
self.open_goto_line_if_active(finished.buffer_id);
}
}
fn finish_line_scan_with_error(&mut self, e: std::io::Error) {
let Some(finished) = self.active_window_mut().line_scan.take_finished() else {
return;
};
self.set_status_message(t!("goto.scan_failed", error = e.to_string()).to_string());
if finished.open_goto_line {
self.open_goto_line_if_active(finished.buffer_id);
}
}
fn open_goto_line_if_active(&mut self, buffer_id: BufferId) {
if self.active_buffer() == buffer_id {
self.start_prompt(
t!("file.goto_line_prompt").to_string(),
PromptType::GotoLine,
);
}
}
// === Incremental Search Scan (for large files) ===
/// Process chunks for the incremental search scan.
/// Returns `true` if the UI should re-render (progress updated or scan finished).
pub fn process_search_scan(&mut self) -> bool {
let Some(buffer_id) = self.active_window_mut().search_scan.buffer_id() else {
return false;
};
if let Err(e) = self.process_search_scan_batch(buffer_id) {
tracing::warn!("Search scan error: {e}");
self.active_window_mut().search_scan.abandon();
self.set_status_message(format!("Search failed: {e}"));
return true;
}
if self.active_window_mut().search_scan.is_done() {
self.finish_search_scan();
} else {
let pct = self.active_window_mut().search_scan.progress_percent();
let match_count = self.active_window_mut().search_scan.match_count();
self.set_status_message(format!(
"Searching... {}% ({} matches so far)",
pct, match_count
));
}
true
}
/// Process a batch of search chunks by delegating to
/// `TextBuffer::search_scan_next_chunk`.
fn process_search_scan_batch(
&mut self,
buffer_id: crate::model::event::BufferId,
) -> std::io::Result<()> {
let concurrency = self.config.editor.read_concurrency.max(1);
for _ in 0..concurrency {
if self.active_window_mut().search_scan.is_done() {
break;
}
// Extract the ChunkedSearchState, run one chunk on the buffer,
// then put it back. This is the same take/restore dance the
// previous `Option<SearchScanState>` code did, now wrapped in
// the subsystem's API so we're not poking its internals.
let Some(mut chunked) = self.active_window_mut().search_scan.take_chunked() else {
return Ok(());
};
let result = if let Some(state) = self
.windows
.get_mut(&self.active_window)
.map(|w| &mut w.buffers)
.expect("active window present")
.get_mut(&buffer_id)
{
state.buffer.search_scan_next_chunk(&mut chunked)
} else {
Ok(false)
};
self.active_window_mut()
.search_scan
.restore_chunked(chunked);
match result {
Ok(false) => break, // scan complete
Ok(true) => {} // more chunks
Err(e) => return Err(e),
}
}
Ok(())
}
/// Finalize the incremental search scan: take the accumulated matches
/// and hand them to `finalize_search()` which sets search_state, moves
/// the cursor, and creates viewport overlays.
fn finish_search_scan(&mut self) {
let Some(finished) = self.active_window_mut().search_scan.take_finished() else {
return;
};
// The search scan loaded chunks via chunk_split_and_load, which
// restructures the piece tree. Refresh saved_root so that
// diff_since_saved() can take the fast Arc::ptr_eq path.
if let Some(state) = self
.windows
.get_mut(&self.active_window)
.map(|w| &mut w.buffers)
.expect("active window present")
.get_mut(&finished.buffer_id)
{
state.buffer.refresh_saved_root_if_unmodified();
}
if finished.match_ranges.is_empty() {
self.active_window_mut().search_state = None;
self.set_status_message(format!("No matches found for '{}'", finished.query));
return;
}
self.finalize_search(
&finished.query,
finished.match_ranges,
finished.capped,
None,
);
}
}