From b525a0978d8a77114bfc4142c316334483bee61d Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sat, 7 May 2022 15:23:35 +0200 Subject: [PATCH] fix column_to_char for tabs --- src/ui/bigtext/line.py | 4 ++++ src/ui/bigtext/testline.py | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ui/bigtext/line.py b/src/ui/bigtext/line.py index cb9acd2..11b794b 100644 --- a/src/ui/bigtext/line.py +++ b/src/ui/bigtext/line.py @@ -77,8 +77,12 @@ class Line: return result def column_to_char(self, column_in_line: int) -> int: + while not column_in_line in self._column_to_char_cache and column_in_line > 0: + column_in_line = column_in_line - 1 + if column_in_line in self._column_to_char_cache: return self._column_to_char_cache[column_in_line] + return 0 def char_to_column(self, char_in_line: int) -> int: diff --git a/src/ui/bigtext/testline.py b/src/ui/bigtext/testline.py index 6f7ab55..8c17e33 100644 --- a/src/ui/bigtext/testline.py +++ b/src/ui/bigtext/testline.py @@ -11,7 +11,11 @@ from line import Line class MyTestCase(unittest.TestCase): def test_column_to_char(self): byte_offset = 123 - text = "\tabc\td\tef\tg" # will be rendered as: ....abc.d...ef..g where . represents a whitespace column + # will be rendered as: + # ....abc.d...ef..g where . represents a whitespace column + # 00001234566678991 character index + # 01234567890123456 colum index + text = "\tabc\td\tef\tg" line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text) self.assertEqual(0, line.column_to_char(0)) # the tab @@ -99,8 +103,8 @@ class MyTestCase(unittest.TestCase): print(text) line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text) self.assertEqual(0, line.byte_index_to_char_index(0)) # x - self.assertEqual(0, line.byte_index_to_char_index(1)) # first byte of diacritical mark belonging to x - self.assertEqual(0, line.byte_index_to_char_index(2)) # second byte of diacritical mark belonging to x + self.assertEqual(1, line.byte_index_to_char_index(1)) # first byte of diacritical mark belonging to x + self.assertEqual(1, line.byte_index_to_char_index(2)) # second byte of diacritical mark belonging to x def test_diacritical_marks(self): text = "̈ẍỏôŏ̮👍🏿"