1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00:00

HackStudio: Keep the DeclarationsModel around, and use a filtering proxy

Rather than construct a new DeclarationsModel each time the user types
something in the Locator, keep a single one around permanently in the
ProjectDeclarations, and then use a FilteringProxyModel over it for the
suggestions.
This commit is contained in:
Sam Atkins 2024-01-25 14:19:15 +00:00 committed by Sam Atkins
parent e72b14ef1d
commit 85101c6626
6 changed files with 78 additions and 22 deletions

View file

@ -77,12 +77,15 @@ Locator::Locator(Core::EventReceiver* parent)
m_suggestion_view->on_activation = [this](auto& index) {
open_suggestion(index);
};
m_model = GUI::FilteringProxyModel::create(ProjectDeclarations::the().declarations_model()).release_value_but_fixme_should_propagate_errors();
m_suggestion_view->set_model(m_model);
}
void Locator::open_suggestion(const GUI::ModelIndex& index)
{
auto& model = reinterpret_cast<DeclarationsModel&>(*m_suggestion_view->model());
auto suggestion = model.declarations()[index.row()];
auto original_index = m_model->map(index);
auto suggestion = ProjectDeclarations::the().declarations_model().declarations()[original_index.row()];
if (suggestion.is_filename()) {
auto filename = suggestion.as_filename.value();
open_file(filename);
@ -111,23 +114,9 @@ void Locator::close()
void Locator::update_suggestions()
{
auto typed_text = m_textbox->text();
Vector<Declaration> suggestions;
project().for_each_text_file([&](auto& file) {
if (file.name().contains(typed_text, CaseSensitivity::CaseInsensitive))
suggestions.append(Declaration::create_filename(file.name()));
});
m_model->set_filter_term(m_textbox->text());
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((Declaration::create_symbol_declaration(decl)));
});
bool has_suggestions = !suggestions.is_empty();
m_suggestion_view->set_model(adopt_ref(*new DeclarationsModel(move(suggestions))));
if (!has_suggestions)
if (m_model->row_count() == 0)
m_suggestion_view->selection().clear();
else
m_suggestion_view->selection().set(m_suggestion_view->model()->index(0));