diff --git a/DevTools/HackStudio/HackStudioWidget.cpp b/DevTools/HackStudio/HackStudioWidget.cpp index 8c201c3904..c0f5eb9b0e 100644 --- a/DevTools/HackStudio/HackStudioWidget.cpp +++ b/DevTools/HackStudio/HackStudioWidget.cpp @@ -204,23 +204,34 @@ Vector HackStudioWidget::selected_file_names() const void HackStudioWidget::open_file(const String& filename) { - auto project_file = m_project->get_file(filename); + if (!currently_open_file().is_empty()) { + // Since the file is previously open, it should always be in m_open_files. + ASSERT(m_open_files.find(currently_open_file()) != m_open_files.end()); + auto previous_open_project_file = m_open_files.get(currently_open_file()).value(); - if (!project_file) { - if (auto it = m_open_files.find(filename); it != m_open_files.end()) { - project_file = it->value; - } else { - project_file = ProjectFile::construct_with_name(filename); - } + // 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); } - if (m_open_files.set(filename, *project_file) == AK::HashSetResult::InsertedNewEntry) { + RefPtr new_project_file = nullptr; + if (auto it = m_open_files.find(filename); it != m_open_files.end()) { + new_project_file = it->value; + } else { + new_project_file = m_project->get_file(filename); + if (!new_project_file) { + new_project_file = ProjectFile::construct_with_name(filename); + } + m_open_files.set(filename, *new_project_file); m_open_files_vector.append(filename); m_open_files_view->model()->update(); } - current_editor().set_document(const_cast(project_file->document())); + current_editor().set_document(const_cast(new_project_file->document())); current_editor().set_mode(GUI::TextEditor::Editable); + current_editor().horizontal_scrollbar().set_value(new_project_file->horizontal_scroll_value()); + current_editor().vertical_scrollbar().set_value(new_project_file->vertical_scroll_value()); if (filename.ends_with(".frm")) { set_edit_mode(EditMode::Form); diff --git a/DevTools/HackStudio/HackStudioWidget.h b/DevTools/HackStudio/HackStudioWidget.h index f6e930de3a..51c37fb56c 100644 --- a/DevTools/HackStudio/HackStudioWidget.h +++ b/DevTools/HackStudio/HackStudioWidget.h @@ -41,6 +41,7 @@ #include #include #include +#include namespace HackStudio { diff --git a/DevTools/HackStudio/ProjectFile.cpp b/DevTools/HackStudio/ProjectFile.cpp index e223520483..1c755f68de 100644 --- a/DevTools/HackStudio/ProjectFile.cpp +++ b/DevTools/HackStudio/ProjectFile.cpp @@ -51,4 +51,24 @@ const GUI::TextDocument& ProjectFile::document() const return *m_document; } +int ProjectFile::vertical_scroll_value() const +{ + return m_vertical_scroll_value; +} + +void ProjectFile::vertical_scroll_value(int vertical_scroll_value) +{ + m_vertical_scroll_value = vertical_scroll_value; +} + +int ProjectFile::horizontal_scroll_value() const +{ + return m_horizontal_scroll_value; +} + +void ProjectFile::horizontal_scroll_value(int horizontal_scroll_value) +{ + m_horizontal_scroll_value = horizontal_scroll_value; +} + } diff --git a/DevTools/HackStudio/ProjectFile.h b/DevTools/HackStudio/ProjectFile.h index 07a5294ea2..ce47544410 100644 --- a/DevTools/HackStudio/ProjectFile.h +++ b/DevTools/HackStudio/ProjectFile.h @@ -45,11 +45,18 @@ public: const GUI::TextDocument& document() const; + int vertical_scroll_value() const; + void vertical_scroll_value(int); + int horizontal_scroll_value() const; + void horizontal_scroll_value(int); + private: explicit ProjectFile(const String& name); String m_name; mutable RefPtr m_document; + int m_vertical_scroll_value{ 0 }; + int m_horizontal_scroll_value{ 0 }; }; }