mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:38:10 +00:00
GTextEditor: Simplify delete_selection().
This commit is contained in:
parent
5602f3be6c
commit
c4937f01d8
2 changed files with 40 additions and 12 deletions
|
@ -473,6 +473,8 @@ void GTextEditor::set_cursor(int line, int column)
|
||||||
void GTextEditor::set_cursor(const GTextPosition& position)
|
void GTextEditor::set_cursor(const GTextPosition& position)
|
||||||
{
|
{
|
||||||
ASSERT(!m_lines.is_empty());
|
ASSERT(!m_lines.is_empty());
|
||||||
|
ASSERT(position.line() < m_lines.size());
|
||||||
|
ASSERT(position.column() <= m_lines[position.line()]->length());
|
||||||
if (m_cursor == position)
|
if (m_cursor == position)
|
||||||
return;
|
return;
|
||||||
auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
|
auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
|
||||||
|
@ -505,6 +507,12 @@ void GTextEditor::timer_event(GTimerEvent&)
|
||||||
|
|
||||||
GTextEditor::Line::Line()
|
GTextEditor::Line::Line()
|
||||||
{
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GTextEditor::Line::clear()
|
||||||
|
{
|
||||||
|
m_text.clear();
|
||||||
m_text.append(0);
|
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()))
|
if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
|
||||||
return;
|
return;
|
||||||
|
if (text.is_empty()) {
|
||||||
|
clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
m_text.resize(text.length() + 1);
|
m_text.resize(text.length() + 1);
|
||||||
memcpy(m_text.data(), text.characters(), 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)
|
if (m_cursor < m_selection_start)
|
||||||
swap(normalized_selection_start, normalized_selection_end);
|
swap(normalized_selection_start, normalized_selection_end);
|
||||||
|
|
||||||
for (int i = normalized_selection_start.line(); i <= normalized_selection_end.line();) {
|
// First delete all the lines in between the first and last one.
|
||||||
auto& line = *m_lines[i];
|
for (int i = normalized_selection_start.line() + 1; i < normalized_selection_end.line();) {
|
||||||
int selection_start_column_on_line = normalized_selection_start.line() == i ? normalized_selection_start.column() : 0;
|
m_lines.remove(i);
|
||||||
int selection_end_column_on_line = normalized_selection_end.line() == i ? normalized_selection_end.column() : line.length();
|
normalized_selection_end.set_line(normalized_selection_end.line() - 1);
|
||||||
bool whole_line_is_selected = selection_start_column_on_line == 0 && selection_end_column_on_line == line.length();
|
}
|
||||||
|
|
||||||
|
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) {
|
if (whole_line_is_selected) {
|
||||||
m_lines.remove(i);
|
line.clear();
|
||||||
normalized_selection_end.set_line(normalized_selection_end.line() - 1);
|
} else {
|
||||||
continue;
|
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);
|
} else {
|
||||||
auto after_selection = String(line.characters(), line.length()).substring(selection_end_column_on_line, line.length() - selection_end_column_on_line);
|
// 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());
|
StringBuilder builder(before_selection.length() + after_selection.length());
|
||||||
builder.append(before_selection);
|
builder.append(before_selection);
|
||||||
builder.append(after_selection);
|
builder.append(after_selection);
|
||||||
line.set_text(builder.to_string());
|
first_line.set_text(builder.to_string());
|
||||||
++i;
|
m_lines.remove(normalized_selection_end.line());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_lines.is_empty())
|
if (m_lines.is_empty())
|
||||||
|
|
|
@ -85,6 +85,7 @@ private:
|
||||||
void remove(int index);
|
void remove(int index);
|
||||||
void append(const char*, int);
|
void append(const char*, int);
|
||||||
void truncate(int length);
|
void truncate(int length);
|
||||||
|
void clear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// NOTE: This vector is null terminated.
|
// NOTE: This vector is null terminated.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue