on master help sign in
search_impl will only align cursor center when it isn't in view (#959)
commita69caff450ff8201e16d0a0b4617114e03ed3c97 authorCossonLeo <20379044+cossonleo@users.noreply.github.com> dateTue 9 Nov 2021, 10:11 (5 years ago) parentf96be0f
2 files changed+52−11
helix-term/src/commands.rs+19−3 helix-view/src/view.rs+33−8
helix-term/src/commands.rs+19−3
@@ -1175,6 +1175,7 @@ fn search_impl(
11751175 regex: &Regex,
11761176 movement: Movement,
11771177 direction: Direction,
1178+ scrolloff: usize,
11781179) {
11791180 let text = doc.text().slice(..);
11801181 let selection = doc.selection(view.id);
@@ -1233,7 +1234,11 @@ fn search_impl(
12331234 };
12341235
12351236 doc.set_selection(view.id, selection);
1236− align_view(doc, view, Align::Center);
1237+ if view.is_cursor_in_view(doc, 0) {
1238+ view.ensure_cursor_in_view(doc, scrolloff);
1239+ } else {
1240+ align_view(doc, view, Align::Center)
1241+ }
12371242 };
12381243}
12391244
@@ -1257,6 +1262,8 @@ fn rsearch(cx: &mut Context) {
12571262// TODO: use one function for search vs extend
12581263fn searcher(cx: &mut Context, direction: Direction) {
12591264 let reg = cx.register.unwrap_or('/');
1265+ let scrolloff = cx.editor.config.scrolloff;
1266+
12601267 let (_, doc) = current!(cx.editor);
12611268
12621269 // TODO: could probably share with select_on_matches?
@@ -1281,7 +1288,15 @@ fn searcher(cx: &mut Context, direction: Direction) {
12811288 if event != PromptEvent::Update {
12821289 return;
12831290 }
1284− search_impl(doc, view, &contents, &regex, Movement::Move, direction);
1291+ search_impl(
1292+ doc,
1293+ view,
1294+ &contents,
1295+ &regex,
1296+ Movement::Move,
1297+ direction,
1298+ scrolloff,
1299+ );
12851300 },
12861301 );
12871302
@@ -1289,6 +1304,7 @@ fn searcher(cx: &mut Context, direction: Direction) {
12891304}
12901305
12911306fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) {
1307+ let scrolloff = cx.editor.config.scrolloff;
12921308 let (view, doc) = current!(cx.editor);
12931309 let registers = &cx.editor.registers;
12941310 if let Some(query) = registers.read('/') {
@@ -1303,7 +1319,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
13031319 .case_insensitive(case_insensitive)
13041320 .build()
13051321 {
1306− search_impl(doc, view, &contents, &regex, movement, direction);
1322+ search_impl(doc, view, &contents, &regex, movement, direction, scrolloff);
13071323 } else {
13081324 // get around warning `mutable_borrow_reservation_conflict`
13091325 // which will be a hard error in the future
helix-view/src/view.rs+33−8
@@ -85,7 +85,12 @@ impl View {
8585 self.area.clip_left(OFFSET).clip_bottom(1) // -1 for statusline
8686 }
8787
88− pub fn ensure_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) {
88+ //
89+ pub fn offset_coords_to_in_view(
90+ &self,
91+ doc: &Document,
92+ scrolloff: usize,
93+ ) -> Option<(usize, usize)> {
8994 let cursor = doc
9095 .selection(self.id)
9196 .primary()
@@ -104,23 +109,43 @@ impl View {
104109
105110 let last_col = self.offset.col + inner_area.width.saturating_sub(1) as usize;
106111
107− if line > last_line.saturating_sub(scrolloff) {
112+ let row = if line > last_line.saturating_sub(scrolloff) {
108113 // scroll down
109− self.offset.row += line - (last_line.saturating_sub(scrolloff));
114+ self.offset.row + line - (last_line.saturating_sub(scrolloff))
110115 } else if line < self.offset.row + scrolloff {
111116 // scroll up
112− self.offset.row = line.saturating_sub(scrolloff);
113− }
117+ line.saturating_sub(scrolloff)
118+ } else {
119+ self.offset.row
120+ };
114121
115− if col > last_col.saturating_sub(scrolloff) {
122+ let col = if col > last_col.saturating_sub(scrolloff) {
116123 // scroll right
117− self.offset.col += col - (last_col.saturating_sub(scrolloff));
124+ self.offset.col + col - (last_col.saturating_sub(scrolloff))
118125 } else if col < self.offset.col + scrolloff {
119126 // scroll left
120− self.offset.col = col.saturating_sub(scrolloff);
127+ col.saturating_sub(scrolloff)
128+ } else {
129+ self.offset.col
130+ };
131+ if row == self.offset.row && col == self.offset.col {
132+ None
133+ } else {
134+ Some((row, col))
121135 }
122136 }
123137
138+ pub fn ensure_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) {
139+ if let Some((row, col)) = self.offset_coords_to_in_view(doc, scrolloff) {
140+ self.offset.row = row;
141+ self.offset.col = col;
142+ }
143+ }
144+
145+ pub fn is_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) -> bool {
146+ self.offset_coords_to_in_view(doc, scrolloff).is_none()
147+ }
148+
124149 /// Calculates the last visible line on screen
125150 #[inline]
126151 pub fn last_line(&self, doc: &Document) -> usize {