Refactor global_search as a dynamic Picker

pull/9647/head
Michael Davis 9 months ago
parent 5622db6798
commit 1d023b05ac
No known key found for this signature in database

@ -3,6 +3,7 @@ pub(crate) mod lsp;
pub(crate) mod typed;
pub use dap::*;
use futures_util::FutureExt;
use helix_event::status;
use helix_stdx::{
path::expand_tilde,
@ -2255,93 +2256,81 @@ fn global_search(cx: &mut Context) {
}
}
struct GlobalSearchConfig {
smart_case: bool,
file_picker_config: helix_view::editor::FilePickerConfig,
}
let config = cx.editor.config();
let smart_case = config.search.smart_case;
let file_picker_config = config.file_picker.clone();
let config = GlobalSearchConfig {
smart_case: config.search.smart_case,
file_picker_config: config.file_picker.clone(),
};
let reg = cx.register.unwrap_or('/');
let completions = search_completions(cx, Some(reg));
ui::raw_regex_prompt(
cx,
"global-search:".into(),
Some(reg),
move |_editor: &Editor, input: &str| {
completions
.iter()
.filter(|comp| comp.starts_with(input))
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
.collect()
},
move |cx, _, input, event| {
if event != PromptEvent::Validate {
return;
let columns = vec![
PickerColumn::new("path", |item: &FileResult, _| {
let path = helix_stdx::path::get_relative_path(&item.path);
format!("{}:{}", path.to_string_lossy(), item.line_num + 1).into()
}),
PickerColumn::new("contents", |item: &FileResult, _| {
item.line_content.as_str().into()
})
.without_filtering(),
];
let get_files = |query: &str,
editor: &mut Editor,
config: std::sync::Arc<GlobalSearchConfig>,
injector: &ui::picker::Injector<_, _>| {
if query.is_empty() {
return async { Ok(()) }.boxed();
}
cx.editor.registers.last_search_register = reg;
let current_path = doc_mut!(cx.editor).path().cloned();
let documents: Vec<_> = cx
.editor
let search_root = helix_stdx::env::current_working_dir();
if !search_root.exists() {
return async { Err(anyhow::anyhow!("Current working directory does not exist")) }
.boxed();
}
let documents: Vec<_> = editor
.documents()
.map(|doc| (doc.path().cloned(), doc.text().to_owned()))
.collect();
if let Ok(matcher) = RegexMatcherBuilder::new()
.case_smart(smart_case)
.build(input)
let matcher = match RegexMatcherBuilder::new()
.case_smart(config.smart_case)
.build(query)
{
let search_root = helix_stdx::env::current_working_dir();
if !search_root.exists() {
cx.editor
.set_error("Current working directory does not exist");
return;
Ok(matcher) => {
// Clear any "Failed to compile regex" errors out of the statusline.
editor.clear_status();
matcher
}
let columns = vec![
PickerColumn::new(
"path",
|item: &FileResult, current_path: &Option<PathBuf>| {
let relative_path = helix_stdx::path::get_relative_path(&item.path)
.to_string_lossy()
.into_owned();
if current_path
.as_ref()
.map(|p| p == &item.path)
.unwrap_or(false)
{
format!("{} (*)", relative_path).into()
} else {
relative_path.into()
Err(err) => {
log::info!("Failed to compile search pattern in global search: {}", err);
return async { Err(anyhow::anyhow!("Failed to compile regex")) }.boxed();
}
},
),
PickerColumn::new("contents", |item: &FileResult, _| {
item.line_content.as_str().into()
}),
];
let (picker, injector) = Picker::stream(columns, current_path);
};
let dedup_symlinks = file_picker_config.deduplicate_links;
let dedup_symlinks = config.file_picker_config.deduplicate_links;
let absolute_root = search_root
.canonicalize()
.unwrap_or_else(|_| search_root.clone());
let injector_ = injector.clone();
std::thread::spawn(move || {
let injector = injector.clone();
async move {
let searcher = SearcherBuilder::new()
.binary_detection(BinaryDetection::quit(b'\x00'))
.build();
let mut walk_builder = WalkBuilder::new(search_root);
walk_builder
.hidden(file_picker_config.hidden)
.parents(file_picker_config.parents)
.ignore(file_picker_config.ignore)
.follow_links(file_picker_config.follow_symlinks)
.git_ignore(file_picker_config.git_ignore)
.git_global(file_picker_config.git_global)
.git_exclude(file_picker_config.git_exclude)
.max_depth(file_picker_config.max_depth)
WalkBuilder::new(search_root)
.hidden(config.file_picker_config.hidden)
.parents(config.file_picker_config.parents)
.ignore(config.file_picker_config.ignore)
.follow_links(config.file_picker_config.follow_symlinks)
.git_ignore(config.file_picker_config.git_ignore)
.git_global(config.file_picker_config.git_global)
.git_exclude(config.file_picker_config.git_exclude)
.max_depth(config.file_picker_config.max_depth)
.filter_entry(move |entry| {
filter_picker_entry(entry, &absolute_root, dedup_symlinks)
})
@ -2351,7 +2340,7 @@ fn global_search(cx: &mut Context) {
.run(|| {
let mut searcher = searcher.clone();
let matcher = matcher.clone();
let injector = injector_.clone();
let injector = injector.clone();
let documents = &documents;
Box::new(move |entry: Result<DirEntry, ignore::Error>| -> WalkState {
let entry = match entry {
@ -2404,11 +2393,7 @@ fn global_search(cx: &mut Context) {
};
if let Err(err) = result {
log::error!(
"Global search error: {}, {}",
entry.path().display(),
err
);
log::error!("Global search error: {}, {}", entry.path().display(), err);
}
if stop {
WalkState::Quit
@ -2417,23 +2402,22 @@ fn global_search(cx: &mut Context) {
}
})
});
});
Ok(())
}
.boxed()
};
cx.jobs.callback(async move {
let call = move |_: &mut Editor, compositor: &mut Compositor| {
let picker = Picker::with_stream(
picker,
0,
injector,
let mut picker = Picker::new(
columns,
1, // contents
vec![],
config,
move |cx, FileResult { path, line_num, .. }, action| {
let doc = match cx.editor.open(path, action) {
Ok(id) => doc_mut!(cx.editor, &id),
Err(e) => {
cx.editor.set_error(format!(
"Failed to open file '{}': {}",
path.display(),
e
));
cx.editor
.set_error(format!("Failed to open file '{}': {}", path.display(), e));
return;
}
};
@ -2456,21 +2440,20 @@ fn global_search(cx: &mut Context) {
}
},
)
.with_preview(
|_editor, FileResult { path, line_num, .. }| {
.with_preview(|_editor, FileResult { path, line_num, .. }| {
Some((path.clone().into(), Some((*line_num, *line_num))))
},
);
compositor.push(Box::new(overlaid(picker)))
};
Ok(Callback::EditorCompositor(Box::new(call)))
})
} else {
// Otherwise do nothing
// log::warn!("Global Search Invalid Pattern")
.with_dynamic_query(get_files, Some(275));
if let Some((reg, line)) = cx
.register
.and_then(|reg| Some((reg, cx.editor.registers.first(reg, cx.editor)?)))
{
picker = picker.with_line(line.into_owned(), cx.editor);
cx.editor.registers.last_search_register = reg;
}
},
);
cx.push_layer(Box::new(overlaid(picker)));
}
enum Extend {

Loading…
Cancel
Save