summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorCaoimhe <caoimhebyrne06@gmail.com>2023-05-31 18:11:46 +0100
committerAndreas Kling <kling@serenityos.org>2023-06-01 06:26:40 +0200
commit617edafbf2f3201744e523015c11b317205ef76d (patch)
treebe702a0f0f175e82efec0ae8cb8531d275c76ae3 /Userland
parent75c8e07cc386a41ee03c3d0a884deec3323a491c (diff)
downloadserenity-617edafbf2f3201744e523015c11b317205ef76d.zip
LibGUI: Add support for jumping to a line and column in TextEditor
We had support for going to a specific line before, but now we support jumping around using the `line:column` format :^)
Diffstat (limited to 'Userland')
-rw-r--r--Userland/DevTools/GMLPlayground/MainWidget.cpp2
-rw-r--r--Userland/Libraries/LibGUI/TextEditor.cpp30
-rw-r--r--Userland/Libraries/LibGUI/TextEditor.h4
3 files changed, 29 insertions, 7 deletions
diff --git a/Userland/DevTools/GMLPlayground/MainWidget.cpp b/Userland/DevTools/GMLPlayground/MainWidget.cpp
index bfff84decc..5b6724ed33 100644
--- a/Userland/DevTools/GMLPlayground/MainWidget.cpp
+++ b/Userland/DevTools/GMLPlayground/MainWidget.cpp
@@ -200,7 +200,7 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
TRY(edit_menu->try_add_action(m_editor->paste_action()));
TRY(edit_menu->try_add_separator());
TRY(edit_menu->try_add_action(m_editor->select_all_action()));
- TRY(edit_menu->try_add_action(m_editor->go_to_line_action()));
+ TRY(edit_menu->try_add_action(m_editor->go_to_line_or_column_action()));
TRY(edit_menu->try_add_separator());
auto format_gml_action = GUI::Action::create("&Format GML", { Mod_Ctrl | Mod_Shift, Key_I }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/reformat.png"sv)), [&](auto&) {
diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp
index d958cf9262..4b875bbd00 100644
--- a/Userland/Libraries/LibGUI/TextEditor.cpp
+++ b/Userland/Libraries/LibGUI/TextEditor.cpp
@@ -95,10 +95,32 @@ void TextEditor::create_actions()
m_paste_action = CommonActions::make_paste_action([&](auto&) { paste(); }, this);
m_paste_action->set_enabled(is_editable() && Clipboard::the().fetch_mime_type().starts_with("text/"sv));
if (is_multi_line()) {
- m_go_to_line_action = Action::create(
- "Go to Line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-to.png"sv).release_value_but_fixme_should_propagate_errors(), [this](auto&) {
+ m_go_to_line_or_column_action = Action::create(
+ "Go to Line/Column...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-to.png"sv).release_value_but_fixme_should_propagate_errors(), [this](auto&) {
String value;
- if (InputBox::show(window(), value, "Line:"sv, "Go to Line"sv) == InputBox::ExecResult::OK) {
+ if (InputBox::show(window(), value, "Enter the line, or line:column:"sv, "Go to Line/Column"sv) == InputBox::ExecResult::OK) {
+ // If there is a `:` in the string, the format is expected to be `line:column`. E.g: `123:45`
+ if (value.contains(':')) {
+ auto line_and_column_or_error = value.split(':');
+ if (line_and_column_or_error.is_error()) {
+ return;
+ }
+
+ auto line_and_column = line_and_column_or_error.value();
+ if (line_and_column.size() != 2) {
+ return;
+ }
+
+ auto line_target = AK::StringUtils::convert_to_uint(line_and_column.at(0));
+ auto column_target = AK::StringUtils::convert_to_uint(line_and_column.at(1));
+ if (line_target.has_value() && column_target.has_value()) {
+ set_cursor_and_focus_line(line_target.value() - 1, column_target.value());
+ }
+
+ return;
+ }
+
+ // If there is no `:` in the string, we just treat the integer as the line
auto line_target = AK::StringUtils::convert_to_uint(value.bytes_as_string_view());
if (line_target.has_value()) {
set_cursor_and_focus_line(line_target.value() - 1, 0);
@@ -1965,7 +1987,7 @@ void TextEditor::context_menu_event(ContextMenuEvent& event)
m_context_menu->add_action(insert_emoji_action());
if (is_multi_line()) {
m_context_menu->add_separator();
- m_context_menu->add_action(go_to_line_action());
+ m_context_menu->add_action(go_to_line_or_column_action());
}
if (!m_custom_context_menu_actions.is_empty()) {
m_context_menu->add_separator();
diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h
index 8ffd9e037d..f03a92837a 100644
--- a/Userland/Libraries/LibGUI/TextEditor.h
+++ b/Userland/Libraries/LibGUI/TextEditor.h
@@ -190,7 +190,7 @@ public:
Action& cut_action() { return *m_cut_action; }
Action& copy_action() { return *m_copy_action; }
Action& paste_action() { return *m_paste_action; }
- Action& go_to_line_action() { return *m_go_to_line_action; }
+ Action& go_to_line_or_column_action() { return *m_go_to_line_or_column_action; }
Action& select_all_action() { return *m_select_all_action; }
Action& insert_emoji_action() { return *m_insert_emoji_action; }
@@ -426,7 +426,7 @@ private:
RefPtr<Action> m_cut_action;
RefPtr<Action> m_copy_action;
RefPtr<Action> m_paste_action;
- RefPtr<Action> m_go_to_line_action;
+ RefPtr<Action> m_go_to_line_or_column_action;
RefPtr<Action> m_select_all_action;
RefPtr<Action> m_insert_emoji_action;
Core::ElapsedTimer m_triple_click_timer;