Skip to content

Commit

Permalink
add completions for search and global search
Browse files Browse the repository at this point in the history
  • Loading branch information
cossonleo committed Oct 12, 2021
1 parent 933db94 commit d1c0fff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
36 changes: 36 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,7 @@ fn select_regex(cx: &mut Context) {
cx,
"select:".into(),
Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| {
if event != PromptEvent::Update {
return;
Expand All @@ -1094,6 +1095,7 @@ fn split_selection(cx: &mut Context) {
cx,
"split:".into(),
Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| {
if event != PromptEvent::Update {
return;
Expand Down Expand Up @@ -1154,6 +1156,22 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege
};
}

fn search_completions(cx: &mut Context) -> Vec<String> {
let count = cx.count();
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);

let selection = doc.selection(view.id).clone().transform(|range| {
textobject::textobject_word(text, range, textobject::TextObject::Inside, count)
});

selection
.ranges()
.iter()
.map(|range| text.slice(range.from()..range.to()).to_string())
.collect::<Vec<String>>()
}

// TODO: use one function for search vs extend
fn search(cx: &mut Context) {
let reg = cx.register.unwrap_or('/');
Expand All @@ -1164,11 +1182,19 @@ fn search(cx: &mut Context) {
// HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
// feed chunks into the regex yet
let contents = doc.text().slice(..).to_string();
let completions = search_completions(cx);

let prompt = ui::regex_prompt(
cx,
"search:".into(),
Some(reg),
move |input: &str| {
completions
.iter()
.filter(|comp| comp.starts_with(input))
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
.collect()
},
move |view, doc, regex, event| {
if event != PromptEvent::Update {
return;
Expand Down Expand Up @@ -1228,10 +1254,19 @@ fn global_search(cx: &mut Context) {
let (all_matches_sx, all_matches_rx) =
tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
let smart_case = cx.editor.config.smart_case;

let completions = search_completions(cx);
let prompt = ui::regex_prompt(
cx,
"global search:".into(),
None,
move |input: &str| {
completions
.iter()
.filter(|comp| comp.starts_with(input))
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
.collect()
},
move |_view, _doc, regex, event| {
if event != PromptEvent::Validate {
return;
Expand Down Expand Up @@ -4013,6 +4048,7 @@ fn keep_selections(cx: &mut Context) {
cx,
"keep:".into(),
Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| {
if event != PromptEvent::Update {
return;
Expand Down
3 changes: 2 additions & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn regex_prompt(
cx: &mut crate::commands::Context,
prompt: std::borrow::Cow<'static, str>,
history_register: Option<char>,
completion_fn: impl FnMut(&str) -> Vec<prompt::Completion> + 'static,
fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
) -> Prompt {
let (view, doc) = current!(cx.editor);
Expand All @@ -38,7 +39,7 @@ pub fn regex_prompt(
Prompt::new(
prompt,
history_register,
|_input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
completion_fn,
move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
match event {
PromptEvent::Abort => {
Expand Down

0 comments on commit d1c0fff

Please sign in to comment.