on master help sign in
Center search results.
811f952Blaž Hrastnik
helix-term/src/commands.rs+24−23
@@ -574,12 +574,11 @@ pub fn select_all(cx: &mut Context) {
574}
574}
575
575
576pub fn select_regex(cx: &mut Context) {
576pub fn select_regex(cx: &mut Context) {
577− let view_id = cx.view_id;
578− let prompt = ui::regex_prompt(cx, "select:".to_string(), move |doc, regex| {
577+ let prompt = ui::regex_prompt(cx, "select:".to_string(), move |view, doc, regex| {
579 let text = doc.text().slice(..);
578 let text = doc.text().slice(..);
580− if let Some(selection) = selection::select_on_matches(text, doc.selection(view_id), &regex)
579+ if let Some(selection) = selection::select_on_matches(text, doc.selection(view.id), &regex)
581 {
580 {
582− doc.set_selection(view_id, selection);
581+ doc.set_selection(view.id, selection);
583 }
582 }
584 });
583 });
585
584
@@ -587,11 +586,10 @@ pub fn select_regex(cx: &mut Context) {
587}
586}
588
587
589pub fn split_selection(cx: &mut Context) {
588pub fn split_selection(cx: &mut Context) {
590− let view_id = cx.view_id;
591− let prompt = ui::regex_prompt(cx, "split:".to_string(), move |doc, regex| {
589+ let prompt = ui::regex_prompt(cx, "split:".to_string(), move |view, doc, regex| {
592 let text = doc.text().slice(..);
590 let text = doc.text().slice(..);
593− let selection = selection::split_on_matches(text, doc.selection(view_id), &regex);
591+ let selection = selection::split_on_matches(text, doc.selection(view.id), &regex);
594− doc.set_selection(view_id, selection);
592+ doc.set_selection(view.id, selection);
595 });
593 });
596
594
597 cx.push_layer(Box::new(prompt));
595 cx.push_layer(Box::new(prompt));
@@ -614,9 +612,9 @@ pub fn split_selection_on_newline(cx: &mut Context) {
614// I'd probably collect all the matches right now and store the current index. The cache needs
612// I'd probably collect all the matches right now and store the current index. The cache needs
615// wiping if input happens.
613// wiping if input happens.
616
614
617−fn _search(doc: &mut Document, view_id: ViewId, contents: &str, regex: &Regex, extend: bool) {
615+fn _search(doc: &mut Document, view: &mut View, contents: &str, regex: &Regex, extend: bool) {
618 let text = doc.text();
616 let text = doc.text();
619− let selection = doc.selection(view_id);
617+ let selection = doc.selection(view.id);
620 let start = selection.cursor();
618 let start = selection.cursor();
621
619
622 // use find_at to find the next match after the cursor, loop around the end
620 // use find_at to find the next match after the cursor, loop around the end
@@ -627,14 +625,19 @@ fn _search(doc: &mut Document, view_id: ViewId, contents: &str, regex: &Regex, e
627 let start = text.byte_to_char(mat.start());
625 let start = text.byte_to_char(mat.start());
628 let end = text.byte_to_char(mat.end());
626 let end = text.byte_to_char(mat.end());
629
627
628+ let head = end - 1;
629+
630 let selection = if extend {
630 let selection = if extend {
631− selection.clone().push(Range::new(start, end - 1))
631+ selection.clone().push(Range::new(start, head))
632 } else {
632 } else {
633− Selection::single(start, end - 1)
633+ Selection::single(start, head)
634 };
634 };
635
635
636 // TODO: (first_match, regex) stuff in register?
636 // TODO: (first_match, regex) stuff in register?
637− doc.set_selection(view_id, selection);
637+ doc.set_selection(view.id, selection);
638+ // TODO: extract this centering into a function to share with _goto?
639+ let line = doc.text().char_to_line(head);
640+ view.first_line = line.saturating_sub(view.area.height as usize / 2);
638 };
641 };
639}
642}
640
643
@@ -649,10 +652,10 @@ pub fn search(cx: &mut Context) {
649 let contents = doc.text().slice(..).to_string();
652 let contents = doc.text().slice(..).to_string();
650
653
651 let view_id = cx.view_id;
654 let view_id = cx.view_id;
652− let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
655+ let prompt = ui::regex_prompt(cx, "search:".to_string(), move |view, doc, regex| {
653 let text = doc.text();
656 let text = doc.text();
654− let start = doc.selection(view_id).cursor();
657+ let start = doc.selection(view.id).cursor();
655− _search(doc, view_id, &contents, &regex, false);
658+ _search(doc, view, &contents, &regex, false);
656
659
657 // TODO: only store on enter (accept), not update
660 // TODO: only store on enter (accept), not update
658 register::set('\\', vec![regex.as_str().to_string()]);
661 register::set('\\', vec![regex.as_str().to_string()]);
@@ -664,11 +667,10 @@ pub fn search(cx: &mut Context) {
664pub fn _search_next(cx: &mut Context, extend: bool) {
667pub fn _search_next(cx: &mut Context, extend: bool) {
665 if let Some(query) = register::get('\\') {
668 if let Some(query) = register::get('\\') {
666 let query = query.first().unwrap();
669 let query = query.first().unwrap();
667− let view_id = cx.view_id;
670+ let (view, doc) = cx.current();
668− let doc = cx.doc();
669 let contents = doc.text().slice(..).to_string();
671 let contents = doc.text().slice(..).to_string();
670 let regex = Regex::new(&query).unwrap();
672 let regex = Regex::new(&query).unwrap();
671− _search(doc, view_id, &contents, &regex, extend);
673+ _search(doc, view, &contents, &regex, extend);
672 }
674 }
673}
675}
674
676
@@ -1652,12 +1654,11 @@ pub fn join_selections(cx: &mut Context) {
1652
1654
1653pub fn keep_selections(cx: &mut Context) {
1655pub fn keep_selections(cx: &mut Context) {
1654 // keep selections matching regex
1656 // keep selections matching regex
1655− let view_id = cx.view_id;
1656− let prompt = ui::regex_prompt(cx, "keep:".to_string(), move |doc, regex| {
1657+ let prompt = ui::regex_prompt(cx, "keep:".to_string(), move |view, doc, regex| {
1657 let text = doc.text().slice(..);
1658 let text = doc.text().slice(..);
1658
1659
1659− if let Some(selection) = selection::keep_matches(text, doc.selection(view_id), &regex) {
1660+ if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), &regex) {
1660− doc.set_selection(view_id, selection);
1661+ doc.set_selection(view.id, selection);
1661 }
1662 }
1662 });
1663 });
1663
1664
helix-term/src/ui/mod.rs+3−3
@@ -20,7 +20,7 @@ pub use tui::layout::Rect;
20pub use tui::style::{Color, Modifier, Style};
20pub use tui::style::{Color, Modifier, Style};
21
21
22use helix_core::regex::Regex;
22use helix_core::regex::Regex;
23−use helix_view::{Document, Editor};
23+use helix_view::{View, Document, Editor};
24
24
25use std::path::{Path, PathBuf};
25use std::path::{Path, PathBuf};
26
26
@@ -33,7 +33,7 @@ pub fn text_color() -> Style {
33pub fn regex_prompt(
33pub fn regex_prompt(
34 cx: &mut crate::commands::Context,
34 cx: &mut crate::commands::Context,
35 prompt: String,
35 prompt: String,
36− fun: impl Fn(&mut Document, Regex) + 'static,
36+ fun: impl Fn(&mut View, &mut Document, Regex) + 'static,
37) -> Prompt {
37) -> Prompt {
38 let view_id = cx.view().id;
38 let view_id = cx.view().id;
39 let snapshot = cx.doc().selection(view_id).clone();
39 let snapshot = cx.doc().selection(view_id).clone();
@@ -65,7 +65,7 @@ pub fn regex_prompt(
65 // TODO: also revert text
65 // TODO: also revert text
66 doc.set_selection(view.id, snapshot.clone());
66 doc.set_selection(view.id, snapshot.clone());
67
67
68− fun(doc, regex);
68+ fun(view, doc, regex);
69
69
70 view.ensure_cursor_in_view(doc);
70 view.ensure_cursor_in_view(doc);
71 }
71 }