Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only align cursor center when it isn't in view. #959

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,7 @@ fn search_impl(
regex: &Regex,
movement: Movement,
direction: Direction,
scrolloff: usize,
) {
let text = doc.text().slice(..);
let selection = doc.selection(view.id);
Expand Down Expand Up @@ -1232,7 +1233,11 @@ fn search_impl(
};

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)
}
};
}

Expand All @@ -1256,6 +1261,8 @@ fn rsearch(cx: &mut Context) {
// TODO: use one function for search vs extend
fn searcher(cx: &mut Context, direction: Direction) {
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 @@ -1280,14 +1287,23 @@ fn searcher(cx: &mut Context, direction: Direction) {
if event != PromptEvent::Update {
return;
}
search_impl(doc, view, &contents, &regex, Movement::Move, direction);
search_impl(
doc,
view,
&contents,
&regex,
Movement::Move,
direction,
scrolloff,
);
},
);

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

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 registers = &cx.editor.registers;
if let Some(query) = registers.read('/') {
Expand All @@ -1302,7 +1318,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
.case_insensitive(case_insensitive)
.build()
{
search_impl(doc, view, &contents, &regex, movement, direction);
search_impl(doc, view, &contents, &regex, movement, direction, scrolloff);
} else {
// get around warning `mutable_borrow_reservation_conflict`
// which will be a hard error in the future
Expand Down
41 changes: 33 additions & 8 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ impl View {
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
.selection(self.id)
.primary()
Expand All @@ -104,23 +109,43 @@ impl View {

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
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 {
// 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
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 {
// 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()
}
Comment on lines +138 to +147
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to call offset_coords_to_in_view twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scrolloff is different between two calls.
I don't have idea to merge two calls.


/// Calculates the last visible line on screen
#[inline]
pub fn last_line(&self, doc: &Document) -> usize {
Expand Down