diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-08 15:55:58 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-08 15:55:58 +0100 |
commit | c4937f01d8fbd329683360bedd2a9b6029456a39 (patch) | |
tree | 76620feecdc53f5084bc2a9e3585af0dabd8e07a | |
parent | 5602f3be6cc2d02e84b18ebe95c251706573ab76 (diff) | |
download | serenity-c4937f01d8fbd329683360bedd2a9b6029456a39.zip |
GTextEditor: Simplify delete_selection().
-rw-r--r-- | LibGUI/GTextEditor.cpp | 51 | ||||
-rw-r--r-- | LibGUI/GTextEditor.h | 1 |
2 files changed, 40 insertions, 12 deletions
diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index 1be0da742a..accb9f9483 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -473,6 +473,8 @@ void GTextEditor::set_cursor(int line, int column) void GTextEditor::set_cursor(const GTextPosition& position) { ASSERT(!m_lines.is_empty()); + ASSERT(position.line() < m_lines.size()); + ASSERT(position.column() <= m_lines[position.line()]->length()); if (m_cursor == position) return; auto old_cursor_line_rect = line_widget_rect(m_cursor.line()); @@ -505,6 +507,12 @@ void GTextEditor::timer_event(GTimerEvent&) GTextEditor::Line::Line() { + clear(); +} + +void GTextEditor::Line::clear() +{ + m_text.clear(); m_text.append(0); } @@ -512,6 +520,10 @@ void GTextEditor::Line::set_text(const String& text) { if (text.length() == length() && !memcmp(text.characters(), characters(), length())) return; + if (text.is_empty()) { + clear(); + return; + } m_text.resize(text.length() + 1); memcpy(m_text.data(), text.characters(), text.length() + 1); } @@ -631,23 +643,38 @@ void GTextEditor::delete_selection() if (m_cursor < m_selection_start) swap(normalized_selection_start, normalized_selection_end); - for (int i = normalized_selection_start.line(); i <= normalized_selection_end.line();) { - auto& line = *m_lines[i]; - int selection_start_column_on_line = normalized_selection_start.line() == i ? normalized_selection_start.column() : 0; - int selection_end_column_on_line = normalized_selection_end.line() == i ? normalized_selection_end.column() : line.length(); - bool whole_line_is_selected = selection_start_column_on_line == 0 && selection_end_column_on_line == line.length(); + // First delete all the lines in between the first and last one. + for (int i = normalized_selection_start.line() + 1; i < normalized_selection_end.line();) { + m_lines.remove(i); + normalized_selection_end.set_line(normalized_selection_end.line() - 1); + } + + if (normalized_selection_start.line() == normalized_selection_end.line()) { + // Delete within same line. + auto& line = *m_lines[normalized_selection_start.line()]; + bool whole_line_is_selected = normalized_selection_start.column() == 0 && normalized_selection_end.column() == line.length(); if (whole_line_is_selected) { - m_lines.remove(i); - normalized_selection_end.set_line(normalized_selection_end.line() - 1); - continue; + line.clear(); + } else { + auto before_selection = String(line.characters(), line.length()).substring(0, normalized_selection_start.column()); + auto after_selection = String(line.characters(), line.length()).substring(normalized_selection_end.column(), line.length() - normalized_selection_end.column()); + StringBuilder builder(before_selection.length() + after_selection.length()); + builder.append(before_selection); + builder.append(after_selection); + line.set_text(builder.to_string()); } - auto before_selection = String(line.characters(), line.length()).substring(0, selection_start_column_on_line); - auto after_selection = String(line.characters(), line.length()).substring(selection_end_column_on_line, line.length() - selection_end_column_on_line); + } else { + // Delete across a newline, merging lines. + ASSERT(normalized_selection_start.line() == normalized_selection_end.line() - 1); + auto& first_line = *m_lines[normalized_selection_start.line()]; + auto& second_line = *m_lines[normalized_selection_end.line()]; + auto before_selection = String(first_line.characters(), first_line.length()).substring(0, normalized_selection_start.column()); + auto after_selection = String(second_line.characters(), second_line.length()).substring(normalized_selection_end.column(), second_line.length() - normalized_selection_end.column()); StringBuilder builder(before_selection.length() + after_selection.length()); builder.append(before_selection); builder.append(after_selection); - line.set_text(builder.to_string()); - ++i; + first_line.set_text(builder.to_string()); + m_lines.remove(normalized_selection_end.line()); } if (m_lines.is_empty()) diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h index 145b42f1f6..29db7f3d79 100644 --- a/LibGUI/GTextEditor.h +++ b/LibGUI/GTextEditor.h @@ -85,6 +85,7 @@ private: void remove(int index); void append(const char*, int); void truncate(int length); + void clear(); private: // NOTE: This vector is null terminated. |