on master help sign in
Fix panic from two windows editing the same document (#4570)
ba394dcMichael Davis
helix-term/src/ui/editor.rs+8−8
@@ -227,16 +227,16 @@ impl EditorView {
227 _theme: &Theme,
227 _theme: &Theme,
228 ) -> Box<dyn Iterator<Item = HighlightEvent> + 'doc> {
228 ) -> Box<dyn Iterator<Item = HighlightEvent> + 'doc> {
229 let text = doc.text().slice(..);
229 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− );
235
230
236 let range = {
231 let range = {
237− // calculate viewport byte ranges
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);
238+ let start = text.line_to_byte(offset.row.min(last_line));
239− let end = text.line_to_byte(last_line + 1);
239+ let end = text.line_to_byte(last_visible_line + 1);
240
240
241 start..end
241 start..end
242 };
242 };
helix-view/src/editor.rs+6−2
@@ -1223,9 +1223,11 @@ impl Editor {
1223 pub fn focus(&mut self, view_id: ViewId) {
1223 pub fn focus(&mut self, view_id: ViewId) {
1224 let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
1224 let prev_id = std::mem::replace(&mut self.tree.focus, view_id);
1225
1225
1226− // if leaving the view: mode should reset
1226+ // if leaving the view: mode should reset and the cursor should be
1227+ // within view
1227 if prev_id != view_id {
1228 if prev_id != view_id {
1228 self.mode = Mode::Normal;
1229 self.mode = Mode::Normal;
1230+ self.ensure_cursor_in_view(view_id);
1229 }
1231 }
1230 }
1232 }
1231
1233
@@ -1234,9 +1236,11 @@ impl Editor {
1234 self.tree.focus_next();
1236 self.tree.focus_next();
1235 let id = self.tree.focus;
1237 let id = self.tree.focus;
1236
1238
1237− // if leaving the view: mode should reset
1239+ // if leaving the view: mode should reset and the cursor should be
1240+ // within view
1238 if prev_id != id {
1241 if prev_id != id {
1239 self.mode = Mode::Normal;
1242 self.mode = Mode::Normal;
1243+ self.ensure_cursor_in_view(id);
1240 }
1244 }
1241 }
1245 }
1242
1246