diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index 63ad6ed1de..035173290e 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -188,9 +188,19 @@ void GTextEditor::keydown_event(GKeyEvent& event) if (!event.modifiers() && event.key() == KeyCode::Key_Backspace) { if (m_cursor.column() > 0) { + // Backspace within line current_line().remove(m_cursor.column() - 1); set_cursor(m_cursor.line(), m_cursor.column() - 1); } + if (m_cursor.column() == 0 && m_cursor.line() != 0) { + // Erase at column 0; merge with previous line + auto& previous_line = *m_lines[m_cursor.line() - 1]; + int previous_length = previous_line.length(); + previous_line.append(current_line().characters(), current_line().length()); + m_lines.remove(m_cursor.line()); + update(); + set_cursor(m_cursor.line() - 1, previous_length); + } return; } @@ -343,6 +353,14 @@ int GTextEditor::Line::width(const Font& font) const return font.glyph_width('x') * length(); } +void GTextEditor::Line::append(const char* characters, int length) +{ + int old_length = m_text.size() - 1; + m_text.resize(m_text.size() + length); + memcpy(m_text.data() + old_length, characters, length); + m_text.last() = 0; +} + void GTextEditor::Line::append(char ch) { insert(length(), ch); diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h index 5c16cf04a8..5964df624b 100644 --- a/LibGUI/GTextEditor.h +++ b/LibGUI/GTextEditor.h @@ -72,6 +72,7 @@ private: void prepend(char); void insert(int index, char); void remove(int index); + void append(const char*, int); private: // NOTE: This vector is null terminated.