@@ -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), ®ex)
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), ®ex)
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), ®ex);
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), ®ex);
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, ®ex, 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, ®ex, 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), ®ex) {
3854− doc.set_selection(view.id, selection);
3855− }
3856− });
3996+ if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), ®ex) {
3997+ doc.set_selection(view.id, selection);
3998+ }
3999+ },
4000+ );
38574001
38584002 cx.push_layer(Box::new(prompt));
38594003}