From 33c67f138835d4407e076ebcf2d29322fda44114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 22 Feb 2021 15:14:02 +0900 Subject: [PATCH] commands: add * as selection search. --- helix-core/src/selection.rs | 2 +- helix-term/src/commands.rs | 11 ++++++++++- helix-term/src/keymap.rs | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index d3ac045fc..edad81a77 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -120,7 +120,7 @@ pub fn extend(&self, from: usize, to: usize) -> Self { // groupAt #[inline] - pub fn fragment<'a>(&'a self, text: RopeSlice<'a>) -> Cow<'a, str> { + pub fn fragment<'a, 'b: 'a>(&'a self, text: RopeSlice<'b>) -> Cow<'b, str> { Cow::from(text.slice(self.from()..self.to() + 1)) } } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b6d8d818a..c048ff9a6 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,7 +1,7 @@ use helix_core::{ comment, graphemes, indent::TAB_WIDTH, - regex::Regex, + regex::{self, Regex}, register, selection, state::{Direction, Granularity, State}, ChangeSet, Range, Selection, Tendril, Transaction, @@ -412,6 +412,15 @@ pub fn search_next(cx: &mut Context) { } } +pub fn search_selection(cx: &mut Context) { + let doc = cx.doc(); + let contents = doc.text().slice(..); + let query = doc.selection().primary().fragment(contents); + let regex = regex::escape(&query); + register::set('\\', vec![regex]); + search_next(cx); +} + // TODO: N -> search_prev // need to loop around buffer also and show a message // same for no matches diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 124456c96..69c8e48ca 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -170,6 +170,7 @@ pub fn default() -> Keymaps { vec![key!('x')] => commands::select_line, vec![key!('/')] => commands::search, vec![key!('n')] => commands::search_next, + vec![key!('*')] => commands::search_selection, vec![key!('u')] => commands::undo, vec![shift!('U')] => commands::redo, vec![key!('y')] => commands::yank,