diff options
author | Armin Ronacher | 2022-11-15 14:16:27 +0000 |
---|---|---|
committer | GitHub | 2022-11-15 14:16:27 +0000 |
commit | 392a018aebe1716c94be94dbbf967b1698843218 (patch) | |
tree | b57e990374ea50b254b340d99bf01bd84e04d62a | |
parent | 77be98c78321cc2643f23f0b16889417ea9b0596 (diff) |
Add command to add word boundaries to search (#4322)
* Add command to add word boundaries to search
* Calculate string capacity before building
-rw-r--r-- | helix-term/src/commands.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index e5ca5611..e56c2b67 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -250,6 +250,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", @@ -1809,6 +1810,35 @@ 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 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"); + } + new_regex.push_str(regex); + if !end_anchored { + new_regex.push_str("\\b"); + } + + 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 { |