editor: Add corner cases for comment_lines.
This commit is contained in:
parent
975d13594e
commit
2c873cc914
2 changed files with 55 additions and 18 deletions
|
|
@ -462,21 +462,37 @@ class Editor:
|
||||||
try:
|
try:
|
||||||
index = self.text_widget[self.cursor_y].index("#")
|
index = self.text_widget[self.cursor_y].index("#")
|
||||||
self.cursor_x = index + 1
|
self.cursor_x = index + 1
|
||||||
except ValueError:
|
except ValueError: # '#' not in line
|
||||||
self.jump_to_end_of_line()
|
self.jump_to_end_of_line()
|
||||||
self.insert_text(" # ")
|
self.insert_text(" # ")
|
||||||
else:
|
else:
|
||||||
(start_x, start_y), (end_x, end_y) = self.get_selection_interval()
|
(start_x, start_y), (end_x, end_y) = self.get_selection_interval()
|
||||||
min_indent = min(self._line_indent(y) for y in range(start_y, end_y)
|
if end_x != 0 and not self.cursor_x == len(self.text_widget[end_y]):
|
||||||
|
self.enter()
|
||||||
|
self.cursor_left()
|
||||||
|
if start_x != 0:
|
||||||
|
new_line = (self.text_widget[start_y][:start_x] + "# " +
|
||||||
|
self.text_widget[start_y][start_x:])
|
||||||
|
self.text_widget[start_y] = new_line
|
||||||
|
self.cursor_x = len(new_line)
|
||||||
|
start_y += 1
|
||||||
|
if end_x != 0:
|
||||||
|
end_y += 1
|
||||||
|
mid_lines = range(start_y, end_y)
|
||||||
|
try:
|
||||||
|
min_indent = min(self._line_indent(y) for y in mid_lines
|
||||||
if self.text_widget[y].strip() != "")
|
if self.text_widget[y].strip() != "")
|
||||||
if all(self.text_widget[y][min_indent:min_indent+2] == "# " or self.text_widget[y].strip() == ""
|
except ValueError:
|
||||||
for y in range(start_y, end_y)):
|
pass
|
||||||
for y in range(start_y, end_y):
|
else:
|
||||||
|
if all(self.text_widget[y][min_indent:min_indent+2] == "# "
|
||||||
|
or self.text_widget[y].strip() == "" for y in mid_lines):
|
||||||
|
for y in mid_lines:
|
||||||
line = self.text_widget[y]
|
line = self.text_widget[y]
|
||||||
if line.strip() != "":
|
if line.strip() != "":
|
||||||
self.text_widget[y] = line[:min_indent] + line[min_indent + 2:]
|
self.text_widget[y] = line[:min_indent] + line[min_indent + 2:]
|
||||||
else:
|
else:
|
||||||
for y in range(start_y, end_y):
|
for y in mid_lines:
|
||||||
line = self.text_widget[y]
|
line = self.text_widget[y]
|
||||||
if line.strip() != "":
|
if line.strip() != "":
|
||||||
self.text_widget[y] = line[:min_indent] + "# " + line[min_indent:]
|
self.text_widget[y] = line[:min_indent] + "# " + line[min_indent:]
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ class EditorTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def _assert_changes(self, changes):
|
def _assert_changes(self, changes):
|
||||||
for index, change in enumerate(changes):
|
for index, change in enumerate(changes):
|
||||||
with self.subTest(index=index, change=change):
|
# with self.subTest(index=index, change=change):
|
||||||
method, expected_text, expected_cursor_position = change
|
method, expected_text, expected_cursor_position = change
|
||||||
with contextlib.suppress(IndexError):
|
with contextlib.suppress(IndexError):
|
||||||
method()
|
method()
|
||||||
|
|
@ -166,12 +166,16 @@ class EditorTestCase(unittest.TestCase):
|
||||||
(self.editor.tab_align, " a\n b", (1, 1))])
|
(self.editor.tab_align, " a\n b", (1, 1))])
|
||||||
|
|
||||||
def test_comment_lines(self):
|
def test_comment_lines(self):
|
||||||
|
# from scratch
|
||||||
self._set_editor("", (0, 0))
|
self._set_editor("", (0, 0))
|
||||||
self._assert_changes([(self.editor.comment_lines, "# ", (2, 0))])
|
self._assert_changes([(self.editor.comment_lines, "# ", (2, 0))])
|
||||||
|
# No selection
|
||||||
self._set_editor("a", (0, 0))
|
self._set_editor("a", (0, 0))
|
||||||
self._assert_changes([(self.editor.comment_lines, "a # ", (5, 0))])
|
self._assert_changes([(self.editor.comment_lines, "a # ", (5, 0))])
|
||||||
|
# Comment when comment exists
|
||||||
self.editor.jump_to_start_of_line()
|
self.editor.jump_to_start_of_line()
|
||||||
self._assert_changes([(self.editor.comment_lines, "a # ", (4, 0))])
|
self._assert_changes([(self.editor.comment_lines, "a # ", (4, 0))])
|
||||||
|
# Selection containing blank lines
|
||||||
text = " a\n\n b\n"
|
text = " a\n\n b\n"
|
||||||
self._set_editor(text, (0, 0))
|
self._set_editor(text, (0, 0))
|
||||||
self.editor.set_mark()
|
self.editor.set_mark()
|
||||||
|
|
@ -180,11 +184,28 @@ class EditorTestCase(unittest.TestCase):
|
||||||
self.editor.cursor_down()
|
self.editor.cursor_down()
|
||||||
self._assert_changes([(self.editor.comment_lines, " # a\n\n # b\n", (0, 3))])
|
self._assert_changes([(self.editor.comment_lines, " # a\n\n # b\n", (0, 3))])
|
||||||
self.assertEqual(self.editor.mark, None)
|
self.assertEqual(self.editor.mark, None)
|
||||||
|
# Undo comments in selection
|
||||||
self.editor.set_mark()
|
self.editor.set_mark()
|
||||||
self.editor.cursor_up()
|
self.editor.cursor_up()
|
||||||
self.editor.cursor_up()
|
self.editor.cursor_up()
|
||||||
self.editor.cursor_up()
|
self.editor.cursor_up()
|
||||||
self._assert_changes([(self.editor.comment_lines, text, (0, 0))])
|
self._assert_changes([(self.editor.comment_lines, text, (0, 0))])
|
||||||
|
# Selection on one line, in middle
|
||||||
|
self._set_editor("abc", (1, 0))
|
||||||
|
self.editor.set_mark()
|
||||||
|
self.editor.cursor_right()
|
||||||
|
self._assert_changes([(self.editor.comment_lines, "a# b\nc", (4, 0))])
|
||||||
|
# Selection on one line, on right
|
||||||
|
self._set_editor("ab", (1, 0))
|
||||||
|
self.editor.set_mark()
|
||||||
|
self.editor.cursor_right()
|
||||||
|
self._assert_changes([(self.editor.comment_lines, "a# b", (4, 0))])
|
||||||
|
# Multi-line selection, starting middle, ending middle. Trailing unselected line
|
||||||
|
self._set_editor("abc\ndef\nghi\njkl", (2, 0))
|
||||||
|
self.editor.set_mark()
|
||||||
|
self.editor.cursor_down()
|
||||||
|
self.editor.cursor_down()
|
||||||
|
self._assert_changes([(self.editor.comment_lines, "ab# c\n# def\n# gh\ni\njkl", (4, 2))])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue