From bbb21194a54dbbda470ebc1fcae425572ebfa4d2 Mon Sep 17 00:00:00 2001 From: euclidianAce Date: Sat, 15 May 2021 11:38:53 -0500 Subject: [PATCH] TextEditor: Clear leftover whitespace when inserting newlines Previously when entering a newline, previous indentation would be left, leaving a line consisting only of whitespace. This fixes that. --- Userland/Libraries/LibGUI/TextEditor.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index 44aa98cef9..e1061a4aa8 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -1244,7 +1244,23 @@ void TextEditor::insert_at_cursor_or_replace_selection(const StringView& text) VERIFY(is_editable()); if (has_selection()) delete_selection(); + + // Check if adding a newline leaves the previous line as just whitespace. + auto const clear_length = m_cursor.column(); + auto const should_clear_last_line = text == "\n" + && clear_length > 0 + && current_line().leading_spaces() == clear_length; + execute(text, m_cursor); + + if (should_clear_last_line) { // If it does leave just whitespace, clear it. + auto const original_cursor_position = cursor(); + TextPosition start(original_cursor_position.line() - 1, 0); + TextPosition end(original_cursor_position.line() - 1, clear_length); + TextRange erased_range(start, end); + execute(document().text_in_range(erased_range), erased_range); + set_cursor(original_cursor_position); + } } void TextEditor::cut()