From e75b25f21e0e181a73f7dabdbc5c3d51bbbbd9bc Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 16 Oct 2022 23:42:12 +0200 Subject: [PATCH 1/2] Add command to add word boundaries to search --- helix-term/src/commands.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d5fc1ad934eb..47e92c7babb6 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -247,6 +247,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", + make_search_word_bounded, "Modify current search to make it word bounded", 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", @@ -1791,6 +1792,29 @@ fn search_selection(cx: &mut Context) { cx.editor.set_status(msg); } +fn make_search_word_bounded(cx: &mut Context) { + let regex = match cx.editor.registers.last('/') { + Some(regex) => regex, + None => return, + }; + let mut new_regex = String::new(); + let mut modified = false; + if !regex.starts_with("\\b") { + new_regex.push_str("\\b"); + modified = true; + } + new_regex.push_str(regex); + if !regex.ends_with("\\b") { + new_regex.push_str("\\b"); + modified = true; + } + if modified { + let msg = format!("register '{}' set to '{}'", '/', &new_regex); + cx.editor.registers.get_mut('/').push(new_regex); + cx.editor.set_status(msg); + } +} + fn global_search(cx: &mut Context) { #[derive(Debug)] struct FileResult { From 0de90f8d1f05b3467ee54826587538048585afb9 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 8 Nov 2022 20:11:33 +0100 Subject: [PATCH 2/2] Calculate string capacity before building --- helix-term/src/commands.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 47e92c7babb6..c3ceaa2de2de 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1797,22 +1797,28 @@ fn make_search_word_bounded(cx: &mut Context) { Some(regex) => regex, None => return, }; - let mut new_regex = String::new(); - let mut modified = false; - if !regex.starts_with("\\b") { + let start_anchored = regex.starts_with("\\b"); + let end_anchored = regex.ends_with("\\b"); + + if start_anchored && end_anchored { + return; + } + + let mut new_regex = String::with_capacity( + regex.len() + if start_anchored { 0 } else { 2 } + if end_anchored { 0 } else { 2 }, + ); + + if !start_anchored { new_regex.push_str("\\b"); - modified = true; } new_regex.push_str(regex); - if !regex.ends_with("\\b") { + if !end_anchored { new_regex.push_str("\\b"); - modified = true; - } - if modified { - let msg = format!("register '{}' set to '{}'", '/', &new_regex); - cx.editor.registers.get_mut('/').push(new_regex); - cx.editor.set_status(msg); } + + let msg = format!("register '{}' set to '{}'", '/', &new_regex); + cx.editor.registers.get_mut('/').push(new_regex); + cx.editor.set_status(msg); } fn global_search(cx: &mut Context) {