From f07ac8f20ab9005852162d1f1be33d74a39bf3d1 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 27 May 2023 14:18:19 +0200 Subject: [PATCH] HackStudio: Prefer FileSystem over DeprecatedFile This also straightens out the logic to determine the project_path. Instead of calling realpath on potentially-null strings and sometimes not even reading the result, we now only make these calls when required, and properly handle any error. --- .../DevTools/HackStudio/HackStudioWidget.cpp | 2 +- .../DevTools/HackStudio/HackStudioWidget.h | 2 +- Userland/DevTools/HackStudio/main.cpp | 28 +++++++++---------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index d4eb125ec5..f40943be63 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -1868,7 +1868,7 @@ void HackStudioWidget::change_editor_font(RefPtr font) Config::write_i32("HackStudio"sv, "EditorFont"sv, "Size"sv, m_editor_font->presentation_size()); } -void HackStudioWidget::open_coredump(DeprecatedString const& coredump_path) +void HackStudioWidget::open_coredump(StringView coredump_path) { open_project("/usr/src/serenity"); m_mode = Mode::Coredump; diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.h b/Userland/DevTools/HackStudio/HackStudioWidget.h index d54b375472..04fcba54d8 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.h +++ b/Userland/DevTools/HackStudio/HackStudioWidget.h @@ -75,7 +75,7 @@ public: Coredump }; - void open_coredump(DeprecatedString const& coredump_path); + void open_coredump(StringView coredump_path); void debug_process(pid_t pid); void for_each_open_file(Function); bool semantic_syntax_highlighting_is_enabled() const; diff --git a/Userland/DevTools/HackStudio/main.cpp b/Userland/DevTools/HackStudio/main.cpp index b170b3ea57..3757a5dc7b 100644 --- a/Userland/DevTools/HackStudio/main.cpp +++ b/Userland/DevTools/HackStudio/main.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -35,7 +34,7 @@ static bool make_is_available(); static ErrorOr notify_make_not_available(); static void update_path_environment_variable(); static Optional last_opened_project_path(); -static ErrorOr> create_hack_studio_widget(bool mode_coredump, DeprecatedString const& path, pid_t pid_to_debug); +static ErrorOr> create_hack_studio_widget(bool mode_coredump, StringView path, pid_t pid_to_debug); ErrorOr serenity_main(Main::Arguments arguments) { @@ -64,8 +63,7 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(pid_to_debug, "Attach debugger to running process", "pid", 'p', "PID"); args_parser.parse(arguments); - auto absolute_path_argument = Core::DeprecatedFile::real_path_for(path_argument); - auto hack_studio_widget = TRY(create_hack_studio_widget(mode_coredump, absolute_path_argument, pid_to_debug)); + auto hack_studio_widget = TRY(create_hack_studio_widget(mode_coredump, path_argument, pid_to_debug)); window->set_main_widget(hack_studio_widget); s_hack_studio_widget = hack_studio_widget; @@ -84,7 +82,7 @@ ErrorOr serenity_main(Main::Arguments arguments) hack_studio_widget->update_actions(); if (mode_coredump) - hack_studio_widget->open_coredump(absolute_path_argument); + hack_studio_widget->open_coredump(path_argument); if (pid_to_debug != -1) hack_studio_widget->debug_process(pid_to_debug); @@ -211,18 +209,18 @@ bool semantic_syntax_highlighting_is_enabled() } -static ErrorOr> create_hack_studio_widget(bool mode_coredump, DeprecatedString const& absolute_path_argument, pid_t pid_to_debug) +static ErrorOr> create_hack_studio_widget(bool mode_coredump, StringView raw_path_argument, pid_t pid_to_debug) { - auto project_path = Core::DeprecatedFile::real_path_for("."); - if (!mode_coredump) { - if (!absolute_path_argument.is_null()) - project_path = absolute_path_argument; - else if (auto last_path = last_opened_project_path(); last_path.has_value()) - project_path = last_path.release_value(); - } - - if (pid_to_debug != -1) + DeprecatedString project_path; + if (pid_to_debug != -1 || mode_coredump) project_path = "/usr/src/serenity"; + else if (!raw_path_argument.is_null()) + // FIXME: Validation is unintentional, and should be removed when migrating to String. + project_path = TRY(DeprecatedString::from_utf8(raw_path_argument)); + else if (auto last_path = last_opened_project_path(); last_path.has_value()) + project_path = last_path.release_value(); + else + project_path = TRY(FileSystem::real_path("."sv)).to_deprecated_string(); return HackStudioWidget::create(project_path); }