mirror of https://github.com/helix-editor/helix
search_impl will only align cursor center when it isn't in view (#959)
parent
f96be0fcbc
commit
a69caff450
|
@ -1175,6 +1175,7 @@ fn search_impl(
|
||||||
regex: &Regex,
|
regex: &Regex,
|
||||||
movement: Movement,
|
movement: Movement,
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
|
scrolloff: usize,
|
||||||
) {
|
) {
|
||||||
let text = doc.text().slice(..);
|
let text = doc.text().slice(..);
|
||||||
let selection = doc.selection(view.id);
|
let selection = doc.selection(view.id);
|
||||||
|
@ -1233,7 +1234,11 @@ fn search_impl(
|
||||||
};
|
};
|
||||||
|
|
||||||
doc.set_selection(view.id, selection);
|
doc.set_selection(view.id, selection);
|
||||||
align_view(doc, view, Align::Center);
|
if view.is_cursor_in_view(doc, 0) {
|
||||||
|
view.ensure_cursor_in_view(doc, scrolloff);
|
||||||
|
} else {
|
||||||
|
align_view(doc, view, Align::Center)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1257,6 +1262,8 @@ fn rsearch(cx: &mut Context) {
|
||||||
// TODO: use one function for search vs extend
|
// TODO: use one function for search vs extend
|
||||||
fn searcher(cx: &mut Context, direction: Direction) {
|
fn searcher(cx: &mut Context, direction: Direction) {
|
||||||
let reg = cx.register.unwrap_or('/');
|
let reg = cx.register.unwrap_or('/');
|
||||||
|
let scrolloff = cx.editor.config.scrolloff;
|
||||||
|
|
||||||
let (_, doc) = current!(cx.editor);
|
let (_, doc) = current!(cx.editor);
|
||||||
|
|
||||||
// TODO: could probably share with select_on_matches?
|
// TODO: could probably share with select_on_matches?
|
||||||
|
@ -1281,7 +1288,15 @@ fn searcher(cx: &mut Context, direction: Direction) {
|
||||||
if event != PromptEvent::Update {
|
if event != PromptEvent::Update {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
search_impl(doc, view, &contents, ®ex, Movement::Move, direction);
|
search_impl(
|
||||||
|
doc,
|
||||||
|
view,
|
||||||
|
&contents,
|
||||||
|
®ex,
|
||||||
|
Movement::Move,
|
||||||
|
direction,
|
||||||
|
scrolloff,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1289,6 +1304,7 @@ fn searcher(cx: &mut Context, direction: Direction) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) {
|
fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) {
|
||||||
|
let scrolloff = cx.editor.config.scrolloff;
|
||||||
let (view, doc) = current!(cx.editor);
|
let (view, doc) = current!(cx.editor);
|
||||||
let registers = &cx.editor.registers;
|
let registers = &cx.editor.registers;
|
||||||
if let Some(query) = registers.read('/') {
|
if let Some(query) = registers.read('/') {
|
||||||
|
@ -1303,7 +1319,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
|
||||||
.case_insensitive(case_insensitive)
|
.case_insensitive(case_insensitive)
|
||||||
.build()
|
.build()
|
||||||
{
|
{
|
||||||
search_impl(doc, view, &contents, ®ex, movement, direction);
|
search_impl(doc, view, &contents, ®ex, movement, direction, scrolloff);
|
||||||
} else {
|
} else {
|
||||||
// get around warning `mutable_borrow_reservation_conflict`
|
// get around warning `mutable_borrow_reservation_conflict`
|
||||||
// which will be a hard error in the future
|
// which will be a hard error in the future
|
||||||
|
|
|
@ -85,7 +85,12 @@ impl View {
|
||||||
self.area.clip_left(OFFSET).clip_bottom(1) // -1 for statusline
|
self.area.clip_left(OFFSET).clip_bottom(1) // -1 for statusline
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ensure_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) {
|
//
|
||||||
|
pub fn offset_coords_to_in_view(
|
||||||
|
&self,
|
||||||
|
doc: &Document,
|
||||||
|
scrolloff: usize,
|
||||||
|
) -> Option<(usize, usize)> {
|
||||||
let cursor = doc
|
let cursor = doc
|
||||||
.selection(self.id)
|
.selection(self.id)
|
||||||
.primary()
|
.primary()
|
||||||
|
@ -104,23 +109,43 @@ impl View {
|
||||||
|
|
||||||
let last_col = self.offset.col + inner_area.width.saturating_sub(1) as usize;
|
let last_col = self.offset.col + inner_area.width.saturating_sub(1) as usize;
|
||||||
|
|
||||||
if line > last_line.saturating_sub(scrolloff) {
|
let row = if line > last_line.saturating_sub(scrolloff) {
|
||||||
// scroll down
|
// scroll down
|
||||||
self.offset.row += line - (last_line.saturating_sub(scrolloff));
|
self.offset.row + line - (last_line.saturating_sub(scrolloff))
|
||||||
} else if line < self.offset.row + scrolloff {
|
} else if line < self.offset.row + scrolloff {
|
||||||
// scroll up
|
// scroll up
|
||||||
self.offset.row = line.saturating_sub(scrolloff);
|
line.saturating_sub(scrolloff)
|
||||||
}
|
} else {
|
||||||
|
self.offset.row
|
||||||
|
};
|
||||||
|
|
||||||
if col > last_col.saturating_sub(scrolloff) {
|
let col = if col > last_col.saturating_sub(scrolloff) {
|
||||||
// scroll right
|
// scroll right
|
||||||
self.offset.col += col - (last_col.saturating_sub(scrolloff));
|
self.offset.col + col - (last_col.saturating_sub(scrolloff))
|
||||||
} else if col < self.offset.col + scrolloff {
|
} else if col < self.offset.col + scrolloff {
|
||||||
// scroll left
|
// scroll left
|
||||||
self.offset.col = col.saturating_sub(scrolloff);
|
col.saturating_sub(scrolloff)
|
||||||
|
} else {
|
||||||
|
self.offset.col
|
||||||
|
};
|
||||||
|
if row == self.offset.row && col == self.offset.col {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some((row, col))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ensure_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) {
|
||||||
|
if let Some((row, col)) = self.offset_coords_to_in_view(doc, scrolloff) {
|
||||||
|
self.offset.row = row;
|
||||||
|
self.offset.col = col;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) -> bool {
|
||||||
|
self.offset_coords_to_in_view(doc, scrolloff).is_none()
|
||||||
|
}
|
||||||
|
|
||||||
/// Calculates the last visible line on screen
|
/// Calculates the last visible line on screen
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn last_line(&self, doc: &Document) -> usize {
|
pub fn last_line(&self, doc: &Document) -> usize {
|
||||||
|
|
Loading…
Reference in New Issue