From fd9393c8b1212231e43fea198d58d5523d91d000 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 28 Aug 2020 21:09:16 -0700 Subject: [PATCH] fix position changes when commenting and cursor is before comment --- babi/file.py | 2 +- tests/features/comment_test.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/babi/file.py b/babi/file.py index 6b37f87..823c11e 100644 --- a/babi/file.py +++ b/babi/file.py @@ -691,7 +691,7 @@ class File: assert ws_match is not None ws_len = len(ws_match[0]) self.buf[lineno] = f'{ws_match[0]}{prefix}{line[ws_len:]}' - if lineno == self.buf.y: + if lineno == self.buf.y and self.buf.x > ws_len: self.buf.x += len(prefix) @edit_action('comment', final=True) diff --git a/tests/features/comment_test.py b/tests/features/comment_test.py index 1d5c192..b1bebb9 100644 --- a/tests/features/comment_test.py +++ b/tests/features/comment_test.py @@ -97,3 +97,18 @@ def test_add_comment_moves_cursor(run, ten_lines): h.press_and_enter(':comment') h.await_cursor_position(x=8, y=1) + + +def test_do_not_move_if_cursor_before_comment(run, tmpdir): + f = tmpdir.join('f') + f.write('\t\tfoo') + + with run(str(f)) as h, and_exit(h): + h.press('Right') + + h.await_cursor_position(x=4, y=1) + + trigger_command_mode(h) + h.press_and_enter(':comment') + + h.await_cursor_position(x=4, y=1)