HackStudio: Fuzzy-match Locator suggestions

This also sorts them to put better results first. Currently the
fuzzy-match algorithm gets a little confused, but as that improves, so
will this.
This commit is contained in:
Sam Atkins 2024-01-25 14:51:24 +00:00 committed by Sam Atkins
parent 85101c6626
commit 4b1c7533f5
Notes: sideshowbarker 2024-07-17 00:59:43 +09:00
2 changed files with 18 additions and 11 deletions

View File

@ -8,9 +8,17 @@
#include "DeclarationsModel.h"
#include "ProjectDeclarations.h"
#include <AK/FuzzyMatch.h>
namespace HackStudio {
static String qualified_symbol_name(CodeComprehension::Declaration symbol)
{
if (!symbol.scope.is_empty())
return MUST(String::from_byte_string(symbol.name));
return MUST(String::formatted("{}::{}", symbol.scope, symbol.name));
}
Declaration Declaration::create_filename(ByteString const& filename)
{
Declaration s;
@ -39,11 +47,8 @@ GUI::Variant DeclarationsModel::data(GUI::ModelIndex const& index, GUI::ModelRol
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::Name)
return qualified_symbol_name(suggestion.as_symbol_declaration.value());
if (index.column() == Column::Filename)
return suggestion.as_symbol_declaration.value().position.file;
if (index.column() == Column::Icon) {
@ -68,14 +73,15 @@ GUI::Model::MatchResult DeclarationsModel::data_matches(GUI::ModelIndex const& i
auto& declaration = m_declarations[index.row()];
if (declaration.is_filename()) {
if (declaration.as_filename->contains(needle, CaseSensitivity::CaseInsensitive))
return { TriState::True };
if (auto match_result = fuzzy_match(needle, *declaration.as_filename); match_result.matched)
return { TriState::True, match_result.score };
return { TriState::False };
}
if (declaration.is_symbol_declaration()) {
if (declaration.as_symbol_declaration->name.contains(needle, CaseSensitivity::CaseInsensitive)
|| declaration.as_symbol_declaration->scope.contains(needle, CaseSensitivity::CaseInsensitive))
return { TriState::True };
auto& symbol = *declaration.as_symbol_declaration;
auto haystack = qualified_symbol_name(symbol);
if (auto match_result = fuzzy_match(needle, haystack); match_result.matched)
return { TriState::True, match_result.score };
return { TriState::False };
}

View File

@ -1,6 +1,7 @@
/*
* 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
*/
@ -78,7 +79,7 @@ Locator::Locator(Core::EventReceiver* parent)
open_suggestion(index);
};
m_model = GUI::FilteringProxyModel::create(ProjectDeclarations::the().declarations_model()).release_value_but_fixme_should_propagate_errors();
m_model = GUI::FilteringProxyModel::create(ProjectDeclarations::the().declarations_model(), GUI::FilteringProxyModel::FilteringOptions::SortByScore).release_value_but_fixme_should_propagate_errors();
m_suggestion_view->set_model(m_model);
}