From 7f2e1991cc91582a8cddf0092ca0fba375ce2035 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sat, 1 May 2021 13:04:19 +0300 Subject: [PATCH] HackStudio: Get rid of m_currently_open_file member It had the following FIXME: // FIXME: This doesn't seem compatible with multiple split editors In practice this member was used to get the filename of the currently active edtior. So we now get it directly from the currently active EditorWrapper. --- .../DevTools/HackStudio/EditorWrapper.cpp | 5 +++ Userland/DevTools/HackStudio/EditorWrapper.h | 3 ++ .../DevTools/HackStudio/HackStudioWidget.cpp | 36 +++++++++---------- .../DevTools/HackStudio/HackStudioWidget.h | 5 +-- Userland/DevTools/HackStudio/main.cpp | 2 +- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/Userland/DevTools/HackStudio/EditorWrapper.cpp b/Userland/DevTools/HackStudio/EditorWrapper.cpp index 79ea5c2deaf..111bc930143 100644 --- a/Userland/DevTools/HackStudio/EditorWrapper.cpp +++ b/Userland/DevTools/HackStudio/EditorWrapper.cpp @@ -80,5 +80,10 @@ void EditorWrapper::set_mode_non_displayable() editor().set_palette(palette); editor().document().set_text("The contents of this file could not be displayed. Is it a binary file?"); } +void EditorWrapper::set_filename(const String& filename) +{ + m_filename = filename; + m_filename_label->set_text(m_filename); +} } diff --git a/Userland/DevTools/HackStudio/EditorWrapper.h b/Userland/DevTools/HackStudio/EditorWrapper.h index 226c1030158..af0f9d03162 100644 --- a/Userland/DevTools/HackStudio/EditorWrapper.h +++ b/Userland/DevTools/HackStudio/EditorWrapper.h @@ -34,10 +34,13 @@ public: void set_mode_displayable(); void set_mode_non_displayable(); + void set_filename(const String&); + const String& filename() const {return m_filename;} private: EditorWrapper(); + String m_filename; RefPtr m_filename_label; RefPtr m_cursor_label; RefPtr m_editor; diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index c79b7979fcd..6bf740c92c4 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -215,15 +215,15 @@ bool HackStudioWidget::open_file(const String& full_filename) if (Core::File::is_directory(filename) || !Core::File::exists(filename)) return false; - if (!currently_open_file().is_empty()) { + if (!active_file().is_empty()) { // Since the file is previously open, it should always be in m_open_files. - VERIFY(m_open_files.find(currently_open_file()) != m_open_files.end()); - auto previous_open_project_file = m_open_files.get(currently_open_file()).value(); + VERIFY(m_open_files.find(active_file()) != m_open_files.end()); + auto previous_open_project_file = m_open_files.get(active_file()).value(); // Update the scrollbar values of the previous_open_project_file and save them to m_open_files. previous_open_project_file->vertical_scroll_value(current_editor().vertical_scrollbar().value()); previous_open_project_file->horizontal_scroll_value(current_editor().horizontal_scrollbar().value()); - m_open_files.set(currently_open_file(), previous_open_project_file); + m_open_files.set(active_file(), previous_open_project_file); } RefPtr new_project_file = nullptr; @@ -266,16 +266,14 @@ bool HackStudioWidget::open_file(const String& full_filename) set_edit_mode(EditMode::Text); } - m_currently_open_file = filename; - - String relative_file_path = m_currently_open_file; - if (m_currently_open_file.starts_with(m_project->root_path())) - relative_file_path = m_currently_open_file.substring(m_project->root_path().length() + 1); + String relative_file_path = filename; + if (filename.starts_with(m_project->root_path())) + relative_file_path = filename.substring(m_project->root_path().length() + 1); window()->set_title(String::formatted("{} - {} - Hack Studio", relative_file_path, m_project->name())); m_project_tree_view->update(); - current_editor_wrapper().filename_label().set_text(filename); + current_editor_wrapper().set_filename(filename); current_editor().set_focus(true); return true; @@ -551,10 +549,10 @@ NonnullRefPtr HackStudioWidget::create_open_action() NonnullRefPtr HackStudioWidget::create_save_action() { return GUI::CommonActions::make_save_action([&](auto&) { - if (m_currently_open_file.is_empty()) + if (active_file().is_empty()) return; - current_editor().write_to_file(m_currently_open_file); + current_editor().write_to_file(active_file()); if (m_git_widget->initialized()) m_git_widget->refresh(); @@ -716,16 +714,16 @@ String HackStudioWidget::get_project_executable_path() const void HackStudioWidget::build(TerminalWrapper& wrapper) { - if (m_currently_open_file.ends_with(".js")) - wrapper.run_command(String::formatted("js -A {}", m_currently_open_file)); + if (active_file().ends_with(".js")) + wrapper.run_command(String::formatted("js -A {}", active_file())); else wrapper.run_command("make"); } void HackStudioWidget::run(TerminalWrapper& wrapper) { - if (m_currently_open_file.ends_with(".js")) - wrapper.run_command(String::formatted("js {}", m_currently_open_file)); + if (active_file().ends_with(".js")) + wrapper.run_command(String::formatted("js {}", active_file())); else wrapper.run_command("make run"); } @@ -1104,14 +1102,12 @@ void HackStudioWidget::handle_external_file_deletion(const String& filepath) if (relative_editor_file_path == filepath) { if (m_open_files_vector.is_empty()) { editor.set_document(CodeDocument::create()); - editor_wrapper.filename_label().set_text(String { "Undefined" }); - m_currently_open_file = ""; + editor_wrapper.set_filename(""); } else { auto& first_path = m_open_files_vector[0]; auto& document = m_open_files.get(first_path).value()->code_document(); editor.set_document(document); - editor_wrapper.filename_label().set_text(first_path); - m_currently_open_file = first_path; + editor_wrapper.set_filename(first_path); } } } diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.h b/Userland/DevTools/HackStudio/HackStudioWidget.h index 312d138bf31..5d386712faa 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.h +++ b/Userland/DevTools/HackStudio/HackStudioWidget.h @@ -41,7 +41,7 @@ public: EditorWrapper& current_editor_wrapper(); void set_current_editor_wrapper(RefPtr); - String currently_open_file() const { return m_currently_open_file; } + const String& active_file() const { return m_current_editor_wrapper->filename(); } void initialize_menubar(GUI::Menubar&); Locator& locator() @@ -116,9 +116,6 @@ private: NonnullRefPtrVector m_all_editor_wrappers; RefPtr m_current_editor_wrapper; - // FIXME: This doesn't seem compatible with multiple split editors - String m_currently_open_file; - HashMap> m_open_files; HashMap> m_file_watchers; Vector m_open_files_vector; // NOTE: This contains the keys from m_open_files and m_file_watchers diff --git a/Userland/DevTools/HackStudio/main.cpp b/Userland/DevTools/HackStudio/main.cpp index 547d69ad7a8..0cb393c89b0 100644 --- a/Userland/DevTools/HackStudio/main.cpp +++ b/Userland/DevTools/HackStudio/main.cpp @@ -159,7 +159,7 @@ String currently_open_file() { if (!s_hack_studio_widget) return {}; - return s_hack_studio_widget->currently_open_file(); + return s_hack_studio_widget->active_file(); } void set_current_editor_wrapper(RefPtr wrapper)