1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:07:35 +00:00

EditingEngine: Split selection from movement functions

This patch moves selection updates outside movement functions in
EditingEngine.  Previously, movement functions would automatically
update the selection based on whether the Shift key was pressed down
during movement.  However, not all EditingEngine subclasses want that;
VimEditingEngine being a good example (because all selection is handled
in visual mode).

Therefore, this patch moves all selection updating to
EditingEngine::on_key().  Subclasses wishing to provide custom movement
and selection semantics should override it (and VimEditingEngine already
does).
This commit is contained in:
sin-ack 2021-04-26 20:29:05 +00:00 committed by Andreas Kling
parent 4e6a26cbd2
commit d6dc81874d
6 changed files with 72 additions and 68 deletions

View file

@ -39,14 +39,16 @@ bool EditingEngine::on_key(const KeyEvent& event)
}
}
if (event.ctrl()) {
move_to_previous_span(event);
m_editor->update_selection(event.shift());
move_to_previous_span();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
}
return true;
}
move_one_left(event);
m_editor->update_selection(event.shift());
move_one_left();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
@ -68,7 +70,8 @@ bool EditingEngine::on_key(const KeyEvent& event)
move_to_next_span(event);
return true;
}
move_one_right(event);
m_editor->update_selection(event.shift());
move_one_right();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
@ -77,6 +80,9 @@ bool EditingEngine::on_key(const KeyEvent& event)
}
if (event.key() == KeyCode::Key_Up) {
if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
m_editor->update_selection(event.shift());
}
move_one_up(event);
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
@ -86,6 +92,9 @@ bool EditingEngine::on_key(const KeyEvent& event)
}
if (event.key() == KeyCode::Key_Down) {
if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
m_editor->update_selection(event.shift());
}
move_one_down(event);
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
@ -96,14 +105,14 @@ bool EditingEngine::on_key(const KeyEvent& event)
if (event.key() == KeyCode::Key_Home) {
if (event.ctrl()) {
m_editor->toggle_selection_if_needed_for_event(event.shift());
move_to_first_line();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
}
} else {
move_to_line_beginning(event);
m_editor->update_selection(event.shift());
move_to_line_beginning();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
@ -114,14 +123,14 @@ bool EditingEngine::on_key(const KeyEvent& event)
if (event.key() == KeyCode::Key_End) {
if (event.ctrl()) {
m_editor->toggle_selection_if_needed_for_event(event.shift());
move_to_last_line();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
}
} else {
move_to_line_end(event);
m_editor->update_selection(event.shift());
move_to_line_end();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
@ -131,7 +140,10 @@ bool EditingEngine::on_key(const KeyEvent& event)
}
if (event.key() == KeyCode::Key_PageUp) {
move_page_up(event);
if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
m_editor->update_selection(event.shift());
}
move_page_up();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
@ -140,7 +152,10 @@ bool EditingEngine::on_key(const KeyEvent& event)
}
if (event.key() == KeyCode::Key_PageDown) {
move_page_down(event);
if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
m_editor->update_selection(event.shift());
}
move_page_down();
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
m_editor->did_update_selection();
@ -151,21 +166,19 @@ bool EditingEngine::on_key(const KeyEvent& event)
return false;
}
void EditingEngine::move_one_left(const KeyEvent& event)
void EditingEngine::move_one_left()
{
if (m_editor->cursor().column() > 0) {
int new_column = m_editor->cursor().column() - 1;
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(m_editor->cursor().line(), new_column);
} else if (m_editor->cursor().line() > 0) {
int new_line = m_editor->cursor().line() - 1;
int new_column = m_editor->lines()[new_line].length();
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_line, new_column);
}
}
void EditingEngine::move_one_right(const KeyEvent& event)
void EditingEngine::move_one_right()
{
int new_line = m_editor->cursor().line();
int new_column = m_editor->cursor().column();
@ -176,11 +189,10 @@ void EditingEngine::move_one_right(const KeyEvent& event)
new_line = m_editor->cursor().line() + 1;
new_column = 0;
}
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_line, new_column);
}
void EditingEngine::move_to_previous_span(const KeyEvent& event)
void EditingEngine::move_to_previous_span()
{
TextPosition new_cursor;
if (m_editor->document().has_spans()) {
@ -194,7 +206,6 @@ void EditingEngine::move_to_previous_span(const KeyEvent& event)
} else {
new_cursor = m_editor->document().first_word_break_before(m_editor->cursor(), true);
}
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_cursor);
}
@ -212,7 +223,6 @@ void EditingEngine::move_to_next_span(const KeyEvent& event)
} else {
new_cursor = m_editor->document().first_word_break_after(m_editor->cursor());
}
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_cursor);
if (event.shift() && m_editor->selection()->start().is_valid()) {
m_editor->selection()->set_end(m_editor->cursor());
@ -220,10 +230,9 @@ void EditingEngine::move_to_next_span(const KeyEvent& event)
}
}
void EditingEngine::move_to_line_beginning(const KeyEvent& event)
void EditingEngine::move_to_line_beginning()
{
TextPosition new_cursor;
m_editor->toggle_selection_if_needed_for_event(event.shift());
if (m_editor->is_wrapping_enabled()) {
// FIXME: Replicate the first_nonspace_column behavior in wrapping mode.
auto home_position = m_editor->cursor_content_rect().location().translated(-m_editor->width(), 0);
@ -239,7 +248,7 @@ void EditingEngine::move_to_line_beginning(const KeyEvent& event)
m_editor->set_cursor(new_cursor);
}
void EditingEngine::move_to_line_end(const KeyEvent& event)
void EditingEngine::move_to_line_end()
{
TextPosition new_cursor;
if (m_editor->is_wrapping_enabled()) {
@ -248,7 +257,6 @@ void EditingEngine::move_to_line_end(const KeyEvent& event)
} else {
new_cursor = { m_editor->cursor().line(), m_editor->current_line().length() };
}
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_cursor);
}
@ -268,7 +276,6 @@ void EditingEngine::move_one_up(const KeyEvent& event)
size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
new_cursor = { new_line, new_column };
}
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_cursor);
}
};
@ -289,12 +296,11 @@ void EditingEngine::move_one_down(const KeyEvent& event)
size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
new_cursor = { new_line, new_column };
}
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_cursor);
}
};
void EditingEngine::move_up(const KeyEvent& event, double page_height_factor)
void EditingEngine::move_up(double page_height_factor)
{
if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
@ -309,12 +315,11 @@ void EditingEngine::move_up(const KeyEvent& event, double page_height_factor)
size_t new_column = min(m_editor->cursor().column(), m_editor->line(new_line).length());
new_cursor = { new_line, new_column };
}
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_cursor);
}
};
void EditingEngine::move_down(const KeyEvent& event, double page_height_factor)
void EditingEngine::move_down(double page_height_factor)
{
if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
int pixels = (int)(m_editor->visible_content_rect().height() * page_height_factor);
@ -327,19 +332,18 @@ void EditingEngine::move_down(const KeyEvent& event, double page_height_factor)
size_t new_column = min(m_editor->cursor().column(), m_editor->lines()[new_line].length());
new_cursor = { new_line, new_column };
}
m_editor->toggle_selection_if_needed_for_event(event.shift());
m_editor->set_cursor(new_cursor);
};
}
void EditingEngine::move_page_up(const KeyEvent& event)
void EditingEngine::move_page_up()
{
move_up(event, 1);
move_up(1);
};
void EditingEngine::move_page_down(const KeyEvent& event)
void EditingEngine::move_page_down()
{
move_down(event, 1);
move_down(1);
};
void EditingEngine::move_to_first_line()