From 365a76c4ed79520020ece432d5c09768db00c3bf Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Tue, 15 Mar 2022 10:21:23 +1000 Subject: [PATCH] Fix crash when switching to non-existant line - When switching from an insert area at the bottom of a file. --- diff_edit/__init__.py | 3 ++- diff_edit/editor.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/diff_edit/__init__.py b/diff_edit/__init__.py index 083bab3..96e413f 100755 --- a/diff_edit/__init__.py +++ b/diff_edit/__init__.py @@ -209,7 +209,8 @@ class DiffEditor: right_start, right_end, left_start, left_end if left_start <= y < left_end: fraction = (y - left_start) / (left_end - left_start) - return round(right_start + fraction * (right_end - right_start)) + other_y = round(right_start + fraction * (right_end - right_start)) + return other_y - 1 if other_y == len(self.editors[1].text_widget) else other_y return 0 def follow_scroll(self): diff --git a/diff_edit/editor.py b/diff_edit/editor.py index b284f20..8068c76 100755 --- a/diff_edit/editor.py +++ b/diff_edit/editor.py @@ -263,8 +263,9 @@ class Editor: if not self.is_editing: return result if self.mark is None: - result[self.cursor_y - view_y] = highlight_str(result[self.cursor_y - view_y], - termstr.Color.white, 0.8) + with contextlib.suppress(IndexError): # Fix. The cursor should always be on screen. + result[self.cursor_y - view_y] = highlight_str(result[self.cursor_y - view_y], + termstr.Color.white, 0.8) else: (start_x, start_y), (end_x, end_y) = self.get_selection_interval() screen_start_x = len(expand_str(self.text_widget[start_y][:start_x]))