summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/EditingEngine.cpp
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2022-05-17 20:43:22 +0200
committerAndreas Kling <kling@serenityos.org>2022-07-15 12:33:59 +0200
commitfcb1d2cfb7d4e712a13ca148b0017bfbfeef8279 (patch)
tree87399ea981037f98deb7adfb8d713b2f4cea786c /Userland/Libraries/LibGUI/EditingEngine.cpp
parent4d93fb4789f8a1b8d98c6159304acf72c9f5e3aa (diff)
downloadserenity-fcb1d2cfb7d4e712a13ca148b0017bfbfeef8279.zip
LibGUI: Fix weird behavior when using Ctrl+Shift+[Up,Down] in TextEditor
Those inconveniences come from cursor and selection not being saved during the switch.
Diffstat (limited to 'Userland/Libraries/LibGUI/EditingEngine.cpp')
-rw-r--r--Userland/Libraries/LibGUI/EditingEngine.cpp59
1 files changed, 29 insertions, 30 deletions
diff --git a/Userland/Libraries/LibGUI/EditingEngine.cpp b/Userland/Libraries/LibGUI/EditingEngine.cpp
index 46d43eb90c..5f2d6cfc0d 100644
--- a/Userland/Libraries/LibGUI/EditingEngine.cpp
+++ b/Userland/Libraries/LibGUI/EditingEngine.cpp
@@ -81,28 +81,16 @@ bool EditingEngine::on_key(KeyEvent const& event)
return true;
}
- 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());
- m_editor->did_update_selection();
- }
- return true;
- }
+ if (event.key() == KeyCode::Key_Up || event.key() == KeyCode::Key_Down) {
+ auto const direction = key_code_to_vertical_direction(event.key());
- if (event.key() == KeyCode::Key_Down) {
- if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
+ bool const condition_for_up = direction == VerticalDirection::Up && m_editor->cursor().line() > 0;
+ bool const condition_for_down = direction == VerticalDirection::Down && m_editor->cursor().line() < (m_editor->line_count() - 1);
+
+ if (condition_for_up || condition_for_down || 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());
- m_editor->did_update_selection();
- }
- return true;
+
+ move_one_helper(event, direction);
}
if (event.key() == KeyCode::Key_Home) {
@@ -258,12 +246,21 @@ void EditingEngine::move_to_logical_line_end()
m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().length() });
}
-void EditingEngine::move_one_up(KeyEvent const& event)
+void EditingEngine::move_one_helper(KeyEvent const& event, VerticalDirection direction)
+{
+ auto const result = direction == VerticalDirection::Up ? move_one_up(event) : move_one_down(event);
+ if (result != DidMoveALine::Yes && event.shift() && m_editor->selection().start().is_valid()) {
+ m_editor->selection().set_end(m_editor->cursor());
+ m_editor->did_update_selection();
+ }
+}
+
+EditingEngine::DidMoveALine EditingEngine::move_one_up(KeyEvent const& event)
{
if (m_editor->cursor().line() > 0 || m_editor->is_wrapping_enabled()) {
if (event.ctrl() && event.shift()) {
move_selected_lines_up();
- return;
+ return DidMoveALine::Yes;
}
TextPosition new_cursor;
if (m_editor->is_wrapping_enabled()) {
@@ -276,14 +273,15 @@ void EditingEngine::move_one_up(KeyEvent const& event)
}
m_editor->set_cursor(new_cursor);
}
+ return DidMoveALine::No;
};
-void EditingEngine::move_one_down(KeyEvent const& event)
+EditingEngine::DidMoveALine EditingEngine::move_one_down(KeyEvent const& event)
{
if (m_editor->cursor().line() < (m_editor->line_count() - 1) || m_editor->is_wrapping_enabled()) {
if (event.ctrl() && event.shift()) {
move_selected_lines_down();
- return;
+ return DidMoveALine::Yes;
}
TextPosition new_cursor;
if (m_editor->is_wrapping_enabled()) {
@@ -296,6 +294,7 @@ void EditingEngine::move_one_down(KeyEvent const& event)
}
m_editor->set_cursor(new_cursor);
}
+ return DidMoveALine::No;
};
void EditingEngine::move_up(double page_height_factor)
@@ -381,11 +380,11 @@ void EditingEngine::move_selected_lines_up()
auto& lines = m_editor->document().lines();
lines.insert((int)last_line, lines.take((int)first_line - 1));
- m_editor->set_cursor({ first_line - 1, 0 });
+ m_editor->set_cursor({ m_editor->cursor().line() - 1, m_editor->cursor().column() });
if (m_editor->has_selection()) {
- m_editor->selection().set_start({ first_line - 1, 0 });
- m_editor->selection().set_end({ last_line - 1, m_editor->line(last_line - 1).length() });
+ m_editor->selection().start().set_line(m_editor->selection().start().line() - 1);
+ m_editor->selection().end().set_line(m_editor->selection().end().line() - 1);
}
m_editor->did_change();
@@ -406,11 +405,11 @@ void EditingEngine::move_selected_lines_down()
return;
lines.insert((int)first_line, lines.take((int)last_line + 1));
- m_editor->set_cursor({ first_line + 1, 0 });
+ m_editor->set_cursor({ m_editor->cursor().line() + 1, m_editor->cursor().column() });
if (m_editor->has_selection()) {
- m_editor->selection().set_start({ first_line + 1, 0 });
- m_editor->selection().set_end({ last_line + 1, m_editor->line(last_line + 1).length() });
+ m_editor->selection().start().set_line(m_editor->selection().start().line() + 1);
+ m_editor->selection().end().set_line(m_editor->selection().end().line() + 1);
}
m_editor->did_change();