From e0be8a3f598d43e7ce6df6ff2a1eb5d7ac817177 Mon Sep 17 00:00:00 2001 From: FalseHonesty Date: Thu, 15 Apr 2021 12:42:40 -0400 Subject: HackStudio: Don't crash when invalid file is requested to be opened Previously, if the running debugger asked for HackStudio to open an invalid file, it would crash trying to switch to it. Now it will just continue without switching the editor. --- Userland/DevTools/HackStudio/HackStudioWidget.cpp | 15 +++++++++------ Userland/DevTools/HackStudio/HackStudioWidget.h | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'Userland') diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index b8a6deac45..3382a5dfe2 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -223,15 +223,15 @@ Vector HackStudioWidget::selected_file_paths() const return files; } -void HackStudioWidget::open_file(const String& full_filename) +bool HackStudioWidget::open_file(const String& full_filename) { String filename = full_filename; if (full_filename.starts_with(project().root_path())) { filename = LexicalPath::relative_path(full_filename, project().root_path()); } dbgln("HackStudio is opening {}", filename); - if (Core::File::is_directory(filename)) - return; + if (Core::File::is_directory(filename) || !Core::File::exists(filename)) + return false; if (!currently_open_file().is_empty()) { // Since the file is previously open, it should always be in m_open_files. @@ -282,6 +282,7 @@ void HackStudioWidget::open_file(const String& full_filename) current_editor_wrapper().filename_label().set_text(filename); current_editor().set_focus(true); + return true; } EditorWrapper& HackStudioWidget::current_editor_wrapper() @@ -647,7 +648,8 @@ void HackStudioWidget::initialize_debugger() make( [this, source_position, ®s](auto&) { m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path); - m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1); + if (m_current_editor_in_execution) + m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1); m_debug_info_widget->update_state(*Debugger::the().session(), regs); m_debug_info_widget->set_debug_actions_enabled(true); m_disassembly_widget->update_state(*Debugger::the().session(), regs); @@ -689,7 +691,7 @@ String HackStudioWidget::get_full_path_of_serenity_source(const String& file) return String::formatted("{}/{}", serenity_sources_base, relative_path_builder.to_string()); } -NonnullRefPtr HackStudioWidget::get_editor_of_file(const String& file_name) +RefPtr HackStudioWidget::get_editor_of_file(const String& file_name) { String file_path = file_name; @@ -700,7 +702,8 @@ NonnullRefPtr HackStudioWidget::get_editor_of_file(const String& file_path = get_full_path_of_serenity_source(file_name); } - open_file(file_path); + if (!open_file(file_path)) + return nullptr; return current_editor_wrapper(); } diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.h b/Userland/DevTools/HackStudio/HackStudioWidget.h index 40dd554108..6f95e3bb05 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.h +++ b/Userland/DevTools/HackStudio/HackStudioWidget.h @@ -53,7 +53,7 @@ class HackStudioWidget : public GUI::Widget { public: virtual ~HackStudioWidget() override; - void open_file(const String& filename); + bool open_file(const String& filename); void update_actions(); Project& project(); @@ -106,7 +106,7 @@ private: NonnullRefPtr create_set_autocomplete_mode_action(); void add_new_editor(GUI::Widget& parent); - NonnullRefPtr get_editor_of_file(const String& file_name); + RefPtr get_editor_of_file(const String& file_name); String get_project_executable_path() const; void on_action_tab_change(); -- cgit v1.2.3