editor: Add delete line command. (ctrl-k)

This commit is contained in:
Andrew Hamilton 2022-01-07 11:15:30 +10:00
parent c66f79ea3d
commit 5e23b974f7
2 changed files with 20 additions and 1 deletions

View file

@ -425,6 +425,14 @@ class Editor:
self.previous_word()
self.delete_selection()
def delete_line(self):
empty_selection = self.text_widget[self.cursor_y][self.cursor_x:].strip() == ""
self.set_mark()
self.jump_to_end_of_line()
self.delete_selection()
if empty_selection:
self.delete_character()
def join_lines(self):
if self.cursor_y == 0:
self.jump_to_start_of_line()
@ -564,7 +572,7 @@ class Editor:
terminal.ALT_H: highlight_block, terminal.CTRL_R: syntax_highlight_all,
terminal.CTRL_L: center_cursor, terminal.ALT_SEMICOLON: comment_highlighted,
terminal.ALT_c: cycle_syntax_highlighting, terminal.CTRL_X: prefix, terminal.ESC: quit,
terminal.CTRL_C: ctrl_c}
terminal.CTRL_C: ctrl_c, terminal.CTRL_K: delete_line}
def main():

View file

@ -150,6 +150,17 @@ class EditorTestCase(unittest.TestCase):
(self.editor.join_lines, "ab- -cd ", (0, 0)),
(self.editor.join_lines, "ab- -cd ", (0, 0))])
def test_delete_line(self):
text = "a \ndef"
self._set_editor(text, (1, 0))
self._assert_changes([(self.editor.delete_line, "adef", (1, 0)),
(self.editor.delete_line, "a", (1, 0))])
text = "\nabc"
self._set_editor(text, (0, 0))
self._assert_changes([(self.editor.delete_line, "abc", (0, 0)),
(self.editor.delete_line, "", (0, 0)),
(self.editor.delete_line, "", (0, 0))])
if __name__ == "__main__":
unittest.main()