GTextEditor: Merge with previous line if backspacing at column 0.

This commit is contained in:
Andreas Kling 2019-03-07 16:15:25 +01:00
parent 6094f592a9
commit 38662f884d
Notes: sideshowbarker 2024-07-19 15:08:26 +09:00
2 changed files with 19 additions and 0 deletions

View file

@ -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);

View file

@ -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.