mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-11 18:50:50 +00:00
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.
This commit is contained in:
parent
0994d0a33e
commit
bbb21194a5
Notes:
sideshowbarker
2024-07-18 18:06:07 +09:00
Author: https://github.com/euclidianAce 🔰
Commit: bbb21194a5
Pull-request: https://github.com/SerenityOS/serenity/pull/6726
Reviewed-by: https://github.com/awesomekling
Reviewed-by: https://github.com/linusg
1 changed files with 16 additions and 0 deletions
|
@ -1244,7 +1244,23 @@ void TextEditor::insert_at_cursor_or_replace_selection(const StringView& text)
|
||||||
VERIFY(is_editable());
|
VERIFY(is_editable());
|
||||||
if (has_selection())
|
if (has_selection())
|
||||||
delete_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<InsertTextCommand>(text, m_cursor);
|
execute<InsertTextCommand>(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<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
|
||||||
|
set_cursor(original_cursor_position);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditor::cut()
|
void TextEditor::cut()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue