Skip to content

Commit

Permalink
add history to search's completions
Browse files Browse the repository at this point in the history
  • Loading branch information
cossonleo committed Oct 26, 2021
1 parent 33c4df4 commit a40d7e6
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege
};
}

fn search_completions(cx: &mut Context) -> Vec<String> {
fn search_completions(cx: &mut Context, reg: Option<char>) -> Vec<String> {
let count = cx.count();
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
Expand All @@ -1169,11 +1169,36 @@ fn search_completions(cx: &mut Context) -> Vec<String> {
textobject::textobject_word(text, range, textobject::TextObject::Inside, count)
});

selection
let mut elem_has = std::collections::BTreeSet::new();
let mut has_elem = move |item| {
if elem_has.contains(item) {
false
} else {
elem_has.insert(item);
true
}
};
let mut completions = selection
.ranges()
.iter()
.map(|range| text.slice(range.from()..range.to()).to_string())
.collect::<Vec<String>>()
.filter_map(|range| text.slice(range.from()..range.to()).as_str())
.filter(|&item| has_elem(item))
.map(|item| item.to_string())
.collect::<Vec<String>>();
let empty = Vec::new();
if completions.len() < 10 {
if let Some(reg) = reg {
cx.editor
.registers
.get(reg)
.map_or(empty.as_slice(), |reg| reg.read())
.iter()
.filter(|item| has_elem(item.as_str()))
.take(10 - completions.len())
.for_each(|item| completions.push(item.clone()));
}
}
completions
}

// TODO: use one function for search vs extend
Expand All @@ -1186,7 +1211,7 @@ 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 completions = search_completions(cx, Some(reg));

let prompt = ui::regex_prompt(
cx,
Expand Down Expand Up @@ -1259,7 +1284,7 @@ fn global_search(cx: &mut Context) {
tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
let smart_case = cx.editor.config.smart_case;

let completions = search_completions(cx);
let completions = search_completions(cx, None);
let prompt = ui::regex_prompt(
cx,
"global search:".into(),
Expand Down

0 comments on commit a40d7e6

Please sign in to comment.