bigtext 0.7.1

Fast text editor for huge files (1-50+ GB): opens instantly, never freezes, streaming search/replace and atomic save
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
//! Search → Go to file (Ctrl+P): the files of the side bar's folder — or, with no folder, the
//! open tabs and recent files — found by a fuzzy match of their paths, as in Sublime Text.
//! `name:42` opens the file at line 42. The folder is listed on a worker, at most
//! [`MAX_FILES`] files, without build and version-control folders.

use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::time::{Duration, Instant};

use egui::text::{LayoutJob, TextFormat};
use egui::{Key, Modifiers};

use super::i18n::{tr, trf};
use super::BigTextApp;

/// Files listed from a folder at most.
pub const MAX_FILES: usize = 200_000;
/// Results shown.
const SHOWN: usize = 60;
/// A listing older than this is made again when Go to file opens.
const STALE: Duration = Duration::from_secs(15);
/// Folders not listed: version control, dependencies, caches and build output.
const SKIP_DIRS: &[&str] = &[".git", ".hg", ".svn", "CVS", "node_modules", "__pycache__", ".venv", "venv", ".idea", ".vs", ".mypy_cache", ".pytest_cache", ".gradle", "target"];

/// The dialog's state.
#[derive(Default)]
pub struct QuickOpen {
    pub query: String,
    pub selected: usize,
    focus: bool,
    /// The results, for the name and the listing they were found for.
    found: Vec<(usize, Vec<usize>)>,
    found_for: Option<(String, u64)>,
}

/// The files of the side bar's folder (paths relative to it, with `/`).
#[derive(Default)]
pub struct FileIndex {
    pub root: Option<PathBuf>,
    pub files: Vec<String>,
    /// More files than [`MAX_FILES`] were found.
    pub truncated: bool,
    listed: Option<Instant>,
    /// Counts the listings taken (results are found again after one).
    pub generation: u64,
    rx: Option<mpsc::Receiver<(PathBuf, Vec<String>, bool)>>,
}

impl FileIndex {
    pub fn listing(&self) -> bool {
        self.rx.is_some()
    }

    /// Lists `root` again on a worker unless a fresh listing of it is at hand.
    pub fn refresh(&mut self, root: &Path, waker: bigtext::jobs::Waker, force: bool) {
        let fresh = self.root.as_deref() == Some(root) && self.listed.is_some_and(|t| t.elapsed() < STALE);
        if (fresh && !force) || self.rx.is_some() {
            return;
        }
        if self.root.as_deref() != Some(root) {
            self.files.clear();
            self.truncated = false;
        }
        self.root = Some(root.to_path_buf());
        let (tx, rx) = mpsc::channel();
        self.rx = Some(rx);
        let root = root.to_path_buf();
        bigtext::jobs::spawn("list-files", move || {
            let (files, truncated) = walk(&root, MAX_FILES);
            let _ = tx.send((root, files, truncated));
            waker();
        });
    }

    /// Takes a finished listing.
    pub fn poll(&mut self) {
        let Some(rx) = &self.rx else { return };
        match rx.try_recv() {
            Ok((root, files, truncated)) => {
                self.rx = None;
                if self.root.as_deref() == Some(root.as_path()) {
                    self.files = files;
                    self.truncated = truncated;
                    self.generation += 1;
                    self.listed = Some(Instant::now());
                }
            }
            Err(mpsc::TryRecvError::Disconnected) => self.rx = None,
            Err(mpsc::TryRecvError::Empty) => {}
        }
    }
}

/// The files under `root` (relative, `/`-separated, sorted), at most `max`; and whether there
/// were more. Symbolic links to folders are not followed (they can loop).
pub fn walk(root: &Path, max: usize) -> (Vec<String>, bool) {
    let mut out = Vec::new();
    let mut stack = vec![(root.to_path_buf(), String::new())];
    while let Some((dir, rel)) = stack.pop() {
        let Ok(rd) = std::fs::read_dir(&dir) else { continue };
        let mut subdirs = Vec::new();
        for e in rd.flatten() {
            let name = e.file_name().to_string_lossy().into_owned();
            let Ok(t) = e.file_type() else { continue };
            let path = if rel.is_empty() { name.clone() } else { format!("{rel}/{name}") };
            if t.is_dir() {
                if !SKIP_DIRS.contains(&name.as_str()) {
                    subdirs.push((e.path(), path));
                }
            } else if t.is_file() || (t.is_symlink() && e.path().is_file()) {
                if matches!(name.as_str(), ".DS_Store" | "Thumbs.db" | "desktop.ini") {
                    continue;
                }
                if out.len() == max {
                    out.sort_by(|a: &String, b| super::folder_tree::natural(a, b));
                    return (out, true);
                }
                out.push(path);
            }
        }
        stack.extend(subdirs);
    }
    out.sort_by(|a, b| super::folder_tree::natural(a, b));
    (out, false)
}

/// `main.rs:42` → ("main.rs", Some(42)).
pub fn split_query(q: &str) -> (&str, Option<u64>) {
    let q = q.trim();
    match q.rsplit_once(':') {
        Some((name, n)) if !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()) => (name.trim_end(), n.parse().ok()),
        _ => (q, None),
    }
}

fn boundary(prev: Option<char>, c: char) -> bool {
    match prev {
        None => true,
        Some(p) => matches!(p, '/' | '\\' | '_' | '-' | '.' | ' ') || (p.is_lowercase() && c.is_uppercase()),
    }
}

/// How well `query` matches `path` (higher is better), with the indexes of the matched
/// characters; `None` when the query's characters do not all occur in order. Matches in the file
/// name, at the start of a word and next to each other count more; shorter paths win ties.
pub fn fuzzy(query: &str, path: &str) -> Option<(i32, Vec<usize>)> {
    let q: Vec<char> = query.chars().filter(|c| !c.is_whitespace()).flat_map(char::to_lowercase).collect();
    let p: Vec<char> = path.chars().collect();
    let name_at = path.rfind(['/', '\\']).map_or(0, |i| path[..=i].chars().count());
    if q.is_empty() {
        return Some((-(p.len() as i32), Vec::new()));
    }
    if !subsequence(&q, path) {
        return None;
    }
    let lower: Vec<char> = p.iter().map(|c| c.to_lowercase().next().unwrap_or(*c)).collect();
    // The whole query in the file name, else from the start of the path; at each step a word's
    // start is preferred to the next plain occurrence when there is one before the next match.
    let attempt = |from: usize| -> Option<Vec<usize>> {
        let mut pos = Vec::with_capacity(q.len());
        let mut i = from;
        for (k, &qc) in q.iter().enumerate() {
            let next = (i..p.len()).find(|&j| lower[j] == qc)?;
            // A later occurrence at a word start, if the rest still fits after it.
            let better = (next + 1..p.len()).find(|&j| lower[j] == qc && boundary(j.checked_sub(1).map(|b| p[b]), p[j]));
            let pick = match better {
                Some(b) if !boundary(next.checked_sub(1).map(|x| p[x]), p[next]) && pos.last().is_none_or(|&l: &usize| l + 1 != next) && fits(&lower, &q[k + 1..], b + 1) => b,
                _ => next,
            };
            pos.push(pick);
            i = pick + 1;
        }
        Some(pos)
    };
    let pos = attempt(name_at).or_else(|| attempt(0))?;
    let mut score = 0i32;
    for (k, &j) in pos.iter().enumerate() {
        score += 1;
        if boundary(j.checked_sub(1).map(|b| p[b]), p[j]) {
            score += 8;
        }
        if k > 0 && pos[k - 1] + 1 == j {
            score += 5;
        }
        if j >= name_at {
            score += 3;
        }
    }
    let name: String = lower[name_at..].iter().collect();
    let ql: String = q.iter().collect();
    if name.starts_with(&ql) {
        score += 15;
    }
    score -= (p.len() as i32) / 8;
    Some((score, pos))
}

/// True if the (lower-case) characters `q` occur in `path` in order, ignoring case.
fn subsequence(q: &[char], path: &str) -> bool {
    let mut k = 0;
    for c in path.chars() {
        if k == q.len() {
            break;
        }
        if c == q[k] || c.to_lowercase().next() == Some(q[k]) {
            k += 1;
        }
    }
    k == q.len()
}

fn fits(lower: &[char], rest: &[char], from: usize) -> bool {
    let mut i = from;
    for &c in rest {
        match (i..lower.len()).find(|&j| lower[j] == c) {
            Some(j) => i = j + 1,
            None => return false,
        }
    }
    true
}

/// The best `n` of `candidates` for `query`: (index, score, matched characters).
pub fn best(query: &str, candidates: &[String], n: usize) -> Vec<(usize, Vec<usize>)> {
    let mut v: Vec<(usize, i32, Vec<usize>)> = candidates.iter().enumerate().filter_map(|(i, c)| fuzzy(query, c).map(|(s, p)| (i, s, p))).collect();
    let by = |a: &(usize, i32, Vec<usize>), b: &(usize, i32, Vec<usize>)| b.1.cmp(&a.1).then_with(|| super::folder_tree::natural(&candidates[a.0], &candidates[b.0]));
    if v.len() > n {
        v.select_nth_unstable_by(n, by);
        v.truncate(n);
    }
    v.sort_by(by);
    v.into_iter().map(|(i, _, p)| (i, p)).collect()
}

impl BigTextApp {
    /// Search → Go to file.
    pub(crate) fn open_quick_open(&mut self) {
        if let Some(root) = self.folder.root.clone() {
            self.file_index.refresh(&root, self.waker.clone(), false);
        }
        self.quick_open = Some(QuickOpen { focus: true, ..Default::default() });
    }

    /// With no folder, Go to file chooses from the open tabs and recent files (full paths).
    fn recent_candidates(&self) -> Vec<String> {
        let mut v: Vec<String> = Vec::new();
        for p in self.docs.iter().filter_map(|d| d.path.as_deref()).chain(self.recent.iter().map(PathBuf::as_path)) {
            let s = bigtext::file::display_path(p);
            if !v.contains(&s) {
                v.push(s);
            }
        }
        v
    }

    pub(super) fn quick_open_window(&mut self, ctx: &egui::Context) {
        self.file_index.poll();
        let Some(q) = &mut self.quick_open else { return };
        let (up, down, enter, esc) = ctx.input_mut(|i| {
            (
                i.consume_key(Modifiers::NONE, Key::ArrowUp),
                i.consume_key(Modifiers::NONE, Key::ArrowDown),
                i.consume_key(Modifiers::NONE, Key::Enter),
                i.consume_key(Modifiers::NONE, Key::Escape),
            )
        });
        let (name, mut line) = split_query(&q.query);
        let name = name.to_string();
        let root = self.folder.root.clone().filter(|r| self.file_index.root.as_deref() == Some(r.as_path()));
        let recent;
        let (candidates, generation): (&[String], u64) = if root.is_some() {
            (&self.file_index.files, self.file_index.generation)
        } else {
            recent = self.recent_candidates();
            (&recent, u64::MAX - recent.len() as u64)
        };
        let q = self.quick_open.as_mut().unwrap();
        // Found again only when the name or the listing changes (a folder can hold 200,000 files).
        if q.found_for.as_ref() != Some(&(name.clone(), generation)) {
            q.found = best(&name, candidates, SHOWN);
            q.found_for = Some((name.clone(), generation));
        }
        let found = q.found.clone();
        q.selected = q.selected.min(found.len().saturating_sub(1));
        if up {
            q.selected = q.selected.saturating_sub(1);
        }
        if down && q.selected + 1 < found.len() {
            q.selected += 1;
        }
        let mut chosen = None;
        let listing = self.file_index.listing();
        let truncated = self.file_index.truncated && root.is_some();
        let no_folder = self.folder.root.is_none();
        egui::Window::new("quick-open")
            .title_bar(false)
            .anchor(egui::Align2::CENTER_TOP, egui::vec2(0.0, 60.0))
            .fixed_size(egui::vec2(600.0, 0.0))
            .show(ctx, |ui| {
                ui.horizontal(|ui| {
                    let r = ui.add(egui::TextEdit::singleline(&mut q.query).hint_text(tr("Go to file (name:line)")).desired_width(ui.available_width() - if listing { 24.0 } else { 0.0 }));
                    if q.focus {
                        r.request_focus();
                        q.focus = false;
                    }
                    if r.changed() {
                        q.selected = 0;
                    }
                    if listing {
                        ui.spinner().on_hover_text(tr("Reading…"));
                    }
                });
                ui.separator();
                let normal = ui.visuals().text_color();
                let weak = ui.visuals().weak_text_color();
                let hit = super::theme::info_text(ui);
                let font = egui::TextStyle::Body.resolve(ui.style());
                egui::ScrollArea::vertical().max_height(400.0).show(ui, |ui| {
                    for (k, (i, pos)) in found.iter().enumerate() {
                        let path = &candidates[*i];
                        let name_at = path.rfind(['/', '\\']).map_or(0, |n| n + 1);
                        let mut job = LayoutJob::default();
                        let char_at: Vec<usize> = path.char_indices().map(|(b, _)| b).collect();
                        let push = |job: &mut LayoutJob, from: usize, to: usize, color: egui::Color32| {
                            for (ci, &b) in char_at.iter().enumerate() {
                                if b < from || b >= to {
                                    continue;
                                }
                                let e = char_at.get(ci + 1).copied().unwrap_or(path.len());
                                let c = if pos.contains(&ci) { hit } else { color };
                                job.append(&path[b..e], 0.0, TextFormat { font_id: font.clone(), color: c, ..Default::default() });
                            }
                        };
                        push(&mut job, name_at, path.len(), normal);
                        if name_at > 0 {
                            job.append("   ", 0.0, TextFormat { font_id: font.clone(), color: weak, ..Default::default() });
                            push(&mut job, 0, name_at.saturating_sub(1), weak);
                        }
                        let r = ui.add(egui::Button::selectable(k == q.selected, job).min_size(egui::vec2(ui.available_width(), 0.0)));
                        if k == q.selected {
                            r.scroll_to_me(None);
                        }
                        if r.clicked() {
                            chosen = Some(*i);
                        }
                    }
                    if found.is_empty() && !listing {
                        ui.weak(if candidates.is_empty() && no_folder { tr("Open a folder (Ctrl+Shift+O) to find its files here.") } else { tr("No file matches.") });
                    }
                });
                if truncated {
                    ui.weak(trf("Only the first {n} files of the folder are listed.", &[("n", &super::filter_panel::fmt_count(MAX_FILES as u64))]));
                }
            });
        // Enter takes the text typed in this frame too.
        if enter && chosen.is_none() {
            let (name, l) = split_query(&q.query);
            if q.found_for.as_ref().map(|f| f.0.as_str()) != Some(name) {
                q.found = best(name, candidates, SHOWN);
                q.found_for = Some((name.to_string(), generation));
                q.selected = 0;
            }
            chosen = q.found.get(q.selected).map(|f| f.0);
            line = l;
        }
        if let Some(i) = chosen {
            let rel = candidates[i].clone();
            let path = match &root {
                Some(r) => r.join(rel),
                None => PathBuf::from(rel),
            };
            self.quick_open = None;
            self.open_path_at(path, line);
        } else if esc {
            self.quick_open = None;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn fuzzy_prefers_names_and_word_starts() {
        let files: Vec<String> = ["src/app/mod.rs", "src/app/folder_tree.rs", "src/view/text_view.rs", "docs/index.html", "src/app/find_in_files.rs", "README.md"].iter().map(|s| s.to_string()).collect();
        let top = |q: &str| best(q, &files, 3).first().map(|(i, _)| files[*i].clone());
        assert_eq!(top("ftree").as_deref(), Some("src/app/folder_tree.rs"));
        assert_eq!(top("tv").as_deref(), Some("src/view/text_view.rs"));
        assert_eq!(top("readme").as_deref(), Some("README.md"));
        assert_eq!(top("fif").as_deref(), Some("src/app/find_in_files.rs"));
        assert!(fuzzy("xyz", "src/app/mod.rs").is_none());
        let (_, pos) = fuzzy("mod", "src/app/mod.rs").unwrap();
        assert_eq!(pos, vec![8, 9, 10]);
        assert_eq!(best("", &files, 2).len(), 2);
    }

    #[test]
    fn line_suffix_and_listing() {
        assert_eq!(split_query("main.rs:42"), ("main.rs", Some(42)));
        assert_eq!(split_query("C:\\x"), ("C:\\x", None));
        assert_eq!(split_query(" a : "), ("a :", None));
        let dir = std::env::temp_dir().join(format!("bigtext-quick-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("src/sub")).unwrap();
        std::fs::create_dir_all(dir.join("node_modules/x")).unwrap();
        for f in ["a.txt", "src/b.rs", "src/sub/c.rs", "node_modules/x/d.js"] {
            std::fs::write(dir.join(f), "x").unwrap();
        }
        assert_eq!(walk(&dir, 10), (vec!["a.txt".to_string(), "src/b.rs".into(), "src/sub/c.rs".into()], false));
        assert!(walk(&dir, 2).1);
        let _ = std::fs::remove_dir_all(&dir);
    }
}