diff --git a/include/eepp/core/string.hpp b/include/eepp/core/string.hpp index d94e243db..2a0c8e98f 100644 --- a/include/eepp/core/string.hpp +++ b/include/eepp/core/string.hpp @@ -211,8 +211,6 @@ class EE_API String { */ static bool contains( const String& haystack, const String& needle ); - static int fuzzyMatch( const String& string, const String& pattern ); - static int fuzzyMatch( const std::string& string, const std::string& pattern ); /** Replace all occurrences of the search string with the replacement string. */ diff --git a/src/eepp/core/string.cpp b/src/eepp/core/string.cpp index 705ec477e..c999af863 100644 --- a/src/eepp/core/string.cpp +++ b/src/eepp/core/string.cpp @@ -399,11 +399,9 @@ template constexpr int tFuzzyMatch( const T* str, const T* ptn ) { } str++; } - return score; -} - -int String::fuzzyMatch( const String& string, const String& pattern ) { - return tFuzzyMatch( string.c_str(), pattern.c_str() ); + if ( *ptn ) + return INT_MIN; + return score - strlen( str ); } int String::fuzzyMatch( const std::string& string, const std::string& pattern ) { diff --git a/src/tools/codeeditor/codeeditor.cpp b/src/tools/codeeditor/codeeditor.cpp index 08170ca15..5d15a4ce2 100644 --- a/src/tools/codeeditor/codeeditor.cpp +++ b/src/tools/codeeditor/codeeditor.cpp @@ -496,12 +496,23 @@ void App::hideLocateBar() { void App::updateLocateTable() { if ( !mLocateInput->getText().empty() ) { +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) + mDirTree->asyncFuzzyMatchTree( + mLocateInput->getText(), LOCATEBAR_MAX_RESULTS, [&]( auto res ) { + mUISceneNode->runOnMainThread( [&, res] { + mLocateTable->setModel( res ); + mLocateTable->getSelection().set( mLocateTable->getModel()->index( 0 ) ); + } ); + } ); +#else mLocateTable->setModel( mDirTree->fuzzyMatchTree( mLocateInput->getText(), LOCATEBAR_MAX_RESULTS ) ); + mLocateTable->getSelection().set( mLocateTable->getModel()->index( 0 ) ); +#endif } else { mLocateTable->setModel( mDirTree->asModel( LOCATEBAR_MAX_RESULTS ) ); + mLocateTable->getSelection().set( mLocateTable->getModel()->index( 0 ) ); } - mLocateTable->getSelection().set( mLocateTable->getModel()->index( 0 ) ); } bool App::trySendUnlockedCmd( const KeyEvent& keyEvent ) { @@ -2384,6 +2395,8 @@ void App::init( const std::string& file, const Float& pidelDensity ) { EE_MAIN_FUNC int main( int argc, char* argv[] ) { #ifndef EE_DEBUG Log::create( LogLevel::Info, false, true ); +#else + Log::create( LogLevel::Debug, true, true ); #endif args::ArgumentParser parser( "ecode" ); args::HelpFlag help( parser, "help", "Display this help menu", {'h', "help"} ); diff --git a/src/tools/codeeditor/projectdirectorytree.cpp b/src/tools/codeeditor/projectdirectorytree.cpp index 9ebb9fc08..b889ea34e 100644 --- a/src/tools/codeeditor/projectdirectorytree.cpp +++ b/src/tools/codeeditor/projectdirectorytree.cpp @@ -1,6 +1,5 @@ #include "projectdirectorytree.hpp" #include -#include #include ProjectDirectoryTree::ProjectDirectoryTree( const std::string& path, @@ -12,7 +11,7 @@ ProjectDirectoryTree::ProjectDirectoryTree( const std::string& path, void ProjectDirectoryTree::scan( const ProjectDirectoryTree::ScanCompleteEvent& scanComplete, const std::vector& acceptedPattern, const bool& ignoreHidden ) { -#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined(__EMSCRIPTEN_PTHREADS__) +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) mPool->run( [&, acceptedPattern, ignoreHidden] { #endif @@ -45,11 +44,11 @@ void ProjectDirectoryTree::scan( const ProjectDirectoryTree::ScanCompleteEvent& getDirectoryFiles( mFiles, mNames, mPath, info, ignoreHidden, mIgnoreMatcher ); } mIsReady = true; -#if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN && !defined(__EMSCRIPTEN_PTHREADS__) +#if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN && !defined( __EMSCRIPTEN_PTHREADS__ ) if ( scanComplete ) scanComplete( *this ); #endif -#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined(__EMSCRIPTEN_PTHREADS__) +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN || defined( __EMSCRIPTEN_PTHREADS__ ) }, [scanComplete, this] { if ( scanComplete ) @@ -63,12 +62,8 @@ std::shared_ptr ProjectDirectoryTree::fuzzyMatchTree( const std:: std::multimap> matchesMap; std::vector files; std::vector names; - int score; - for ( size_t i = 0; i < mNames.size(); i++ ) { - if ( ( score = String::fuzzyMatch( mNames[i], match ) ) > 0 ) { - matchesMap.insert( {score, i} ); - } - } + for ( size_t i = 0; i < mNames.size(); i++ ) + matchesMap.insert( {String::fuzzyMatch( mNames[i], match ), i} ); for ( auto& res : matchesMap ) { if ( names.size() < max ) { names.emplace_back( mNames[res.second] ); @@ -94,6 +89,16 @@ std::shared_ptr ProjectDirectoryTree::matchTree( const std::strin return std::make_shared( files, names ); } +void ProjectDirectoryTree::asyncFuzzyMatchTree( const std::string& match, const size_t& max, + ProjectDirectoryTree::MatchResultCb res ) const { + mPool->run( [&, match, max, res]() { res( fuzzyMatchTree( match, max ) ); }, []() {} ); +} + +void ProjectDirectoryTree::asyncMatchTree( const std::string& match, const size_t& max, + ProjectDirectoryTree::MatchResultCb res ) const { + mPool->run( [&, match, max, res]() { res( matchTree( match, max ) ); }, []() {} ); +} + std::shared_ptr ProjectDirectoryTree::asModel( const size_t& max ) const { if ( mNames.empty() ) return std::make_shared( std::vector(), diff --git a/src/tools/codeeditor/projectdirectorytree.hpp b/src/tools/codeeditor/projectdirectorytree.hpp index 5011e4d77..573ea4686 100644 --- a/src/tools/codeeditor/projectdirectorytree.hpp +++ b/src/tools/codeeditor/projectdirectorytree.hpp @@ -46,6 +46,7 @@ class FileListModel : public Model { class ProjectDirectoryTree { public: typedef std::function ScanCompleteEvent; + typedef std::function )> MatchResultCb; ProjectDirectoryTree( const std::string& path, std::shared_ptr threadPool ); @@ -58,6 +59,11 @@ class ProjectDirectoryTree { std::shared_ptr matchTree( const std::string& match, const size_t& max ) const; + void asyncFuzzyMatchTree( const std::string& match, const size_t& max, + MatchResultCb res ) const; + + void asyncMatchTree( const std::string& match, const size_t& max, MatchResultCb res ) const; + std::shared_ptr asModel( const size_t& max ) const; size_t getFilesCount() const;