Skip to content

Commit

Permalink
view scroll more smaller when search match
Browse files Browse the repository at this point in the history
  • Loading branch information
cossonleo committed Nov 2, 2021
1 parent 1720b98 commit 40574fb
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,14 @@ fn split_selection_on_newline(cx: &mut Context) {
doc.set_selection(view.id, selection);
}

fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Regex, extend: bool) {
fn search_impl(
doc: &mut Document,
view: &mut View,
contents: &str,
regex: &Regex,
extend: bool,
scrolloff: usize,
) {
let text = doc.text().slice(..);
let selection = doc.selection(view.id);

Expand Down Expand Up @@ -1168,13 +1175,15 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege
};

doc.set_selection(view.id, selection);
align_view(doc, view, Align::Center);
view.ensure_cursor_in_view(doc, scrolloff);
};
}

// TODO: use one function for search vs extend
fn search(cx: &mut Context) {
let reg = cx.register.unwrap_or('/');
let scrolloff = cx.editor.config.scrolloff;

let (_, doc) = current!(cx.editor);

// TODO: could probably share with select_on_matches?
Expand All @@ -1191,14 +1200,15 @@ fn search(cx: &mut Context) {
if event != PromptEvent::Update {
return;
}
search_impl(doc, view, &contents, &regex, false);
search_impl(doc, view, &contents, &regex, false, scrolloff);
},
);

cx.push_layer(Box::new(prompt));
}

fn search_next_impl(cx: &mut Context, extend: bool) {
let scrolloff = cx.editor.config.scrolloff;
let (view, doc) = current!(cx.editor);
let registers = &cx.editor.registers;
if let Some(query) = registers.read('/') {
Expand All @@ -1213,7 +1223,7 @@ fn search_next_impl(cx: &mut Context, extend: bool) {
.case_insensitive(case_insensitive)
.build()
{
search_impl(doc, view, &contents, &regex, extend);
search_impl(doc, view, &contents, &regex, extend, scrolloff);
} else {
// get around warning `mutable_borrow_reservation_conflict`
// which will be a hard error in the future
Expand Down

0 comments on commit 40574fb

Please sign in to comment.