Skip to content

Commit 38662f8

Browse files
committed
GTextEditor: Merge with previous line if backspacing at column 0.
1 parent 6094f59 commit 38662f8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

LibGUI/GTextEditor.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,19 @@ void GTextEditor::keydown_event(GKeyEvent& event)
188188

189189
if (!event.modifiers() && event.key() == KeyCode::Key_Backspace) {
190190
if (m_cursor.column() > 0) {
191+
// Backspace within line
191192
current_line().remove(m_cursor.column() - 1);
192193
set_cursor(m_cursor.line(), m_cursor.column() - 1);
193194
}
195+
if (m_cursor.column() == 0 && m_cursor.line() != 0) {
196+
// Erase at column 0; merge with previous line
197+
auto& previous_line = *m_lines[m_cursor.line() - 1];
198+
int previous_length = previous_line.length();
199+
previous_line.append(current_line().characters(), current_line().length());
200+
m_lines.remove(m_cursor.line());
201+
update();
202+
set_cursor(m_cursor.line() - 1, previous_length);
203+
}
194204
return;
195205
}
196206

@@ -343,6 +353,14 @@ int GTextEditor::Line::width(const Font& font) const
343353
return font.glyph_width('x') * length();
344354
}
345355

356+
void GTextEditor::Line::append(const char* characters, int length)
357+
{
358+
int old_length = m_text.size() - 1;
359+
m_text.resize(m_text.size() + length);
360+
memcpy(m_text.data() + old_length, characters, length);
361+
m_text.last() = 0;
362+
}
363+
346364
void GTextEditor::Line::append(char ch)
347365
{
348366
insert(length(), ch);

LibGUI/GTextEditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class GTextEditor : public GWidget {
7272
void prepend(char);
7373
void insert(int index, char);
7474
void remove(int index);
75+
void append(const char*, int);
7576

7677
private:
7778
// NOTE: This vector is null terminated.

0 commit comments

Comments
 (0)