Runs Kate's syntax definitions directly, on a hand-written editor core. Spring-driven UI with a pop-out scrolling preview, baked-in terminal grown from a morphing button, jelly tabs, command palette, side panels, git, language servers and hot exit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
189 lines
9.3 KiB
Rust
189 lines
9.3 KiB
Rust
//! The find bubble: floats over the top of the editor and unfolds a replace row when asked.
|
|
|
|
use super::morph::{self, Morph};
|
|
use super::{fonts, widgets::*};
|
|
use crate::app::App;
|
|
use crate::motion::{self, spring};
|
|
use crate::text::{Doc, EditKind, Sel};
|
|
use eframe::egui::{self, pos2, vec2, Align2, Id, Key, Rect, Ui};
|
|
|
|
#[derive(Default)]
|
|
pub struct Find {
|
|
pub open: bool,
|
|
pub replace: bool,
|
|
pub query: String,
|
|
pub with: String,
|
|
pub case: bool,
|
|
pub word: bool,
|
|
pub regex: bool,
|
|
pub focus: bool,
|
|
/// Char ranges of every match, for the doc in `key.0`.
|
|
pub hits: Vec<(usize, usize)>,
|
|
pub cur: usize,
|
|
key: (u64, u64, String, u8),
|
|
pub error: bool,
|
|
}
|
|
|
|
const MAX_HITS: usize = 50_000;
|
|
|
|
impl Find {
|
|
pub fn refresh(&mut self, doc: &Doc) {
|
|
let flags = self.case as u8 | (self.word as u8) << 1 | (self.regex as u8) << 2;
|
|
let key = (doc.id, doc.buf.version, self.query.clone(), flags);
|
|
if key == self.key {
|
|
return;
|
|
}
|
|
self.key = key;
|
|
self.hits.clear();
|
|
self.error = false;
|
|
if self.query.is_empty() {
|
|
return;
|
|
}
|
|
let pat = if self.regex { self.query.clone() } else { regex::escape(&self.query) };
|
|
let pat = if self.word { format!(r"\b(?:{pat})\b") } else { pat };
|
|
let re = regex::RegexBuilder::new(&pat).case_insensitive(!self.case).multi_line(true).build();
|
|
let Ok(re) = re else { self.error = true; return };
|
|
let text = doc.buf.rope.to_string();
|
|
for m in re.find_iter(&text).take(MAX_HITS) {
|
|
if m.is_empty() { continue }
|
|
self.hits.push((doc.buf.rope.byte_to_char(m.start()), doc.buf.rope.byte_to_char(m.end())));
|
|
}
|
|
let head = doc.buf.head();
|
|
self.cur = self.hits.iter().position(|h| h.0 >= head).unwrap_or(0);
|
|
}
|
|
}
|
|
|
|
impl App {
|
|
pub fn find_hits_in(&self, doc_id: u64, doc: &Doc, rows: &[(usize, (usize, usize), usize)]) -> Vec<(usize, usize, bool)> {
|
|
if !self.find.open || self.find.key.0 != doc_id || rows.is_empty() {
|
|
return vec![];
|
|
}
|
|
let lines = doc.buf.lines();
|
|
let first = doc.buf.rope.line_to_char(rows[0].1.0.min(lines - 1));
|
|
let last_line = rows[rows.len() - 1].1.0.min(lines - 1);
|
|
let end = doc.buf.at(last_line, usize::MAX);
|
|
let a = self.find.hits.partition_point(|h| h.1 < first);
|
|
self.find.hits[a..].iter().enumerate().take_while(|(_, h)| h.0 <= end).map(|(i, h)| (h.0, h.1, a + i == self.find.cur)).collect()
|
|
}
|
|
|
|
fn find_refresh(&mut self) {
|
|
if let Some(d) = self.cur_doc_id().and_then(|i| self.docs.get(&i)) {
|
|
self.find.refresh(d);
|
|
}
|
|
}
|
|
|
|
pub fn find_step(&mut self, ctx: &egui::Context, back: bool) {
|
|
let n = self.find.hits.len();
|
|
if n == 0 {
|
|
return;
|
|
}
|
|
self.find.cur = if back { (self.find.cur + n - 1) % n } else { (self.find.cur + 1) % n };
|
|
self.find_show(ctx);
|
|
}
|
|
|
|
fn find_show(&mut self, ctx: &egui::Context) {
|
|
let Some(&(a, b)) = self.find.hits.get(self.find.cur) else { return };
|
|
if let Some(d) = self.cur_doc_mut() {
|
|
d.buf.set_sels(vec![Sel { anchor: a, head: b, want_col: None }]);
|
|
let line = d.buf.pos(a).0;
|
|
self.jump_to_line(ctx, line);
|
|
}
|
|
}
|
|
|
|
fn replace_one(&mut self, ctx: &egui::Context) {
|
|
let Some(&(a, b)) = self.find.hits.get(self.find.cur) else { return };
|
|
let with = self.find.with.clone();
|
|
if let Some(d) = self.cur_doc_mut() {
|
|
d.buf.apply(vec![(a..b, with)], EditKind::Other, |_, st, n| Sel::cursor(st + n));
|
|
}
|
|
self.find_refresh();
|
|
self.find_show(ctx);
|
|
}
|
|
|
|
fn replace_all(&mut self) {
|
|
let hits = self.find.hits.clone();
|
|
let with = self.find.with.clone();
|
|
let n = hits.len();
|
|
if let Some(d) = self.cur_doc_mut() {
|
|
let ch = hits.into_iter().map(|(a, b)| (a..b, with.clone())).collect();
|
|
d.buf.apply(ch, EditKind::Other, |_, st, n| Sel::cursor(st + n));
|
|
}
|
|
self.toast("find_replace", format!("Replaced {n} matches"));
|
|
}
|
|
|
|
pub fn find_bar(&mut self, ui: &mut Ui, card: Rect) {
|
|
let t = self.t.clone();
|
|
let k = spring("find/open", self.find.open as u8 as f32, motion::DEFAULT);
|
|
if k < 0.01 && !self.find.open {
|
|
return;
|
|
}
|
|
let rep = spring("find/rep", self.find.replace as u8 as f32, motion::DEFAULT).clamp(0.0, 1.2);
|
|
self.find_refresh();
|
|
let w = (card.width() - 150.0).clamp(300.0, 500.0);
|
|
let h = 52.0 + 46.0 * rep;
|
|
let target = Rect::from_min_size(pos2(card.right() - 108.0 - w, card.top() + 10.0), vec2(w, h));
|
|
let origin = Rect::from_center_size(pos2(target.right() - 26.0, target.top() + 26.0), vec2(44.0, 44.0));
|
|
let rect = lerp_rect(origin, target, k);
|
|
egui::Area::new(Id::unique("findbar")).order(egui::Order::Foreground).fixed_pos(rect.min).interactable(true).show(ui.ctx(), |ui| {
|
|
ui.set_clip_rect(rect.expand(30.0));
|
|
let p = ui.painter().clone();
|
|
shadow(&p, rect, 24.0, k.clamp(0.0, 1.0), t.dark);
|
|
p.rect_filled(rect, cr(24.0), t.high);
|
|
if k < 0.6 {
|
|
return;
|
|
}
|
|
let row = Rect::from_min_size(rect.min, vec2(rect.width(), 52.0));
|
|
if morph::button(ui, &t, Rect::from_center_size(pos2(rect.left() + 26.0, row.center().y), vec2(34.0, 34.0)), "find/more", Morph::Fold, self.find.replace, "Replace", t.high).clicked() {
|
|
self.find.replace = !self.find.replace;
|
|
}
|
|
let tools = 4.0 * 32.0 + 124.0;
|
|
let field = Rect::from_min_max(pos2(rect.left() + 48.0, row.top() + 9.0), pos2(rect.right() - tools, row.bottom() - 9.0));
|
|
let te = ui.put(field, egui::TextEdit::singleline(&mut self.find.query).frame(egui::Frame::NONE).hint_text("Find").font(fonts::ui(14.0)).align(Align2::LEFT_CENTER).text_color(if self.find.error { t.error } else { t.on_surface }));
|
|
if self.find.focus {
|
|
self.find.focus = false;
|
|
te.request_focus();
|
|
let mut st = egui::text_edit::TextEditState::load(ui.ctx(), te.id).unwrap_or_default();
|
|
let all = egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(self.find.query.chars().count()));
|
|
st.cursor.set_char_range(Some(all));
|
|
st.store(ui.ctx(), te.id);
|
|
}
|
|
if te.has_focus() || te.lost_focus() {
|
|
let (enter, shift) = ui.input(|i| (i.key_pressed(Key::Enter), i.modifiers.shift));
|
|
if enter {
|
|
self.find_step(ui.ctx(), shift);
|
|
te.request_focus();
|
|
}
|
|
}
|
|
let count = if self.find.query.is_empty() { String::new() } else if self.find.hits.is_empty() { "None".to_string() } else { format!("{}/{}", self.find.cur + 1, self.find.hits.len()) };
|
|
p.text(pos2(rect.right() - tools + 52.0, row.center().y), Align2::CENTER_CENTER, count, fonts::ui(12.0), t.on_variant);
|
|
let mut x = rect.right() - 142.0;
|
|
for (key, tip, label, on) in [("find/case", "Match case", "Aa", self.find.case), ("find/word", "Whole word", "ab", self.find.word), ("find/re", "Regular expression", ".*", self.find.regex)] {
|
|
let r = Rect::from_center_size(pos2(x, row.center().y), vec2(30.0, 30.0));
|
|
let resp = icon_btn(ui, &t, r, key, "", tip, on);
|
|
p.text(r.center(), Align2::CENTER_CENTER, label, fonts::med(12.0), if on { t.on_secondary_container } else { t.on_variant });
|
|
if resp.clicked() {
|
|
match key { "find/case" => self.find.case ^= true, "find/word" => self.find.word ^= true, _ => self.find.regex ^= true }
|
|
}
|
|
x += 30.0;
|
|
}
|
|
if icon_btn(ui, &t, Rect::from_center_size(pos2(x + 2.0, row.center().y), vec2(30.0, 30.0)), "find/next", "keyboard_arrow_down", "Next (Enter)", false).clicked() { self.find_step(ui.ctx(), false) }
|
|
if morph::button(ui, &t, Rect::from_center_size(pos2(x + 32.0, row.center().y), vec2(30.0, 30.0)), "find/close", Morph::SearchClose, self.find.open, "Close (Esc)", t.high).clicked() { self.find_close(ui.ctx()) }
|
|
if rep > 0.5 {
|
|
let y = row.bottom() - 2.0;
|
|
let f2 = Rect::from_min_max(pos2(rect.left() + 48.0, y + 8.0), pos2(rect.right() - 170.0, y + 34.0));
|
|
ui.put(f2, egui::TextEdit::singleline(&mut self.find.with).frame(egui::Frame::NONE).hint_text("Replace with").font(fonts::ui(14.0)).align(Align2::LEFT_CENTER).text_color(t.on_surface));
|
|
if pill(ui, &t, Rect::from_min_size(pos2(rect.right() - 164.0, y + 6.0), vec2(74.0, 30.0)), "find/r1", "", "Replace", Tone::Tonal).clicked() { self.replace_one(ui.ctx()) }
|
|
if pill(ui, &t, Rect::from_min_size(pos2(rect.right() - 86.0, y + 6.0), vec2(74.0, 30.0)), "find/ra", "", "All", Tone::Tonal).clicked() { self.replace_all() }
|
|
}
|
|
});
|
|
if self.find.open && ui.input(|i| i.key_pressed(Key::Escape)) {
|
|
self.find_close(ui.ctx());
|
|
}
|
|
}
|
|
|
|
pub fn find_close(&mut self, ctx: &egui::Context) {
|
|
self.find.open = false;
|
|
self.find.hits.clear();
|
|
self.focus_editor(ctx);
|
|
}
|
|
}
|