on master help sign in
Initial implementation of global search (#651)

* initial implementation of global search * use tokio::sync::mpsc::unbounded_channel instead of Arc, Mutex, Waker poll_fn * use tokio_stream::wrappers::UnboundedReceiverStream to collect all search matches * regex_prompt: unified callback; refactor * global search doc

commit9456d5c1a258e71bbb7e391dec8c3efb819e2d7d authorLeoi Hung Kin <leoihungkin@outlook.com> dateWed 22 Sep 2021, 00:03 (5 years ago) parenta512f48
6 files changed+259−25
Cargo.lock+74 book/src/keymap.md+3−1 helix-term/Cargo.toml+5 helix-term/src/commands.rs+166−22 helix-term/src/keymap.rs+1 helix-term/src/ui/mod.rs+10−2
Cargo.lock+74
@@ -41,9 +41,17 @@ version = "0.2.16"
4141source = "registry+https://github.com/rust-lang/crates.io-index"
4242checksum = "90682c8d613ad3373e66de8c6411e0ae2ab2571e879d2efbf73558cc66f21279"
4343dependencies = [
44+ "lazy_static",
4445 "memchr",
46+ "regex-automata",
4547]
4648
49+[[package]]
50+name = "bytecount"
51+version = "0.6.2"
52+source = "registry+https://github.com/rust-lang/crates.io-index"
53+checksum = "72feb31ffc86498dacdbd0fcebb56138e7177a8cc5cea4516031d15ae85a742e"
54+
4755[[package]]
4856name = "bytes"
4957version = "1.0.1"
@@ -174,6 +182,15 @@ dependencies = [
174182 "cfg-if",
175183]
176184
185+[[package]]
186+name = "encoding_rs_io"
187+version = "0.1.7"
188+source = "registry+https://github.com/rust-lang/crates.io-index"
189+checksum = "1cc3c5651fb62ab8aa3103998dade57efdd028544bd300516baa31840c252a83"
190+dependencies = [
191+ "encoding_rs",
192+]
193+
177194[[package]]
178195name = "error-code"
179196version = "2.3.0"
@@ -300,6 +317,45 @@ dependencies = [
300317 "regex",
301318]
302319
320+[[package]]
321+name = "grep-matcher"
322+version = "0.1.5"
323+source = "registry+https://github.com/rust-lang/crates.io-index"
324+checksum = "6d27563c33062cd33003b166ade2bb4fd82db1fd6a86db764dfdad132d46c1cc"
325+dependencies = [
326+ "memchr",
327+]
328+
329+[[package]]
330+name = "grep-regex"
331+version = "0.1.9"
332+source = "registry+https://github.com/rust-lang/crates.io-index"
333+checksum = "121553c9768c363839b92fc2d7cdbbad44a3b70e8d6e7b1b72b05c977527bd06"
334+dependencies = [
335+ "aho-corasick",
336+ "bstr",
337+ "grep-matcher",
338+ "log",
339+ "regex",
340+ "regex-syntax",
341+ "thread_local",
342+]
343+
344+[[package]]
345+name = "grep-searcher"
346+version = "0.1.8"
347+source = "registry+https://github.com/rust-lang/crates.io-index"
348+checksum = "7fbdbde90ba52adc240d2deef7b6ad1f99f53142d074b771fe9b7bede6c4c23d"
349+dependencies = [
350+ "bstr",
351+ "bytecount",
352+ "encoding_rs",
353+ "encoding_rs_io",
354+ "grep-matcher",
355+ "log",
356+ "memmap2",
357+]
358+
303359[[package]]
304360name = "helix-core"
305361version = "0.4.1"
@@ -361,6 +417,8 @@ dependencies = [
361417 "fern",
362418 "futures-util",
363419 "fuzzy-matcher",
420+ "grep-regex",
421+ "grep-searcher",
364422 "helix-core",
365423 "helix-lsp",
366424 "helix-tui",
@@ -375,6 +433,7 @@ dependencies = [
375433 "signal-hook",
376434 "signal-hook-tokio",
377435 "tokio",
436+ "tokio-stream",
378437 "toml",
379438]
380439
@@ -552,6 +611,15 @@ version = "2.4.1"
552611source = "registry+https://github.com/rust-lang/crates.io-index"
553612checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
554613
614+[[package]]
615+name = "memmap2"
616+version = "0.3.1"
617+source = "registry+https://github.com/rust-lang/crates.io-index"
618+checksum = "00b6c2ebff6180198788f5db08d7ce3bc1d0b617176678831a7510825973e357"
619+dependencies = [
620+ "libc",
621+]
622+
555623[[package]]
556624name = "mio"
557625version = "0.7.13"
@@ -753,6 +821,12 @@ dependencies = [
753821 "regex-syntax",
754822]
755823
824+[[package]]
825+name = "regex-automata"
826+version = "0.1.10"
827+source = "registry+https://github.com/rust-lang/crates.io-index"
828+checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
829+
756830[[package]]
757831name = "regex-syntax"
758832version = "0.6.25"
book/src/keymap.md+3−1
@@ -206,8 +206,10 @@ This layer is a kludge of mappings, mostly pickers.
206206| `y` | Join and yank selections to clipboard | `yank_joined_to_clipboard` |
207207| `Y` | Yank main selection to clipboard | `yank_main_selection_to_clipboard` |
208208| `R` | Replace selections by clipboard contents | `replace_selections_with_clipboard` |
209+| `/` | Global search in workspace folder | `global_search` |
209210
210−
211+> NOTE: Global search display results in a fuzzy picker, use `space + '` to bring it back up after opening a file.
212+
211213#### Unimpaired
212214
213215Mappings in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaired).
helix-term/Cargo.toml+5
@@ -55,5 +55,10 @@ toml = "0.5"
5555serde_json = "1.0"
5656serde = { version = "1.0", features = ["derive"] }
5757
58+# ripgrep for global search
59+grep-regex = "0.1.9"
60+grep-searcher = "0.1.8"
61+tokio-stream = "0.1.7"
62+
5863[target.'cfg(not(windows))'.dependencies] # https://github.com/vorner/signal-hook/issues/100
5964signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }
helix-term/src/commands.rs+166−22
@@ -31,7 +31,7 @@ use crate::{
3131};
3232
3333use crate::job::{self, Job, Jobs};
34−use futures_util::FutureExt;
34+use futures_util::{FutureExt, StreamExt};
3535use std::num::NonZeroUsize;
3636use std::{fmt, future::Future};
3737
@@ -43,6 +43,11 @@ use std::{
4343use once_cell::sync::Lazy;
4444use serde::de::{self, Deserialize, Deserializer};
4545
46+use grep_regex::RegexMatcher;
47+use grep_searcher::{sinks, BinaryDetection, SearcherBuilder};
48+use ignore::{DirEntry, WalkBuilder, WalkState};
49+use tokio_stream::wrappers::UnboundedReceiverStream;
50+
4651pub struct Context<'a> {
4752 pub register: Option<char>,
4853 pub count: Option<NonZeroUsize>,
@@ -209,6 +214,7 @@ impl Command {
209214 search_next, "Select next search match",
210215 extend_search_next, "Add next search match to selection",
211216 search_selection, "Use current selection as search pattern",
217+ global_search, "Global Search in workspace folder",
212218 extend_line, "Select current line, if already selected, extend to next line",
213219 extend_to_line_bounds, "Extend selection to line bounds (line-wise selection)",
214220 delete_selection, "Delete selection",
@@ -1061,24 +1067,41 @@ fn select_all(cx: &mut Context) {
10611067
10621068fn select_regex(cx: &mut Context) {
10631069 let reg = cx.register.unwrap_or('/');
1064− let prompt = ui::regex_prompt(cx, "select:".into(), Some(reg), move |view, doc, regex| {
1065− let text = doc.text().slice(..);
1066− if let Some(selection) = selection::select_on_matches(text, doc.selection(view.id), &regex)
1067− {
1068− doc.set_selection(view.id, selection);
1069− }
1070− });
1070+ let prompt = ui::regex_prompt(
1071+ cx,
1072+ "select:".into(),
1073+ Some(reg),
1074+ move |view, doc, regex, event| {
1075+ if event != PromptEvent::Update {
1076+ return;
1077+ }
1078+ let text = doc.text().slice(..);
1079+ if let Some(selection) =
1080+ selection::select_on_matches(text, doc.selection(view.id), &regex)
1081+ {
1082+ doc.set_selection(view.id, selection);
1083+ }
1084+ },
1085+ );
10711086
10721087 cx.push_layer(Box::new(prompt));
10731088}
10741089
10751090fn split_selection(cx: &mut Context) {
10761091 let reg = cx.register.unwrap_or('/');
1077− let prompt = ui::regex_prompt(cx, "split:".into(), Some(reg), move |view, doc, regex| {
1078− let text = doc.text().slice(..);
1079− let selection = selection::split_on_matches(text, doc.selection(view.id), &regex);
1080− doc.set_selection(view.id, selection);
1081− });
1092+ let prompt = ui::regex_prompt(
1093+ cx,
1094+ "split:".into(),
1095+ Some(reg),
1096+ move |view, doc, regex, event| {
1097+ if event != PromptEvent::Update {
1098+ return;
1099+ }
1100+ let text = doc.text().slice(..);
1101+ let selection = selection::split_on_matches(text, doc.selection(view.id), &regex);
1102+ doc.set_selection(view.id, selection);
1103+ },
1104+ );
10821105
10831106 cx.push_layer(Box::new(prompt));
10841107}
@@ -1141,9 +1164,17 @@ fn search(cx: &mut Context) {
11411164 // feed chunks into the regex yet
11421165 let contents = doc.text().slice(..).to_string();
11431166
1144− let prompt = ui::regex_prompt(cx, "search:".into(), Some(reg), move |view, doc, regex| {
1145− search_impl(doc, view, &contents, &regex, false);
1146− });
1167+ let prompt = ui::regex_prompt(
1168+ cx,
1169+ "search:".into(),
1170+ Some(reg),
1171+ move |view, doc, regex, event| {
1172+ if event != PromptEvent::Update {
1173+ return;
1174+ }
1175+ search_impl(doc, view, &contents, &regex, false);
1176+ },
1177+ );
11471178
11481179 cx.push_layer(Box::new(prompt));
11491180}
@@ -1192,6 +1223,111 @@ fn search_selection(cx: &mut Context) {
11921223 cx.editor.set_status(msg);
11931224}
11941225
1226+fn global_search(cx: &mut Context) {
1227+ let (all_matches_sx, all_matches_rx) =
1228+ tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
1229+ let prompt = ui::regex_prompt(
1230+ cx,
1231+ "global search:".into(),
1232+ None,
1233+ move |_view, _doc, regex, event| {
1234+ if event != PromptEvent::Validate {
1235+ return;
1236+ }
1237+ if let Ok(matcher) = RegexMatcher::new_line_matcher(regex.as_str()) {
1238+ let searcher = SearcherBuilder::new()
1239+ .binary_detection(BinaryDetection::quit(b'\x00'))
1240+ .build();
1241+
1242+ let search_root = std::env::current_dir()
1243+ .expect("Global search error: Failed to get current dir");
1244+ WalkBuilder::new(search_root).build_parallel().run(|| {
1245+ let mut searcher_cl = searcher.clone();
1246+ let matcher_cl = matcher.clone();
1247+ let all_matches_sx_cl = all_matches_sx.clone();
1248+ Box::new(move |dent: Result<DirEntry, ignore::Error>| -> WalkState {
1249+ let dent = match dent {
1250+ Ok(dent) => dent,
1251+ Err(_) => return WalkState::Continue,
1252+ };
1253+
1254+ match dent.file_type() {
1255+ Some(fi) => {
1256+ if !fi.is_file() {
1257+ return WalkState::Continue;
1258+ }
1259+ }
1260+ None => return WalkState::Continue,
1261+ }
1262+
1263+ let result_sink = sinks::UTF8(|line_num, _| {
1264+ match all_matches_sx_cl
1265+ .send((line_num as usize - 1, dent.path().to_path_buf()))
1266+ {
1267+ Ok(_) => Ok(true),
1268+ Err(_) => Ok(false),
1269+ }
1270+ });
1271+ let result = searcher_cl.search_path(&matcher_cl, dent.path(), result_sink);
1272+
1273+ if let Err(err) = result {
1274+ log::error!("Global search error: {}, {}", dent.path().display(), err);
1275+ }
1276+ WalkState::Continue
1277+ })
1278+ });
1279+ } else {
1280+ // Otherwise do nothing
1281+ // log::warn!("Global Search Invalid Pattern")
1282+ }
1283+ },
1284+ );
1285+
1286+ cx.push_layer(Box::new(prompt));
1287+
1288+ let show_picker = async move {
1289+ let all_matches: Vec<(usize, PathBuf)> =
1290+ UnboundedReceiverStream::new(all_matches_rx).collect().await;
1291+ let call: job::Callback =
1292+ Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
1293+ if all_matches.is_empty() {
1294+ editor.set_status("No matches found".to_string());
1295+ return;
1296+ }
1297+ let picker = FilePicker::new(
1298+ all_matches,
1299+ move |(_line_num, path)| path.to_str().unwrap().into(),
1300+ move |editor: &mut Editor, (line_num, path), action| {
1301+ match editor.open(path.into(), action) {
1302+ Ok(_) => {}
1303+ Err(e) => {
1304+ editor.set_error(format!(
1305+ "Failed to open file '{}': {}",
1306+ path.display(),
1307+ e
1308+ ));
1309+ return;
1310+ }
1311+ }
1312+
1313+ let line_num = *line_num;
1314+ let (view, doc) = current!(editor);
1315+ let text = doc.text();
1316+ let start = text.line_to_char(line_num);
1317+ let end = text.line_to_char((line_num + 1).min(text.len_lines()));
1318+
1319+ doc.set_selection(view.id, Selection::single(start, end));
1320+ align_view(doc, view, Align::Center);
1321+ },
1322+ |_editor, (line_num, path)| Some((path.clone(), Some((*line_num, *line_num)))),
1323+ );
1324+ compositor.push(Box::new(picker));
1325+ });
1326+ Ok(call)
1327+ };
1328+ cx.jobs.callback(show_picker);
1329+}
1330+
11951331fn extend_line(cx: &mut Context) {
11961332 let count = cx.count();
11971333 let (view, doc) = current!(cx.editor);
@@ -3847,13 +3983,21 @@ fn join_selections(cx: &mut Context) {
38473983fn keep_selections(cx: &mut Context) {
38483984 // keep selections matching regex
38493985 let reg = cx.register.unwrap_or('/');
3850− let prompt = ui::regex_prompt(cx, "keep:".into(), Some(reg), move |view, doc, regex| {
3851− let text = doc.text().slice(..);
3986+ let prompt = ui::regex_prompt(
3987+ cx,
3988+ "keep:".into(),
3989+ Some(reg),
3990+ move |view, doc, regex, event| {
3991+ if event != PromptEvent::Update {
3992+ return;
3993+ }
3994+ let text = doc.text().slice(..);
38523995
3853− if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), &regex) {
3854− doc.set_selection(view.id, selection);
3855− }
3856− });
3996+ if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), &regex) {
3997+ doc.set_selection(view.id, selection);
3998+ }
3999+ },
4000+ );
38574001
38584002 cx.push_layer(Box::new(prompt));
38594003}
helix-term/src/keymap.rs+1
@@ -555,6 +555,7 @@ impl Default for Keymaps {
555555 "P" => paste_clipboard_before,
556556 "R" => replace_selections_with_clipboard,
557557 "space" => keep_primary_selection,
558+ "/" => global_search,
558559 },
559560 "z" => { "View"
560561 "z" | "c" => align_view_center,
helix-term/src/ui/mod.rs+10−2
@@ -29,7 +29,7 @@ pub fn regex_prompt(
2929 cx: &mut crate::commands::Context,
3030 prompt: std::borrow::Cow<'static, str>,
3131 history_register: Option<char>,
32− fun: impl Fn(&mut View, &mut Document, Regex) + 'static,
32+ fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
3333) -> Prompt {
3434 let (view, doc) = current!(cx.editor);
3535 let view_id = view.id;
@@ -47,6 +47,14 @@ pub fn regex_prompt(
4747 }
4848 PromptEvent::Validate => {
4949 // TODO: push_jump to store selection just before jump
50+
51+ match Regex::new(input) {
52+ Ok(regex) => {
53+ let (view, doc) = current!(cx.editor);
54+ fun(view, doc, regex, event);
55+ }
56+ Err(_err) => (), // TODO: mark command line as error
57+ }
5058 }
5159 PromptEvent::Update => {
5260 // skip empty input, TODO: trigger default
@@ -70,7 +78,7 @@ pub fn regex_prompt(
7078 // revert state to what it was before the last update
7179 doc.set_selection(view.id, snapshot.clone());
7280
73− fun(view, doc, regex);
81+ fun(view, doc, regex, event);
7482
7583 view.ensure_cursor_in_view(doc, cx.editor.config.scrolloff);
7684 }