From 27a4605fe3667ac1d298976c85c23c4a6cbdfff5 Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Sun, 24 Jul 2022 14:00:05 +0300 Subject: [PATCH 1/4] Show status msg when next/prev cycles around --- helix-term/src/commands.rs | 39 +++++++++++++++++++++++--------------- helix-term/src/ui/mod.rs | 21 +++++++++++--------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index bd1bd8945029..54bb99ddb871 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1504,7 +1504,8 @@ fn select_regex(cx: &mut Context) { "select:".into(), Some(reg), ui::completers::none, - move |view, doc, regex, event| { + move |editor, regex, event| { + let (view, doc) = current!(editor); if !matches!(event, PromptEvent::Update | PromptEvent::Validate) { return; } @@ -1525,7 +1526,8 @@ fn split_selection(cx: &mut Context) { "split:".into(), Some(reg), ui::completers::none, - move |view, doc, regex, event| { + move |editor, regex, event| { + let (view, doc) = current!(editor); if !matches!(event, PromptEvent::Update | PromptEvent::Validate) { return; } @@ -1547,10 +1549,9 @@ fn split_selection_on_newline(cx: &mut Context) { doc.set_selection(view.id, selection); } -#[allow(clippy::too_many_arguments)] +// #[allow(clippy::too_many_arguments)] fn search_impl( - doc: &mut Document, - view: &mut View, + editor: &mut Editor, contents: &str, regex: &Regex, movement: Movement, @@ -1558,6 +1559,7 @@ fn search_impl( scrolloff: usize, wrap_around: bool, ) { + let (view, doc) = current!(editor); let text = doc.text().slice(..); let selection = doc.selection(view.id); @@ -1594,10 +1596,18 @@ fn search_impl( offset = start; regex.find_iter(&contents[start..]).last() } - } - // TODO: message on wraparound + }; + let msg = match direction { + Direction::Forward => "Cycled to beginning of file", + Direction::Backward => "Cycled to end of file", + }; + editor.set_status(msg); } + let (view, doc) = current!(editor); + let text = doc.text().slice(..); + let selection = doc.selection(view.id); + if let Some(mat) = mat { let start = text.byte_to_char(mat.start() + offset); let end = text.byte_to_char(mat.end() + offset); @@ -1673,13 +1683,12 @@ fn searcher(cx: &mut Context, direction: Direction) { .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone()))) .collect() }, - move |view, doc, regex, event| { + move |editor, regex, event| { if !matches!(event, PromptEvent::Update | PromptEvent::Validate) { return; } search_impl( - doc, - view, + editor, &contents, ®ex, Movement::Move, @@ -1695,7 +1704,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir let count = cx.count(); let config = cx.editor.config(); let scrolloff = config.scrolloff; - let (view, doc) = current!(cx.editor); + let (_, doc) = current!(cx.editor); let registers = &cx.editor.registers; if let Some(query) = registers.read('/').and_then(|query| query.last()) { let contents = doc.text().slice(..).to_string(); @@ -1713,8 +1722,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir { for _ in 0..count { search_impl( - doc, - view, + cx.editor, &contents, ®ex, movement, @@ -1810,7 +1818,7 @@ fn global_search(cx: &mut Context) { .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone()))) .collect() }, - move |_view, _doc, regex, event| { + move |_editor, regex, event| { if event != PromptEvent::Validate { return; } @@ -3732,7 +3740,8 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) { if remove { "remove:" } else { "keep:" }.into(), Some(reg), ui::completers::none, - move |view, doc, regex, event| { + move |editor, regex, event| { + let (view, doc) = current!(editor); if !matches!(event, PromptEvent::Update | PromptEvent::Validate) { return; } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 257608f00dac..02140b8ac285 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -24,7 +24,7 @@ pub use text::Text; use helix_core::regex::Regex; use helix_core::regex::RegexBuilder; -use helix_view::{Document, Editor, View}; +use helix_view::Editor; use std::path::PathBuf; @@ -66,7 +66,7 @@ pub fn regex_prompt( prompt: std::borrow::Cow<'static, str>, history_register: Option, completion_fn: impl FnMut(&Editor, &str) -> Vec + 'static, - fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static, + fun: impl Fn(&mut Editor, Regex, PromptEvent) + 'static, ) { let (view, doc) = current!(cx.editor); let doc_id = view.doc; @@ -103,18 +103,21 @@ pub fn regex_prompt( .build() { Ok(regex) => { - let (view, doc) = current!(cx.editor); + { + let (view, doc) = current!(cx.editor); - // revert state to what it was before the last update - doc.set_selection(view.id, snapshot.clone()); + // revert state to what it was before the last update + doc.set_selection(view.id, snapshot.clone()); - if event == PromptEvent::Validate { - // Equivalent to push_jump to store selection just before jump - view.jumps.push((doc_id, snapshot.clone())); + if event == PromptEvent::Validate { + // Equivalent to push_jump to store selection just before jump + view.jumps.push((doc_id, snapshot.clone())); + } } - fun(view, doc, regex, event); + fun(cx.editor, regex, event); + let (view, doc) = current!(cx.editor); view.ensure_cursor_in_view(doc, config.scrolloff); } Err(_err) => (), // TODO: mark command line as error From 6ebc3c140622e9070460908e58c6cd41e272a6a9 Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Sun, 24 Jul 2022 14:06:44 +0300 Subject: [PATCH 2/4] Add msg when there is no wraparound --- helix-term/src/commands.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 54bb99ddb871..d8e68e00cef0 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1589,19 +1589,23 @@ fn search_impl( Direction::Backward => regex.find_iter(&contents[..start]).last(), }; - if wrap_around && mat.is_none() { - mat = match direction { - Direction::Forward => regex.find(contents), - Direction::Backward => { - offset = start; - regex.find_iter(&contents[start..]).last() - } - }; - let msg = match direction { - Direction::Forward => "Cycled to beginning of file", - Direction::Backward => "Cycled to end of file", - }; - editor.set_status(msg); + if mat.is_none() { + if wrap_around { + mat = match direction { + Direction::Forward => regex.find(contents), + Direction::Backward => { + offset = start; + regex.find_iter(&contents[start..]).last() + } + }; + let msg = match direction { + Direction::Forward => "Cycled to beginning of file", + Direction::Backward => "Cycled to end of file", + }; + editor.set_status(msg); + } else { + editor.set_error("No more matches"); + } } let (view, doc) = current!(editor); From 03fa959ee82d8f92751f19600434eac513762915 Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Sun, 24 Jul 2022 14:27:03 +0300 Subject: [PATCH 3/4] Cleanup code --- helix-term/src/commands.rs | 1 - helix-term/src/ui/mod.rs | 14 ++++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d8e68e00cef0..24edc2549c67 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1549,7 +1549,6 @@ fn split_selection_on_newline(cx: &mut Context) { doc.set_selection(view.id, selection); } -// #[allow(clippy::too_many_arguments)] fn search_impl( editor: &mut Editor, contents: &str, diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 02140b8ac285..ada629048ef2 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -103,16 +103,14 @@ pub fn regex_prompt( .build() { Ok(regex) => { - { - let (view, doc) = current!(cx.editor); + let (view, doc) = current!(cx.editor); - // revert state to what it was before the last update - doc.set_selection(view.id, snapshot.clone()); + // revert state to what it was before the last update + doc.set_selection(view.id, snapshot.clone()); - if event == PromptEvent::Validate { - // Equivalent to push_jump to store selection just before jump - view.jumps.push((doc_id, snapshot.clone())); - } + if event == PromptEvent::Validate { + // Equivalent to push_jump to store selection just before jump + view.jumps.push((doc_id, snapshot.clone())); } fun(cx.editor, regex, event); From c53f4da75abdd1632c71f9541abbf01c939d87ca Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Tue, 26 Jul 2022 11:39:57 +0300 Subject: [PATCH 4/4] Change msg to "Wrapped around document" --- helix-term/src/commands.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 24edc2549c67..effeaa40496a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1597,11 +1597,7 @@ fn search_impl( regex.find_iter(&contents[start..]).last() } }; - let msg = match direction { - Direction::Forward => "Cycled to beginning of file", - Direction::Backward => "Cycled to end of file", - }; - editor.set_status(msg); + editor.set_status("Wrapped around document"); } else { editor.set_error("No more matches"); }