Added a new fuzzy matching algorithm, it should be better than the previous version, it's similar to what most editors have (implementation is inspired in the sublime_text fuzzy matcher).

This commit is contained in:
Martín Lucas Golini
2025-03-08 18:33:25 -03:00
parent 0dba4a222b
commit dfb0820d0f
12 changed files with 305 additions and 59 deletions

View File

@@ -66,7 +66,7 @@ void CommandPalette::setCurModel( const std::shared_ptr<CommandPaletteModel>& cu
std::shared_ptr<CommandPaletteModel>
CommandPalette::fuzzyMatch( const std::vector<std::vector<std::string>>& cmdPalette,
const std::string& match, const size_t& max ) const {
const std::string& pattern, const size_t& max ) const {
if ( cmdPalette.empty() )
return {};
@@ -75,9 +75,11 @@ CommandPalette::fuzzyMatch( const std::vector<std::vector<std::string>>& cmdPale
std::vector<std::vector<std::string>> ret;
for ( size_t i = 0; i < cmdPalette.size(); i++ ) {
int matchName = String::fuzzyMatch( cmdPalette[i][0], match );
int matchKeybind = String::fuzzyMatch( cmdPalette[i][2], match );
matchesMap.insert( { std::max( matchName, matchKeybind ), i } );
int matchName = String::fuzzyMatch( pattern, cmdPalette[i][0] );
int matchKeybind = String::fuzzyMatch( pattern, cmdPalette[i][2] );
int matchScore = std::max( matchName, matchKeybind );
if ( matchScore > std::numeric_limits<int>::min() )
matchesMap.insert( { matchScore, i } );
}
for ( auto& res : matchesMap ) {
if ( ret.size() < max )
@@ -86,15 +88,15 @@ CommandPalette::fuzzyMatch( const std::vector<std::vector<std::string>>& cmdPale
return CommandPaletteModel::create( 3, ret );
}
void CommandPalette::asyncFuzzyMatch( const std::string& match, const size_t& max,
void CommandPalette::asyncFuzzyMatch( const std::string& pattern, const size_t& max,
MatchResultCb res ) const {
if ( !mCurModel )
return;
mPool->run( [this, match, max, res]() {
mPool->run( [this, pattern, max, res]() {
const std::vector<std::vector<std::string>>& cmdPalette =
mCurModel.get() == mBaseModel.get() ? mCommandPalette : mCommandPaletteEditor;
res( fuzzyMatch( cmdPalette, match, max ) );
res( fuzzyMatch( cmdPalette, pattern, max ) );
} );
}