Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added search_selection_bounded to search with word boundaries #4222

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl MappableCommand {
extend_search_next, "Add next search match to selection",
extend_search_prev, "Add previous search match to selection",
search_selection, "Use current selection as search pattern",
search_selection_bounded, "Use current selection as search pattern with word boundaries",
global_search, "Global search in workspace folder",
extend_line, "Select current line, if already selected, extend to another line based on the anchor",
extend_line_below, "Select current line, if already selected, extend to next line",
Expand Down Expand Up @@ -1772,14 +1773,21 @@ fn extend_search_prev(cx: &mut Context) {
search_next_or_prev_impl(cx, Movement::Extend, Direction::Backward);
}

fn search_selection(cx: &mut Context) {
fn search_selection_impl(cx: &mut Context, word_bounds: bool) {
let (view, doc) = current!(cx.editor);
let contents = doc.text().slice(..);

let regex = doc
.selection(view.id)
.iter()
.map(|selection| regex::escape(&selection.fragment(contents)))
.map(|selection| {
let escaped = regex::escape(&selection.fragment(contents));
if word_bounds {
format!("\\b{}\\b", escaped)
} else {
escaped
}
})
.collect::<HashSet<_>>() // Collect into hashset to deduplicate identical regexes
.into_iter()
.collect::<Vec<_>>()
Expand All @@ -1790,6 +1798,14 @@ fn search_selection(cx: &mut Context) {
cx.editor.set_status(msg);
}

fn search_selection(cx: &mut Context) {
search_selection_impl(cx, false)
}

fn search_selection_bounded(cx: &mut Context) {
search_selection_impl(cx, true)
}

fn global_search(cx: &mut Context) {
#[derive(Debug)]
struct FileResult {
Expand Down