on master help sign in
commands: Implement select_on_matches.
commit2bea5db7bdb3ad3fa029df830d824cd5c26a153f authorBlaž Hrastnik <blaz@mxxn.io> dateFri 22 Jan 2021, 17:13 (6 years ago) parenta702af0
4 files changed+98−41
helix-core/src/selection.rs+29 helix-term/src/commands.rs+18−32 helix-term/src/keymap.rs+10−9 helix-term/src/ui/mod.rs+41
helix-core/src/selection.rs+29
@@ -258,6 +258,35 @@ impl Selection {
258258
259259// TODO: checkSelection -> check if valid for doc length
260260
261+pub fn select_on_matches(
262+ text: &RopeSlice,
263+ selections: &Selection,
264+ regex: &crate::regex::Regex,
265+) -> Selection {
266+ let mut result = SmallVec::with_capacity(selections.ranges().len());
267+
268+ for sel in selections.ranges() {
269+ // TODO: can't avoid occasional allocations since Regex can't operate on chunks yet
270+ let fragment = sel.fragment(&text);
271+
272+ let mut sel_start = sel.from();
273+ let sel_end = sel.to();
274+
275+ let mut start_byte = text.char_to_byte(sel_start);
276+
277+ for mat in regex.find_iter(&fragment) {
278+ // TODO: retain range direction
279+
280+ let start = text.byte_to_char(start_byte + mat.start());
281+ let end = text.byte_to_char(start_byte + mat.end());
282+ result.push(Range::new(start, end - 1));
283+ }
284+ }
285+
286+ // TODO: figure out a new primary index
287+ Selection::new(result, 0)
288+}
289+
261290// TODO: support to split on capture #N instead of whole match
262291pub fn split_on_matches(
263292 text: &RopeSlice,
helix-term/src/commands.rs+18−32
@@ -282,6 +282,19 @@ pub fn extend_line_down(cx: &mut Context) {
282282 doc.set_selection(selection);
283283}
284284
285+pub fn select_regex(cx: &mut Context) {
286+ let prompt = ui::regex_prompt(cx, "select:".to_string(), |doc, regex| {
287+ let text = &doc.text().slice(..);
288+ let selection = selection::select_on_matches(text, doc.selection(), &regex);
289+ doc.set_selection(selection);
290+ });
291+ cx.callback = Some(Box::new(
292+ move |compositor: &mut Compositor, editor: &mut Editor| {
293+ compositor.push(Box::new(prompt));
294+ },
295+ ));
296+}
297+
285298pub fn split_selection(cx: &mut Context) {
286299 // TODO: this needs to store initial selection state, revert on esc, confirm on enter
287300 // needs to also call the callback function per input change, not just final time.
@@ -298,38 +311,11 @@ pub fn split_selection(cx: &mut Context) {
298311
299312 let snapshot = cx.doc().state.clone();
300313
301− let prompt = Prompt::new(
302− "split:".to_string(),
303− |input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
304− move |editor: &mut Editor, input: &str, event: PromptEvent| {
305− match event {
306− PromptEvent::Abort => {
307− // revert state
308− let doc = &mut editor.view_mut().doc;
309− doc.state = snapshot.clone();
310− }
311− PromptEvent::Validate => {
312− //
313− }
314− PromptEvent::Update => {
315− match Regex::new(input) {
316− Ok(regex) => {
317− let doc = &mut editor.view_mut().doc;
318−
319− // revert state to what it was before the last update
320− doc.state = snapshot.clone();
321−
322− let text = &doc.text().slice(..);
323− let selection =
324− selection::split_on_matches(text, doc.selection(), &regex);
325− doc.set_selection(selection);
326− }
327− Err(_err) => (), // TODO: mark command line as error
328− }
329− }
330− }
331− },
332− );
314+ let prompt = ui::regex_prompt(cx, "split:".to_string(), |doc, regex| {
315+ let text = &doc.text().slice(..);
316+ let selection = selection::split_on_matches(text, doc.selection(), &regex);
317+ doc.set_selection(selection);
318+ });
333319
334320 cx.callback = Some(Box::new(
335321 move |compositor: &mut Compositor, editor: &mut Editor| {
helix-term/src/keymap.rs+10−9
@@ -121,14 +121,14 @@ macro_rules! ctrl {
121121 };
122122}
123123
124−// macro_rules! alt {
125−// ($ch:expr) => {
126−// Key {
127−// code: KeyCode::Char($ch),
128−// modifiers: Modifiers::ALT,
129−// }
130−// };
131−// }
124+macro_rules! alt {
125+ ($ch:expr) => {
126+ Key {
127+ code: KeyCode::Char($ch),
128+ modifiers: Modifiers::ALT,
129+ }
130+ };
131+}
132132
133133pub fn default() -> Keymaps {
134134 hashmap!(
@@ -159,7 +159,8 @@ pub fn default() -> Keymaps {
159159 vec![key!('o')] => commands::open_below,
160160 vec![key!('d')] => commands::delete_selection,
161161 vec![key!('c')] => commands::change_selection,
162− vec![key!('s')] => commands::split_selection_on_newline,
162+ vec![key!('s')] => commands::select_regex,
163+ vec![alt!('s')] => commands::split_selection_on_newline,
163164 vec![shift!('S')] => commands::split_selection,
164165 vec![key!(';')] => commands::collapse_selection,
165166 // TODO should be alt(;)
helix-term/src/ui/mod.rs+41
@@ -9,12 +9,53 @@ pub use prompt::{Prompt, PromptEvent};
99pub use tui::layout::Rect;
1010pub use tui::style::{Color, Modifier, Style};
1111
12+use helix_core::regex::Regex;
13+use helix_view::{Document, Editor};
14+
1215// TODO: temp
1316#[inline(always)]
1417pub fn text_color() -> Style {
1518 Style::default().fg(Color::Rgb(219, 191, 239)) // lilac
1619}
1720
21+pub fn regex_prompt(
22+ cx: &mut crate::commands::Context,
23+ prompt: String,
24+ fun: impl Fn(&mut Document, Regex) + 'static,
25+) -> Prompt {
26+ let snapshot = cx.doc().state.clone();
27+
28+ Prompt::new(
29+ prompt,
30+ |input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
31+ move |editor: &mut Editor, input: &str, event: PromptEvent| {
32+ match event {
33+ PromptEvent::Abort => {
34+ // revert state
35+ let doc = &mut editor.view_mut().doc;
36+ doc.state = snapshot.clone();
37+ }
38+ PromptEvent::Validate => {
39+ //
40+ }
41+ PromptEvent::Update => {
42+ match Regex::new(input) {
43+ Ok(regex) => {
44+ let doc = &mut editor.view_mut().doc;
45+
46+ // revert state to what it was before the last update
47+ doc.state = snapshot.clone();
48+
49+ fun(doc, regex);
50+ }
51+ Err(_err) => (), // TODO: mark command line as error
52+ }
53+ }
54+ }
55+ },
56+ )
57+}
58+
1859use std::path::{Path, PathBuf};
1960pub fn file_picker(root: &str, ex: &'static smol::Executor) -> Picker<PathBuf> {
2061 use ignore::Walk;