From 28e4e351cbb716f2ac42d7254be439e967b44d7c Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 25 Jan 2024 12:39:52 +0000 Subject: [PATCH] HackStudio: Extract and rename the Locator's suggestions model No code changes besides moving and renaming it. --- Userland/DevTools/HackStudio/CMakeLists.txt | 1 + .../DevTools/HackStudio/DeclarationsModel.cpp | 60 +++++++++++++ .../DevTools/HackStudio/DeclarationsModel.h | 51 +++++++++++ Userland/DevTools/HackStudio/Locator.cpp | 90 ++----------------- 4 files changed, 118 insertions(+), 84 deletions(-) create mode 100644 Userland/DevTools/HackStudio/DeclarationsModel.cpp create mode 100644 Userland/DevTools/HackStudio/DeclarationsModel.h diff --git a/Userland/DevTools/HackStudio/CMakeLists.txt b/Userland/DevTools/HackStudio/CMakeLists.txt index 79fe47384a..949402cb12 100644 --- a/Userland/DevTools/HackStudio/CMakeLists.txt +++ b/Userland/DevTools/HackStudio/CMakeLists.txt @@ -21,6 +21,7 @@ set(SOURCES Debugger/DisassemblyWidget.cpp Debugger/RegistersModel.cpp Debugger/VariablesModel.cpp + DeclarationsModel.cpp Dialogs/Git/GitCommitDialog.cpp Dialogs/NewProjectDialog.cpp Dialogs/ProjectTemplatesModel.cpp diff --git a/Userland/DevTools/HackStudio/DeclarationsModel.cpp b/Userland/DevTools/HackStudio/DeclarationsModel.cpp new file mode 100644 index 0000000000..47e5e9286d --- /dev/null +++ b/Userland/DevTools/HackStudio/DeclarationsModel.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2024, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "DeclarationsModel.h" +#include "ProjectDeclarations.h" + +namespace HackStudio { + +DeclarationsModel::Suggestion DeclarationsModel::Suggestion::create_filename(ByteString const& filename) +{ + DeclarationsModel::Suggestion s; + s.as_filename = filename; + return s; +} +DeclarationsModel::Suggestion DeclarationsModel::Suggestion::create_symbol_declaration(CodeComprehension::Declaration const& decl) +{ + DeclarationsModel::Suggestion s; + s.as_symbol_declaration = decl; + return s; +} + +GUI::Variant DeclarationsModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const +{ + auto& suggestion = m_suggestions.at(index.row()); + if (role != GUI::ModelRole::Display) + return {}; + + if (suggestion.is_filename()) { + if (index.column() == Column::Name) + return suggestion.as_filename.value(); + if (index.column() == Column::Filename) + return ""; + if (index.column() == Column::Icon) + return GUI::FileIconProvider::icon_for_path(suggestion.as_filename.value()); + } + if (suggestion.is_symbol_declaration()) { + if (index.column() == Column::Name) { + if (!suggestion.as_symbol_declaration.value().scope.is_empty()) + return suggestion.as_symbol_declaration.value().name; + return ByteString::formatted("{}::{}", suggestion.as_symbol_declaration.value().scope, suggestion.as_symbol_declaration.value().name); + } + if (index.column() == Column::Filename) + return suggestion.as_symbol_declaration.value().position.file; + if (index.column() == Column::Icon) { + auto icon = ProjectDeclarations::get_icon_for(suggestion.as_symbol_declaration.value().type); + if (icon.has_value()) + return icon.value(); + return {}; + } + } + + return {}; +} + +} diff --git a/Userland/DevTools/HackStudio/DeclarationsModel.h b/Userland/DevTools/HackStudio/DeclarationsModel.h new file mode 100644 index 0000000000..2063bbdd98 --- /dev/null +++ b/Userland/DevTools/HackStudio/DeclarationsModel.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2024, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace HackStudio { + +class DeclarationsModel final : public GUI::Model { +public: + struct Suggestion { + static Suggestion create_filename(ByteString const& filename); + static Suggestion create_symbol_declaration(CodeComprehension::Declaration const&); + + bool is_filename() const { return as_filename.has_value(); } + bool is_symbol_declaration() const { return as_symbol_declaration.has_value(); } + + Optional as_filename; + Optional as_symbol_declaration; + }; + + explicit DeclarationsModel(Vector&& suggestions) + : m_suggestions(move(suggestions)) + { + } + + enum Column { + Icon, + Name, + Filename, + __Column_Count, + }; + virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return m_suggestions.size(); } + virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Column_Count; } + virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override; + + Vector const& suggestions() const { return m_suggestions; } + +private: + Vector m_suggestions; +}; + +} diff --git a/Userland/DevTools/HackStudio/Locator.cpp b/Userland/DevTools/HackStudio/Locator.cpp index 6451f4c714..3287693514 100644 --- a/Userland/DevTools/HackStudio/Locator.cpp +++ b/Userland/DevTools/HackStudio/Locator.cpp @@ -6,95 +6,17 @@ */ #include "Locator.h" +#include "DeclarationsModel.h" #include "HackStudio.h" #include "Project.h" #include "ProjectDeclarations.h" -#include #include -#include #include #include #include namespace HackStudio { -class LocatorSuggestionModel final : public GUI::Model { -public: - struct Suggestion { - static Suggestion create_filename(ByteString const& filename); - static Suggestion create_symbol_declaration(CodeComprehension::Declaration const&); - - bool is_filename() const { return as_filename.has_value(); } - bool is_symbol_declaration() const { return as_symbol_declaration.has_value(); } - - Optional as_filename; - Optional as_symbol_declaration; - }; - - explicit LocatorSuggestionModel(Vector&& suggestions) - : m_suggestions(move(suggestions)) - { - } - - enum Column { - Icon, - Name, - Filename, - __Column_Count, - }; - virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); } - virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; } - virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override - { - auto& suggestion = m_suggestions.at(index.row()); - if (role != GUI::ModelRole::Display) - return {}; - - if (suggestion.is_filename()) { - if (index.column() == Column::Name) - return suggestion.as_filename.value(); - if (index.column() == Column::Filename) - return ""; - if (index.column() == Column::Icon) - return GUI::FileIconProvider::icon_for_path(suggestion.as_filename.value()); - } - if (suggestion.is_symbol_declaration()) { - if (index.column() == Column::Name) { - if (!suggestion.as_symbol_declaration.value().scope.is_empty()) - return suggestion.as_symbol_declaration.value().name; - return ByteString::formatted("{}::{}", suggestion.as_symbol_declaration.value().scope, suggestion.as_symbol_declaration.value().name); - } - if (index.column() == Column::Filename) - return suggestion.as_symbol_declaration.value().position.file; - if (index.column() == Column::Icon) { - auto icon = ProjectDeclarations::get_icon_for(suggestion.as_symbol_declaration.value().type); - if (icon.has_value()) - return icon.value(); - return {}; - } - } - return {}; - } - - Vector const& suggestions() const { return m_suggestions; } - -private: - Vector m_suggestions; -}; - -LocatorSuggestionModel::Suggestion LocatorSuggestionModel::Suggestion::create_filename(ByteString const& filename) -{ - LocatorSuggestionModel::Suggestion s; - s.as_filename = filename; - return s; -} -LocatorSuggestionModel::Suggestion LocatorSuggestionModel::Suggestion::create_symbol_declaration(CodeComprehension::Declaration const& decl) -{ - LocatorSuggestionModel::Suggestion s; - s.as_symbol_declaration = decl; - return s; -} - Locator::Locator(Core::EventReceiver* parent) { set_layout(); @@ -159,7 +81,7 @@ Locator::Locator(Core::EventReceiver* parent) void Locator::open_suggestion(const GUI::ModelIndex& index) { - auto& model = reinterpret_cast(*m_suggestion_view->model()); + auto& model = reinterpret_cast(*m_suggestion_view->model()); auto suggestion = model.suggestions()[index.row()]; if (suggestion.is_filename()) { auto filename = suggestion.as_filename.value(); @@ -190,20 +112,20 @@ void Locator::close() void Locator::update_suggestions() { auto typed_text = m_textbox->text(); - Vector suggestions; + Vector suggestions; project().for_each_text_file([&](auto& file) { if (file.name().contains(typed_text, CaseSensitivity::CaseInsensitive)) - suggestions.append(LocatorSuggestionModel::Suggestion::create_filename(file.name())); + suggestions.append(DeclarationsModel::Suggestion::create_filename(file.name())); }); ProjectDeclarations::the().for_each_declared_symbol([&suggestions, &typed_text](auto& decl) { if (decl.name.contains(typed_text, CaseSensitivity::CaseInsensitive) || decl.scope.contains(typed_text, CaseSensitivity::CaseInsensitive)) - suggestions.append((LocatorSuggestionModel::Suggestion::create_symbol_declaration(decl))); + suggestions.append((DeclarationsModel::Suggestion::create_symbol_declaration(decl))); }); bool has_suggestions = !suggestions.is_empty(); - m_suggestion_view->set_model(adopt_ref(*new LocatorSuggestionModel(move(suggestions)))); + m_suggestion_view->set_model(adopt_ref(*new DeclarationsModel(move(suggestions)))); if (!has_suggestions) m_suggestion_view->selection().clear();