Merge pull request #90 from asottile/fix_out_of_bounds_on_uncomment

Fix out of bounds on uncomment
This commit is contained in:
Anthony Sottile
2020-08-28 21:00:52 -07:00
committed by GitHub
2 changed files with 30 additions and 1 deletions

View File

@@ -681,13 +681,18 @@ class File:
rest_offset = ws_len + len(prefix)
if line.startswith(prefix, ws_len):
self.buf[lineno] = f'{ws_match[0]}{line[rest_offset:].lstrip()}'
if self.buf.y == lineno and self.buf.x > ws_len:
self.buf.x -= len(line) - len(self.buf[lineno])
def _comment_add(self, lineno: int, prefix: str) -> None:
prefix = f'{prefix} '
line = self.buf[lineno]
ws_match = WS_RE.match(line)
assert ws_match is not None
ws_len = len(ws_match[0])
self.buf[lineno] = f'{ws_match[0]}{prefix} {line[ws_len:]}'
self.buf[lineno] = f'{ws_match[0]}{prefix}{line[ws_len:]}'
if lineno == self.buf.y:
self.buf.x += len(prefix)
@edit_action('comment', final=True)
def toggle_comment(self, prefix: str) -> None:

View File

@@ -73,3 +73,27 @@ def test_comment_with_trailing_whitespace(run, ten_lines):
h.press_and_enter(':comment // ')
h.await_text('// line_0\nline_1\n')
def test_comment_cursor_at_end_of_line(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
h.press('# ')
h.press('End')
h.await_cursor_position(x=8, y=1)
trigger_command_mode(h)
h.press_and_enter(':comment')
h.await_cursor_position(x=6, y=1)
def test_add_comment_moves_cursor(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
h.press('End')
h.await_cursor_position(x=6, y=1)
trigger_command_mode(h)
h.press_and_enter(':comment')
h.await_cursor_position(x=8, y=1)