From 447141a7bc2b453fe7dfd4ea81157ca8d7b7555a Mon Sep 17 00:00:00 2001 From: Marsma13 Date: Wed, 4 May 2022 21:36:48 +0000 Subject: [PATCH 1/4] Change of behavour of Right command for spaces at the start of line --- babi/file.py | 10 ++++++++- tests/features/movement_test.py | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/babi/file.py b/babi/file.py index 57b707e7..5db1c266 100644 --- a/babi/file.py +++ b/babi/file.py @@ -377,7 +377,15 @@ def ctrl_right(self, dim: Dim) -> None: else: self.buf.right(dim) tp = line[self.buf.x].isalnum() - while self.buf.x < len(line) and tp == line[self.buf.x].isalnum(): + while ( + self.buf.x < len(line) and + tp == line[self.buf.x].isalnum() and + not ( + self.buf.x == 1 and + line[self.buf.x - 1].isspace() and + not line[self.buf.x].isspace() + ) + ): self.buf.right(dim) @action diff --git a/tests/features/movement_test.py b/tests/features/movement_test.py index 84df1978..589b951a 100644 --- a/tests/features/movement_test.py +++ b/tests/features/movement_test.py @@ -545,3 +545,43 @@ def another_test(): h.press('M-Down') h.await_cursor_position(x=0, y=12) + + +def test_ctrl_right_jump_by_word_with_tabs(run, tmpdir): + src = '''\ +\tHello\tWorld +\t\tGoodday +hi\t\tAnthonk + +\t\t\tthis(is_some_code) # comment +''' + f = tmpdir.join('f') + f.write(src) + with run(str(f)) as h, and_exit(h): + h.press('^Right') + h.await_cursor_position(x=4, y=1) + h.press('^Right') + h.await_cursor_position(x=9, y=1) + h.press('Left') + h.await_cursor_position(x=8, y=1) + h.press('^Right') + h.await_cursor_position(x=12, y=1) + h.press('^Right') + h.await_cursor_position(x=17, y=1) + h.press('^Right') + h.await_cursor_position(x=8, y=2) + h.press('^Right') + h.await_cursor_position(x=15, y=2) + h.press('^Right') + h.await_cursor_position(x=0, y=3) + h.press('^Right') + h.await_cursor_position(x=2, y=3) + h.press('^Right') + h.await_cursor_position(x=8, y=3) + h.press('^Right') + h.await_cursor_position(x=15, y=3) + h.press('^Right') + h.await_cursor_position(x=12, y=5) + h.press('Down') + h.press('^Right') + h.await_cursor_position(x=0, y=6) From 037ed8976c81923a009c089e53eaa1f8fb08ed62 Mon Sep 17 00:00:00 2001 From: Marsma13 Date: Thu, 5 May 2022 16:38:55 +0000 Subject: [PATCH 2/4] Change behavior for tabs in editor --- babi/file.py | 31 +++++++++++++++++++++++++++---- tests/features/movement_test.py | 29 +++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/babi/file.py b/babi/file.py index 5db1c266..59159f3e 100644 --- a/babi/file.py +++ b/babi/file.py @@ -302,6 +302,30 @@ def _initialize_highlighters(self) -> None: for file_hl in self._file_hls: file_hl.register_callbacks(self.buf) + def _is_last_char_in_tab(self, line: str, pos: int) -> bool: + current = 0 + cpos = 0 + if line[pos - 1] != '\t': + return True + while cpos < pos - 1: + if line[cpos] == '\t': + current = current + 4 - (current % 4) + else: + current += 1 + cpos += 1 + return current % 4 == 3 + # for i, x in enumerate(line): + # if x == '\t' and i < tab_pos: + # current = current + 4 - (current % 4) + # elif x == '\t': + # ret = current % 4 == 3 + # break + # else: + # current += 1 + # if line[tab_pos] != '\t': + # return True + # return ret + def reload_theme(self, syntax: Syntax) -> None: self._syntax = syntax self.lint_errors = self.lint_errors.clone( @@ -380,10 +404,9 @@ def ctrl_right(self, dim: Dim) -> None: while ( self.buf.x < len(line) and tp == line[self.buf.x].isalnum() and - not ( - self.buf.x == 1 and - line[self.buf.x - 1].isspace() and - not line[self.buf.x].isspace() + ( + self._is_last_char_in_tab(line, self.buf.x) or + line[self.buf.x].isspace() ) ): self.buf.right(dim) diff --git a/tests/features/movement_test.py b/tests/features/movement_test.py index 589b951a..7cc5d3ed 100644 --- a/tests/features/movement_test.py +++ b/tests/features/movement_test.py @@ -547,12 +547,13 @@ def another_test(): h.await_cursor_position(x=0, y=12) -def test_ctrl_right_jump_by_word_with_tabs(run, tmpdir): +def test_ctrl_right_jump_with_tabs(run, tmpdir): src = '''\ \tHello\tWorld \t\tGoodday hi\t\tAnthonk - +as\tHello +Helloa\tdsa\tasd \t\t\tthis(is_some_code) # comment ''' f = tmpdir.join('f') @@ -581,7 +582,27 @@ def test_ctrl_right_jump_by_word_with_tabs(run, tmpdir): h.press('^Right') h.await_cursor_position(x=15, y=3) h.press('^Right') - h.await_cursor_position(x=12, y=5) + h.await_cursor_position(x=0, y=4) + h.press('^Right') + h.await_cursor_position(x=2, y=4) + h.press('Left') + h.await_cursor_position(x=1, y=4) + h.press('^Right') + h.await_cursor_position(x=4, y=4) + h.press('^Right') + h.await_cursor_position(x=9, y=4) + h.press('^Right') + h.await_cursor_position(x=0, y=5) + h.press('^Right') + h.await_cursor_position(x=6, y=5) + h.press('^Right') + h.await_cursor_position(x=8, y=5) + h.press('^Right') + h.await_cursor_position(x=11, y=5) + h.press('^Right') + h.await_cursor_position(x=15, y=5) + h.press('^Right') + h.await_cursor_position(x=12, y=6) h.press('Down') h.press('^Right') - h.await_cursor_position(x=0, y=6) + h.await_cursor_position(x=0, y=7) From c4ef37500987250a2a01f794002a0adb553b67fe Mon Sep 17 00:00:00 2001 From: Marsma13 Date: Thu, 5 May 2022 16:50:45 +0000 Subject: [PATCH 3/4] Cleanup of comments and some condition --- babi/file.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/babi/file.py b/babi/file.py index 59159f3e..9828769b 100644 --- a/babi/file.py +++ b/babi/file.py @@ -305,7 +305,7 @@ def _initialize_highlighters(self) -> None: def _is_last_char_in_tab(self, line: str, pos: int) -> bool: current = 0 cpos = 0 - if line[pos - 1] != '\t': + if line[pos - 1] != '\t' or line[pos].isspace(): return True while cpos < pos - 1: if line[cpos] == '\t': @@ -314,17 +314,6 @@ def _is_last_char_in_tab(self, line: str, pos: int) -> bool: current += 1 cpos += 1 return current % 4 == 3 - # for i, x in enumerate(line): - # if x == '\t' and i < tab_pos: - # current = current + 4 - (current % 4) - # elif x == '\t': - # ret = current % 4 == 3 - # break - # else: - # current += 1 - # if line[tab_pos] != '\t': - # return True - # return ret def reload_theme(self, syntax: Syntax) -> None: self._syntax = syntax @@ -404,10 +393,7 @@ def ctrl_right(self, dim: Dim) -> None: while ( self.buf.x < len(line) and tp == line[self.buf.x].isalnum() and - ( - self._is_last_char_in_tab(line, self.buf.x) or - line[self.buf.x].isspace() - ) + self._is_last_char_in_tab(line, self.buf.x) ): self.buf.right(dim) From 6d135bb33fd9be43f6367d328122ccc2df5e779a Mon Sep 17 00:00:00 2001 From: Marsma13 Date: Thu, 5 May 2022 19:45:20 +0000 Subject: [PATCH 4/4] Change behaviour for ^right with tabs --- babi/file.py | 2 +- tests/features/movement_test.py | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/babi/file.py b/babi/file.py index 57b707e7..7fed1afc 100644 --- a/babi/file.py +++ b/babi/file.py @@ -376,7 +376,7 @@ def ctrl_right(self, dim: Dim) -> None: # if we're inside the line, jump to next position that's not our type else: self.buf.right(dim) - tp = line[self.buf.x].isalnum() + tp = line[self.buf.x].isalnum() and line[self.buf.x - 1] != '\t' while self.buf.x < len(line) and tp == line[self.buf.x].isalnum(): self.buf.right(dim) diff --git a/tests/features/movement_test.py b/tests/features/movement_test.py index 84df1978..a9bd1b71 100644 --- a/tests/features/movement_test.py +++ b/tests/features/movement_test.py @@ -545,3 +545,66 @@ def another_test(): h.press('M-Down') h.await_cursor_position(x=0, y=12) + + +def test_ctrl_right_jump_with_tabs(run, tmpdir): + src = '''\ +\tHello\tWorld +\t\tGoodday +hi\t\tAnthonk +as\tHello +Helloa\tdsa\tasd +\t\t\tthis(is_some_code) # comment +''' + f = tmpdir.join('f') + f.write(src) + with run(str(f)) as h, and_exit(h): + h.press('^Right') + h.await_cursor_position(x=4, y=1) + h.press('^Right') + h.await_cursor_position(x=9, y=1) + h.press('Left') + h.await_cursor_position(x=8, y=1) + h.press('^Right') + h.await_cursor_position(x=12, y=1) + h.press('^Right') + h.await_cursor_position(x=17, y=1) + h.press('^Right') + h.await_cursor_position(x=8, y=2) + h.press('^Right') + h.await_cursor_position(x=15, y=2) + h.press('^Right') + h.await_cursor_position(x=0, y=3) + h.press('^Right') + h.await_cursor_position(x=2, y=3) + h.press('^Right') + h.await_cursor_position(x=8, y=3) + h.press('^Right') + h.await_cursor_position(x=15, y=3) + h.press('^Right') + h.await_cursor_position(x=0, y=4) + h.press('^Right') + h.await_cursor_position(x=2, y=4) + h.press('Left') + h.await_cursor_position(x=1, y=4) + h.press('^Right') + h.await_cursor_position(x=4, y=4) + h.press('^Right') + h.await_cursor_position(x=9, y=4) + h.press('^Right') + h.await_cursor_position(x=0, y=5) + h.press('^Right') + h.await_cursor_position(x=6, y=5) + h.press('^Right') + h.await_cursor_position(x=8, y=5) + h.press('^Right') + h.await_cursor_position(x=11, y=5) + h.press('^Right') + h.await_cursor_position(x=12, y=5) + h.press('^Right') + h.await_cursor_position(x=15, y=5) + h.press('^Right') + h.await_cursor_position(x=12, y=6) + h.press('Down') + h.press('^Right') + h.await_cursor_position(x=0, y=7)