Skip to content

Commit f3958aa

Browse files
authored
Cycled to end/beginning + no more matches msgs (#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 2fac9e2 commit f3958aa

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
@@ -1520,7 +1520,8 @@ fn select_regex(cx: &mut Context) {
15201520
"select:".into(),
15211521
Some(reg),
15221522
ui::completers::none,
1523-
move |view, doc, regex, event| {
1523+
move |editor, regex, event| {
1524+
let (view, doc) = current!(editor);
15241525
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
15251526
return;
15261527
}
@@ -1541,7 +1542,8 @@ fn split_selection(cx: &mut Context) {
15411542
"split:".into(),
15421543
Some(reg),
15431544
ui::completers::none,
1544-
move |view, doc, regex, event| {
1545+
move |editor, regex, event| {
1546+
let (view, doc) = current!(editor);
15451547
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
15461548
return;
15471549
}
@@ -1563,17 +1565,16 @@ fn split_selection_on_newline(cx: &mut Context) {
15631565
doc.set_selection(view.id, selection);
15641566
}
15651567

1566-
#[allow(clippy::too_many_arguments)]
15671568
fn search_impl(
1568-
doc: &mut Document,
1569-
view: &mut View,
1569+
editor: &mut Editor,
15701570
contents: &str,
15711571
regex: &Regex,
15721572
movement: Movement,
15731573
direction: Direction,
15741574
scrolloff: usize,
15751575
wrap_around: bool,
15761576
) {
1577+
let (view, doc) = current!(editor);
15771578
let text = doc.text().slice(..);
15781579
let selection = doc.selection(view.id);
15791580

@@ -1603,17 +1604,25 @@ fn search_impl(
16031604
Direction::Backward => regex.find_iter(&contents[..start]).last(),
16041605
};
16051606

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

1622+
let (view, doc) = current!(editor);
1623+
let text = doc.text().slice(..);
1624+
let selection = doc.selection(view.id);
1625+
16171626
if let Some(mat) = mat {
16181627
let start = text.byte_to_char(mat.start() + offset);
16191628
let end = text.byte_to_char(mat.end() + offset);
@@ -1689,13 +1698,12 @@ fn searcher(cx: &mut Context, direction: Direction) {
16891698
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
16901699
.collect()
16911700
},
1692-
move |view, doc, regex, event| {
1701+
move |editor, regex, event| {
16931702
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
16941703
return;
16951704
}
16961705
search_impl(
1697-
doc,
1698-
view,
1706+
editor,
16991707
&contents,
17001708
&regex,
17011709
Movement::Move,
@@ -1711,7 +1719,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
17111719
let count = cx.count();
17121720
let config = cx.editor.config();
17131721
let scrolloff = config.scrolloff;
1714-
let (view, doc) = current!(cx.editor);
1722+
let (_, doc) = current!(cx.editor);
17151723
let registers = &cx.editor.registers;
17161724
if let Some(query) = registers.read('/').and_then(|query| query.last()) {
17171725
let contents = doc.text().slice(..).to_string();
@@ -1729,8 +1737,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
17291737
{
17301738
for _ in 0..count {
17311739
search_impl(
1732-
doc,
1733-
view,
1740+
cx.editor,
17341741
&contents,
17351742
&regex,
17361743
movement,
@@ -1834,7 +1841,7 @@ fn global_search(cx: &mut Context) {
18341841
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
18351842
.collect()
18361843
},
1837-
move |_view, _doc, regex, event| {
1844+
move |_editor, regex, event| {
18381845
if event != PromptEvent::Validate {
18391846
return;
18401847
}
@@ -3773,7 +3780,8 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
37733780
if remove { "remove:" } else { "keep:" }.into(),
37743781
Some(reg),
37753782
ui::completers::none,
3776-
move |view, doc, regex, event| {
3783+
move |editor, regex, event| {
3784+
let (view, doc) = current!(editor);
37773785
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
37783786
return;
37793787
}

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)