Skip to content

Commit ec6fc7f

Browse files
A-Walruspathwave
authored andcommitted
Cycled to end/beginning + no more matches msgs (helix-editor#3176)
* Show status msg when next/prev cycles around * Add msg when there is no wraparound * Cleanup code * Change msg to "Wrapped around document"
1 parent b230f02 commit ec6fc7f

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

helix-term/src/commands.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,8 @@ fn select_regex(cx: &mut Context) {
15231523
"select:".into(),
15241524
Some(reg),
15251525
ui::completers::none,
1526-
move |view, doc, regex, event| {
1526+
move |editor, regex, event| {
1527+
let (view, doc) = current!(editor);
15271528
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
15281529
return;
15291530
}
@@ -1544,7 +1545,8 @@ fn split_selection(cx: &mut Context) {
15441545
"split:".into(),
15451546
Some(reg),
15461547
ui::completers::none,
1547-
move |view, doc, regex, event| {
1548+
move |editor, regex, event| {
1549+
let (view, doc) = current!(editor);
15481550
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
15491551
return;
15501552
}
@@ -1566,17 +1568,16 @@ fn split_selection_on_newline(cx: &mut Context) {
15661568
doc.set_selection(view.id, selection);
15671569
}
15681570

1569-
#[allow(clippy::too_many_arguments)]
15701571
fn search_impl(
1571-
doc: &mut Document,
1572-
view: &mut View,
1572+
editor: &mut Editor,
15731573
contents: &str,
15741574
regex: &Regex,
15751575
movement: Movement,
15761576
direction: Direction,
15771577
scrolloff: usize,
15781578
wrap_around: bool,
15791579
) {
1580+
let (view, doc) = current!(editor);
15801581
let text = doc.text().slice(..);
15811582
let selection = doc.selection(view.id);
15821583

@@ -1606,17 +1607,25 @@ fn search_impl(
16061607
Direction::Backward => regex.find_iter(&contents[..start]).last(),
16071608
};
16081609

1609-
if wrap_around && mat.is_none() {
1610-
mat = match direction {
1611-
Direction::Forward => regex.find(contents),
1612-
Direction::Backward => {
1613-
offset = start;
1614-
regex.find_iter(&contents[start..]).last()
1615-
}
1610+
if mat.is_none() {
1611+
if wrap_around {
1612+
mat = match direction {
1613+
Direction::Forward => regex.find(contents),
1614+
Direction::Backward => {
1615+
offset = start;
1616+
regex.find_iter(&contents[start..]).last()
1617+
}
1618+
};
1619+
editor.set_status("Wrapped around document");
1620+
} else {
1621+
editor.set_error("No more matches");
16161622
}
1617-
// TODO: message on wraparound
16181623
}
16191624

1625+
let (view, doc) = current!(editor);
1626+
let text = doc.text().slice(..);
1627+
let selection = doc.selection(view.id);
1628+
16201629
if let Some(mat) = mat {
16211630
let start = text.byte_to_char(mat.start() + offset);
16221631
let end = text.byte_to_char(mat.end() + offset);
@@ -1696,13 +1705,12 @@ fn searcher(cx: &mut Context, direction: Direction) {
16961705
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
16971706
.collect()
16981707
},
1699-
move |view, doc, regex, event| {
1708+
move |editor, regex, event| {
17001709
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
17011710
return;
17021711
}
17031712
search_impl(
1704-
doc,
1705-
view,
1713+
editor,
17061714
&contents,
17071715
&regex,
17081716
Movement::Move,
@@ -1718,7 +1726,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
17181726
let count = cx.count();
17191727
let config = cx.editor.config();
17201728
let scrolloff = config.scrolloff;
1721-
let (view, doc) = current!(cx.editor);
1729+
let (_, doc) = current!(cx.editor);
17221730
let registers = &cx.editor.registers;
17231731
if let Some(query) = registers.read('/').and_then(|query| query.last()) {
17241732
let contents = doc.text().slice(..).to_string();
@@ -1736,8 +1744,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
17361744
{
17371745
for _ in 0..count {
17381746
search_impl(
1739-
doc,
1740-
view,
1747+
cx.editor,
17411748
&contents,
17421749
&regex,
17431750
movement,
@@ -1841,7 +1848,7 @@ fn global_search(cx: &mut Context) {
18411848
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
18421849
.collect()
18431850
},
1844-
move |_view, _doc, regex, event| {
1851+
move |_editor, regex, event| {
18451852
if event != PromptEvent::Validate {
18461853
return;
18471854
}
@@ -3780,7 +3787,8 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
37803787
if remove { "remove:" } else { "keep:" }.into(),
37813788
Some(reg),
37823789
ui::completers::none,
3783-
move |view, doc, regex, event| {
3790+
move |editor, regex, event| {
3791+
let (view, doc) = current!(editor);
37843792
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
37853793
return;
37863794
}

helix-term/src/ui/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use text::Text;
2626

2727
use helix_core::regex::Regex;
2828
use helix_core::regex::RegexBuilder;
29-
use helix_view::{Document, Editor, View};
29+
use helix_view::Editor;
3030

3131
use std::path::PathBuf;
3232

@@ -61,7 +61,7 @@ pub fn regex_prompt(
6161
prompt: std::borrow::Cow<'static, str>,
6262
history_register: Option<char>,
6363
completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
64-
fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
64+
fun: impl Fn(&mut Editor, Regex, PromptEvent) + 'static,
6565
) {
6666
let (view, doc) = current!(cx.editor);
6767
let doc_id = view.doc;
@@ -108,8 +108,9 @@ pub fn regex_prompt(
108108
view.jumps.push((doc_id, snapshot.clone()));
109109
}
110110

111-
fun(view, doc, regex, event);
111+
fun(cx.editor, regex, event);
112112

113+
let (view, doc) = current!(cx.editor);
113114
view.ensure_cursor_in_view(doc, config.scrolloff);
114115
}
115116
Err(err) => {

0 commit comments

Comments
 (0)