HackStudio: Extract and rename the Locator's suggestions model

No code changes besides moving and renaming it.
This commit is contained in:
Sam Atkins 2024-01-25 12:39:52 +00:00 committed by Sam Atkins
parent a805cbc5ab
commit 28e4e351cb
4 changed files with 118 additions and 84 deletions

View file

@ -21,6 +21,7 @@ set(SOURCES
Debugger/DisassemblyWidget.cpp Debugger/DisassemblyWidget.cpp
Debugger/RegistersModel.cpp Debugger/RegistersModel.cpp
Debugger/VariablesModel.cpp Debugger/VariablesModel.cpp
DeclarationsModel.cpp
Dialogs/Git/GitCommitDialog.cpp Dialogs/Git/GitCommitDialog.cpp
Dialogs/NewProjectDialog.cpp Dialogs/NewProjectDialog.cpp
Dialogs/ProjectTemplatesModel.cpp Dialogs/ProjectTemplatesModel.cpp

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
*
* 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 {};
}
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibCodeComprehension/Types.h>
#include <LibGUI/FileIconProvider.h>
#include <LibGUI/Model.h>
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<ByteString> as_filename;
Optional<CodeComprehension::Declaration> as_symbol_declaration;
};
explicit DeclarationsModel(Vector<Suggestion>&& 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<Suggestion> const& suggestions() const { return m_suggestions; }
private:
Vector<Suggestion> m_suggestions;
};
}

View file

@ -6,95 +6,17 @@
*/ */
#include "Locator.h" #include "Locator.h"
#include "DeclarationsModel.h"
#include "HackStudio.h" #include "HackStudio.h"
#include "Project.h" #include "Project.h"
#include "ProjectDeclarations.h" #include "ProjectDeclarations.h"
#include <LibGUI/AutocompleteProvider.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
#include <LibGUI/FileIconProvider.h>
#include <LibGUI/TableView.h> #include <LibGUI/TableView.h>
#include <LibGUI/TextBox.h> #include <LibGUI/TextBox.h>
#include <LibGUI/Window.h> #include <LibGUI/Window.h>
namespace HackStudio { 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<ByteString> as_filename;
Optional<CodeComprehension::Declaration> as_symbol_declaration;
};
explicit LocatorSuggestionModel(Vector<Suggestion>&& 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<Suggestion> const& suggestions() const { return m_suggestions; }
private:
Vector<Suggestion> 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) Locator::Locator(Core::EventReceiver* parent)
{ {
set_layout<GUI::VerticalBoxLayout>(); set_layout<GUI::VerticalBoxLayout>();
@ -159,7 +81,7 @@ Locator::Locator(Core::EventReceiver* parent)
void Locator::open_suggestion(const GUI::ModelIndex& index) void Locator::open_suggestion(const GUI::ModelIndex& index)
{ {
auto& model = reinterpret_cast<LocatorSuggestionModel&>(*m_suggestion_view->model()); auto& model = reinterpret_cast<DeclarationsModel&>(*m_suggestion_view->model());
auto suggestion = model.suggestions()[index.row()]; auto suggestion = model.suggestions()[index.row()];
if (suggestion.is_filename()) { if (suggestion.is_filename()) {
auto filename = suggestion.as_filename.value(); auto filename = suggestion.as_filename.value();
@ -190,20 +112,20 @@ void Locator::close()
void Locator::update_suggestions() void Locator::update_suggestions()
{ {
auto typed_text = m_textbox->text(); auto typed_text = m_textbox->text();
Vector<LocatorSuggestionModel::Suggestion> suggestions; Vector<DeclarationsModel::Suggestion> suggestions;
project().for_each_text_file([&](auto& file) { project().for_each_text_file([&](auto& file) {
if (file.name().contains(typed_text, CaseSensitivity::CaseInsensitive)) 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) { 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)) 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(); 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) if (!has_suggestions)
m_suggestion_view->selection().clear(); m_suggestion_view->selection().clear();