Skip to content

Commit ba394dc

Browse files
Fix panic from two windows editing the same document (#4570)
* Clamp highlighting range to be within document This fixes a panic possible when two vsplits of the same document exist and enough lines are deleted from the document so that one of the windows focuses past the end of the document. * Ensure cursor is in view on window change If two windows are editing the same document, one may delete enough of the document so that the other window is pointing at a blank page (past the document end). In this change we ensure that the cursor is within view whenever we switch to a new window (for example with `<C-w>w`). * Update helix-term/src/ui/editor.rs Co-authored-by: Blaž Hrastnik <[email protected]> Co-authored-by: Blaž Hrastnik <[email protected]>
1 parent 1bed2f3 commit ba394dc

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

helix-term/src/ui/editor.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,16 @@ impl EditorView {
227227
_theme: &Theme,
228228
) -> Box<dyn Iterator<Item = HighlightEvent> + 'doc> {
229229
let text = doc.text().slice(..);
230-
let last_line = std::cmp::min(
231-
// Saturating subs to make it inclusive zero indexing.
232-
(offset.row + height as usize).saturating_sub(1),
233-
doc.text().len_lines().saturating_sub(1),
234-
);
235230

236231
let range = {
237-
// calculate viewport byte ranges
238-
let start = text.line_to_byte(offset.row);
239-
let end = text.line_to_byte(last_line + 1);
232+
// Calculate viewport byte ranges:
233+
// Saturating subs to make it inclusive zero indexing.
234+
let last_line = doc.text().len_lines().saturating_sub(1);
235+
let last_visible_line = (offset.row + height as usize)
236+
.saturating_sub(1)
237+
.min(last_line);
238+
let start = text.line_to_byte(offset.row.min(last_line));
239+
let end = text.line_to_byte(last_visible_line + 1);
240240

241241
start..end
242242
};

helix-view/src/editor.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,9 +1223,11 @@ impl Editor {
12231223
pub fn focus(&mut self, view_id: ViewId) {
12241224
let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
12251225

1226-
// if leaving the view: mode should reset
1226+
// if leaving the view: mode should reset and the cursor should be
1227+
// within view
12271228
if prev_id != view_id {
12281229
self.mode = Mode::Normal;
1230+
self.ensure_cursor_in_view(view_id);
12291231
}
12301232
}
12311233

@@ -1234,9 +1236,11 @@ impl Editor {
12341236
self.tree.focus_next();
12351237
let id = self.tree.focus;
12361238

1237-
// if leaving the view: mode should reset
1239+
// if leaving the view: mode should reset and the cursor should be
1240+
// within view
12381241
if prev_id != id {
12391242
self.mode = Mode::Normal;
1243+
self.ensure_cursor_in_view(id);
12401244
}
12411245
}
12421246

0 commit comments

Comments
 (0)