From b3d0eea6fcaa8c655a9ea1a3398c649fb94af3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 14 Mar 2024 10:38:46 -0300 Subject: [PATCH] More project search filters WIP. --- include/eepp/core/string.hpp | 4 ++-- src/eepp/core/string.cpp | 26 +++++++++++----------- src/tools/ecode/ecode.cpp | 1 + src/tools/ecode/globalsearchcontroller.cpp | 2 +- src/tools/ecode/projectdirectorytree.cpp | 4 ++++ src/tools/ecode/projectdirectorytree.hpp | 2 ++ src/tools/ecode/projectsearch.cpp | 13 +++++++---- src/tools/ecode/projectsearch.hpp | 4 ++-- 8 files changed, 34 insertions(+), 22 deletions(-) diff --git a/include/eepp/core/string.hpp b/include/eepp/core/string.hpp index ce0f3b879..401d23a99 100644 --- a/include/eepp/core/string.hpp +++ b/include/eepp/core/string.hpp @@ -401,13 +401,13 @@ class EE_API String { /** glob matches a string against a glob ** @return True if matches */ - static bool globMatch( const std::string& text, const std::string_view& glob, + static bool globMatch( const std::string_view& text, const std::string_view& glob, bool caseInsensitive = false ); /** glob matches a string against a set of globs ** @return True if matches */ - static bool globMatch( const std::string& text, const std::vector& globs, + static bool globMatch( const std::string_view& text, const std::vector& globs, bool caseInsensitive = false ); /** @brief Default constructor diff --git a/src/eepp/core/string.cpp b/src/eepp/core/string.cpp index edfd7c305..3cce4c472 100644 --- a/src/eepp/core/string.cpp +++ b/src/eepp/core/string.cpp @@ -23,16 +23,16 @@ const std::size_t String::InvalidPos = StringType::npos; #define PATHSEP '/' #define CASE( c, caseInsensitive ) ( caseInsensitive ? std::tolower( c ) : ( c ) ) -bool String::globMatch( const std::string& text, const std::string_view& glob, +bool String::globMatch( const std::string_view& text, const std::string_view& glob, bool caseInsensitive ) { size_t i = 0; size_t j = 0; size_t n = text.size(); size_t m = glob.size(); - size_t text1_backup = std::string::npos; - size_t glob1_backup = std::string::npos; - size_t text2_backup = std::string::npos; - size_t glob2_backup = std::string::npos; + size_t text1_backup = std::string_view::npos; + size_t glob1_backup = std::string_view::npos; + size_t text2_backup = std::string_view::npos; + size_t glob2_backup = std::string_view::npos; bool nodot = !DOTGLOB; // match pathname if glob contains a / otherwise match the basename if ( j + 1 < m && glob[j] == '/' ) { @@ -43,9 +43,9 @@ bool String::globMatch( const std::string& text, const std::string_view& glob, if ( i < n && text[i] == PATHSEP ) i++; j++; - } else if ( glob.find( '/' ) == std::string::npos ) { + } else if ( glob.find( '/' ) == std::string_view::npos ) { size_t sep = text.rfind( PATHSEP ); - if ( sep != std::string::npos ) + if ( sep != std::string_view::npos ) i = sep + 1; } while ( i < n ) { @@ -63,8 +63,8 @@ bool String::globMatch( const std::string& text, const std::string_view& glob, if ( glob[j] != '/' ) return false; // new **-loop, discard *-loop - text1_backup = std::string::npos; - glob1_backup = std::string::npos; + text1_backup = std::string_view::npos; + glob1_backup = std::string_view::npos; text2_backup = i; glob2_backup = ++j; continue; @@ -130,13 +130,13 @@ bool String::globMatch( const std::string& text, const std::string_view& glob, continue; } } - if ( glob1_backup != std::string::npos && text[text1_backup] != PATHSEP ) { + if ( glob1_backup != std::string_view::npos && text[text1_backup] != PATHSEP ) { // *-loop: backtrack to the last * but do not jump over / i = ++text1_backup; j = glob1_backup; continue; } - if ( glob2_backup != std::string::npos ) { + if ( glob2_backup != std::string_view::npos ) { // **-loop: backtrack to the last ** i = ++text2_backup; j = glob2_backup; @@ -151,8 +151,8 @@ bool String::globMatch( const std::string& text, const std::string_view& glob, return j >= m; } -bool String::globMatch( const std::string& text, const std::vector& globs, - bool caseInsensitive ) { +bool String::globMatch( const std::string_view& text, const std::vector& globs, + bool caseInsensitive ) { for ( const auto& glob : globs ) { if ( globMatch( text, glob, caseInsensitive ) ) return true; diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 197455d9b..87faa14bd 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -733,6 +733,7 @@ App::~App() { delete mFileWatcher; mFileWatcher = nullptr; } + mDirTree->resetPluginManager(); mPluginManager.reset(); eeSAFE_DELETE( mSplitter ); diff --git a/src/tools/ecode/globalsearchcontroller.cpp b/src/tools/ecode/globalsearchcontroller.cpp index c756093a7..7879b4419 100644 --- a/src/tools/ecode/globalsearchcontroller.cpp +++ b/src/tools/ecode/globalsearchcontroller.cpp @@ -589,7 +589,7 @@ void GlobalSearchController::doGlobalSearch( String text, String filter, bool ca caseSensitive, wholeWord, luaPattern ? TextDocument::FindReplaceType::LuaPattern : TextDocument::FindReplaceType::Normal, - parseGlobMatches( filter ) ); + parseGlobMatches( filter ), mApp->getCurrentProject() ); } } diff --git a/src/tools/ecode/projectdirectorytree.cpp b/src/tools/ecode/projectdirectorytree.cpp index 961568d72..bb521345d 100644 --- a/src/tools/ecode/projectdirectorytree.cpp +++ b/src/tools/ecode/projectdirectorytree.cpp @@ -323,6 +323,10 @@ void ProjectDirectoryTree::onChange( const ProjectDirectoryTree::Action& action, } } +void ProjectDirectoryTree::resetPluginManager() { + mPluginManager = nullptr; +} + void ProjectDirectoryTree::tryAddFile( const FileInfo& file ) { if ( mIgnoreHidden && file.isHidden() ) return; diff --git a/src/tools/ecode/projectdirectorytree.hpp b/src/tools/ecode/projectdirectorytree.hpp index 11021672d..50e9860ae 100644 --- a/src/tools/ecode/projectdirectorytree.hpp +++ b/src/tools/ecode/projectdirectorytree.hpp @@ -144,6 +144,8 @@ class ProjectDirectoryTree { const std::string& getPath() const { return mPath; } + void resetPluginManager(); + protected: std::string mPath; std::shared_ptr mPool; diff --git a/src/tools/ecode/projectsearch.cpp b/src/tools/ecode/projectsearch.cpp index f98e64dd6..3d92c30ff 100644 --- a/src/tools/ecode/projectsearch.cpp +++ b/src/tools/ecode/projectsearch.cpp @@ -145,7 +145,7 @@ searchInFileLuaPattern( const std::string& file, const std::string& text, const void ProjectSearch::find( const std::vector files, const std::string& string, ResultCb result, bool caseSensitive, bool wholeWord, const TextDocument::FindReplaceType& type, - const std::vector& pathFilters ) { + const std::vector& pathFilters, std::string basePath ) { Result res; const auto occ = type == TextDocument::FindReplaceType::Normal @@ -182,9 +182,10 @@ struct FindData { void ProjectSearch::find( const std::vector files, std::string string, std::shared_ptr pool, ResultCb result, bool caseSensitive, bool wholeWord, const TextDocument::FindReplaceType& type, - const std::vector& pathFilters ) { + const std::vector& pathFilters, std::string basePath ) { if ( files.empty() ) result( {} ); + FileSystem::dirAddSlashAtEnd( basePath ); FindData* findData = eeNew( FindData, () ); findData->resCount = files.size(); if ( !caseSensitive ) @@ -197,10 +198,14 @@ void ProjectSearch::find( const std::vector files, std::string stri search.resize( files.size() ); size_t pos = 0; size_t count = 0; - for ( auto& file : files ) { + for ( const auto& file : files ) { bool skip = false; + std::string_view fsv( file ); + if ( !basePath.empty() && String::startsWith( file, basePath ) ) + fsv = fsv.substr( basePath.size() ); + for ( const auto& filter : pathFilters ) { - bool matches = String::globMatch( file, filter.first ); + bool matches = String::globMatch( fsv, filter.first ); if ( ( matches && filter.second ) || ( !matches && !filter.second ) ) { skip = true; break; diff --git a/src/tools/ecode/projectsearch.hpp b/src/tools/ecode/projectsearch.hpp index 57f7422d5..4ac1b74f2 100644 --- a/src/tools/ecode/projectsearch.hpp +++ b/src/tools/ecode/projectsearch.hpp @@ -212,14 +212,14 @@ class ProjectSearch { find( const std::vector files, const std::string& string, ResultCb result, bool caseSensitive, bool wholeWord = false, const TextDocument::FindReplaceType& type = TextDocument::FindReplaceType::Normal, - const std::vector& pathFilters = {} ); + const std::vector& pathFilters = {}, std::string basePath = "" ); static void find( const std::vector files, std::string string, std::shared_ptr pool, ResultCb result, bool caseSensitive, bool wholeWord = false, const TextDocument::FindReplaceType& type = TextDocument::FindReplaceType::Normal, - const std::vector& pathFilters = {} ); + const std::vector& pathFilters = {}, std::string basePath = "" ); }; } // namespace ecode