From db09c57c4057ed47453c727cbffb789ccaabf75a Mon Sep 17 00:00:00 2001 From: Eran Ifrah Date: Mon, 8 Apr 2024 20:08:04 +0300 Subject: [PATCH] Filter "self", "&mut self" etc only when the current editor is of Rust or Python --- CodeLite/clFilesCollector.cpp | 91 +++++++++++++++++---------------- Plugin/cl_editor_tip_window.cpp | 15 ++++-- Plugin/cl_editor_tip_window.h | 4 +- 3 files changed, 61 insertions(+), 49 deletions(-) diff --git a/CodeLite/clFilesCollector.cpp b/CodeLite/clFilesCollector.cpp index 38b1f619ae..9ac9685450 100644 --- a/CodeLite/clFilesCollector.cpp +++ b/CodeLite/clFilesCollector.cpp @@ -4,6 +4,7 @@ #include "fileutils.h" #include +#include #include #include #include @@ -44,18 +45,22 @@ bool IsRelPathContainedInSpec(const wxString& rootPath, const wxString& fullPath wxFileName fp(fullPath); fp.MakeRelativeTo(rootPath); - wxArrayString fpDirs = fp.GetDirs(); - fpDirs.Add(fp.GetFullName()); // Add the last (filename) part into the path array + std::unordered_set fpSet{ fp.GetDirs().begin(), fp.GetDirs().end() }; + fpSet.insert(fp.GetFullName()); // Add the last (filename) part into the path array - const wxString pathSeparators = fp.GetPathSeparators(); - for(const wxString& spec : specSet) { + const wxString pathSeparators = wxFileName::GetPathSeparators(); + for (const wxString& spec : specSet) { // Check if spec matches the beginning of the full path - if(fp.GetFullPath().StartsWith(spec)) { + // Add the pathSeparators here to avoid capturing wrong + // directories. e.g. exclude contains `.git`, without the + // separator, it will also filter out `.github` + if (fp.GetFullPath().StartsWith(spec + pathSeparators)) { return true; } + // First check if spec is a path-elem (without path separators) // Then check if path-elem if found in the array of full path-elements - if(!spec.Contains(pathSeparators) && (fpDirs.Index(spec) != wxNOT_FOUND)) { + if (!spec.Contains(pathSeparators) && fpSet.count(spec)) { return true; } } @@ -67,7 +72,7 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector& f const wxString& excludeFilespec, const wxStringSet_t& excludeFolders) { filesOutput.clear(); - if(!wxFileName::DirExists(rootFolder)) { + if (!wxFileName::DirExists(rootFolder)) { clDEBUG() << "clFilesScanner: No such dir:" << rootFolder << clEndl; return 0; } @@ -85,18 +90,18 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector& f Q.push(rootFolder); Visited.insert(rootFolder); - while(!Q.empty()) { + while (!Q.empty()) { wxString dirpath = Q.front(); Q.pop(); wxDir dir(dirpath); - if(!dir.IsOpened()) { + if (!dir.IsOpened()) { continue; } wxString filename; bool cont = dir.GetFirst(&filename); - while(cont) { + while (cont) { // Check to see if this is a folder wxString fullpath; fullpath << dir.GetNameWithSep() << filename; @@ -115,16 +120,16 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector& f (excludeFolders.count(FileUtils::RealPath(fullpath)) #endif || IsRelPathContainedInSpec(rootFolder, fullpath, excludeFolders))); - if(isDirectory && !isExcludeDir) { + if (isDirectory && !isExcludeDir) { // Traverse into this folder wxString realPath = FileUtils::RealPath(fullpath); - if(Visited.insert(realPath).second) { + if (Visited.insert(realPath).second) { Q.push(fullpath); } - } else if(!isDirectory && FileUtils::WildMatch(excludeSpecArr, filename)) { + } else if (!isDirectory && FileUtils::WildMatch(excludeSpecArr, filename)) { // Do nothing - } else if(!isDirectory && FileUtils::WildMatch(specArr, filename)) { + } else if (!isDirectory && FileUtils::WildMatch(specArr, filename)) { // Include this file filesOutput.push_back(fullpath); } @@ -137,7 +142,7 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector& f size_t clFilesScanner::Scan(const wxString& rootFolder, const wxString& filespec, const wxString& excludeFilespec, const wxString& excludeFoldersSpec, std::function&& collect_cb) { - if(!wxFileName::DirExists(rootFolder)) { + if (!wxFileName::DirExists(rootFolder)) { clDEBUG() << "clFilesScanner: No such directory:" << rootFolder << clEndl; return 0; } @@ -152,37 +157,37 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, const wxString& filespec Visited.insert(FileUtils::RealPath(rootFolder)); size_t nCount = 0; - while(!Q.empty()) { + while (!Q.empty()) { wxString dirpath = Q.front(); Q.pop(); wxDir dir(dirpath); - if(!dir.IsOpened()) { + if (!dir.IsOpened()) { continue; } wxString filename; bool cont = dir.GetFirst(&filename); - while(cont) { + while (cont) { // Check to see if this is a folder wxString fullpath; fullpath << dir.GetNameWithSep() << filename; bool isDirectory = wxFileName::DirExists(fullpath); bool isFile = !isDirectory; // Use FileUtils::RealPath() here to cope with symlinks on Linux - if(isDirectory /* a folder */ && - !FileUtils::WildMatch(excludeFoldersSpecArr, filename) /* does not match the exclude folder spec */) { + if (isDirectory /* a folder */ && + !FileUtils::WildMatch(excludeFoldersSpecArr, filename) /* does not match the exclude folder spec */) { // Traverse into this folder wxString real_path = FileUtils::RealPath(fullpath); - if(Visited.count(real_path) == 0) { + if (Visited.count(real_path) == 0) { Visited.insert(real_path); Q.push(fullpath); } - } else if(isFile && /* a file */ - !FileUtils::WildMatch(excludeSpecArr, filename) /* does not match the exclude file spec */ && - FileUtils::WildMatch(specArr, filename) /* matches the file spec array */) { + } else if (isFile && /* a file */ + !FileUtils::WildMatch(excludeSpecArr, filename) /* does not match the exclude file spec */ && + FileUtils::WildMatch(specArr, filename) /* matches the file spec array */) { // Include this file - if(!collect_cb(fullpath)) { + if (!collect_cb(fullpath)) { // requested to stop return nCount; } else { @@ -199,13 +204,13 @@ size_t clFilesScanner::ScanNoRecurse(const wxString& rootFolder, clFilesScanner: const wxString& matchSpec) { results.clear(); - if(!wxFileName::DirExists(rootFolder)) { + if (!wxFileName::DirExists(rootFolder)) { clDEBUG() << "clFilesScanner::ScanNoRecurse(): No such dir:" << rootFolder << clEndl; return 0; } wxArrayString specArr = ::wxStringTokenize(matchSpec.Lower(), ";,|", wxTOKEN_STRTOK); wxDir dir(rootFolder); - if(!dir.IsOpened()) { + if (!dir.IsOpened()) { clDEBUG() << "Failed to open root dir:" << rootFolder; return 0; } @@ -213,20 +218,20 @@ size_t clFilesScanner::ScanNoRecurse(const wxString& rootFolder, clFilesScanner: wxString filename; bool cont = dir.GetFirst(&filename); - while(cont) { - if(FileUtils::WildMatch(specArr, filename)) { + while (cont) { + if (FileUtils::WildMatch(specArr, filename)) { wxString fullpath; fullpath << dirWithSep << filename; EntryData ed; - if(FileUtils::IsDirectory(fullpath)) { + if (FileUtils::IsDirectory(fullpath)) { ed.flags |= kIsFolder; } else { ed.flags |= kIsFile; } - if(FileUtils::IsSymlink(fullpath)) { + if (FileUtils::IsSymlink(fullpath)) { ed.flags |= kIsSymlink; } - if(FileUtils::IsHidden(fullpath)) { + if (FileUtils::IsHidden(fullpath)) { ed.flags |= kIsHidden; } ed.fullpath = fullpath; @@ -246,7 +251,7 @@ size_t clFilesScanner::ScanNoRecurse(const wxString& rootFolder, clFilesScanner: void clFilesScanner::ScanWithCallbacks(const wxString& rootFolder, std::function&& on_folder_cb, std::function&& on_file_cb, size_t search_flags) { - if(!wxFileName::DirExists(rootFolder)) { + if (!wxFileName::DirExists(rootFolder)) { clDEBUG() << "clFilesScanner: No such directory:" << rootFolder << clEndl; return; } @@ -257,12 +262,12 @@ void clFilesScanner::ScanWithCallbacks(const wxString& rootFolder, std::function Q.push_back(FileUtils::RealPath(rootFolder)); Visited.insert(FileUtils::RealPath(rootFolder)); - while(!Q.empty()) { + while (!Q.empty()) { wxString dirpath = Q.front(); Q.erase(Q.begin()); wxDir dir(dirpath); - if(!dir.IsOpened()) { + if (!dir.IsOpened()) { continue; } @@ -272,41 +277,41 @@ void clFilesScanner::ScanWithCallbacks(const wxString& rootFolder, std::function wxString filename; bool cont = dir.GetFirst(&filename); - while(cont) { + while (cont) { // Check to see if this is a folder wxString fullpath; fullpath << dirpath << DIR_SEPARATOR << filename; bool isDirectory = wxFileName::DirExists(fullpath); bool isFile = !isDirectory; - if(isDirectory) { + if (isDirectory) { // A hidden folder? - if((search_flags & SF_EXCLUDE_HIDDEN_DIRS) && FileUtils::IsHidden(fullpath)) { + if ((search_flags & SF_EXCLUDE_HIDDEN_DIRS) && FileUtils::IsHidden(fullpath)) { cont = dir.GetNext(&filename); continue; } // A symlink? - if((search_flags & SF_DONT_FOLLOW_SYMLINKS) && FileUtils::IsSymlink(fullpath)) { + if ((search_flags & SF_DONT_FOLLOW_SYMLINKS) && FileUtils::IsSymlink(fullpath)) { cont = dir.GetNext(&filename); continue; } - if(on_folder_cb && on_folder_cb(fullpath)) { + if (on_folder_cb && on_folder_cb(fullpath)) { // Traverse into this folder wxString real_path = FileUtils::RealPath(fullpath); - if(Visited.insert(real_path).second) { + if (Visited.insert(real_path).second) { Q.push_back(fullpath); } } - } else if(isFile) { + } else if (isFile) { files.Add(fullpath); } cont = dir.GetNext(&filename); } // notify about this batch of files - if(on_file_cb) { + if (on_file_cb) { on_file_cb(files); } } diff --git a/Plugin/cl_editor_tip_window.cpp b/Plugin/cl_editor_tip_window.cpp index 775a775820..fb89a1402a 100644 --- a/Plugin/cl_editor_tip_window.cpp +++ b/Plugin/cl_editor_tip_window.cpp @@ -52,16 +52,21 @@ namespace { /// Rust & Python has the "self" as the first argument /// ignore it -bool ShouldSkipFirstArgument(wxString arg) +bool ShouldSkipFirstArgument(wxString arg, int lexerId) { - arg.Trim().Trim(false); - return arg == "self" || arg == "&self" || arg == "&mut self" || arg == "&self"; + if (lexerId == wxSTC_LEX_RUST || lexerId == wxSTC_LEX_PYTHON) { + arg.Trim().Trim(false); + return arg == "self" || arg == "&self" || arg == "&mut self" || arg == "&self"; + } else { + return false; + } } } // namespace -clEditorTipWindow::clEditorTipWindow(wxWindow* parent) +clEditorTipWindow::clEditorTipWindow(wxStyledTextCtrl* parent) : wxPanel(parent) , m_highlighIndex(0) + , m_lexerId(parent->GetLexer()) { SetBackgroundStyle(wxBG_STYLE_PAINT); m_font = ColoursAndFontsManager::Get().GetFixedFont(true); @@ -144,7 +149,7 @@ void clEditorTipWindow::OnPaint(wxPaintEvent& e) // Choose the line to highlight size_t highlight_index = m_highlighIndex; - if (!m_args.empty() && ShouldSkipFirstArgument(m_args[0])) { + if (!m_args.empty() && ShouldSkipFirstArgument(m_args[0], m_lexerId)) { highlight_index++; } for (size_t i = 0; i < m_args.size(); ++i) { diff --git a/Plugin/cl_editor_tip_window.h b/Plugin/cl_editor_tip_window.h index 879a57bd80..cf3c1eaa75 100644 --- a/Plugin/cl_editor_tip_window.h +++ b/Plugin/cl_editor_tip_window.h @@ -34,6 +34,7 @@ #include #include #include // Base class: wxPanel +#include class WXDLLIMPEXP_SDK clEditorTipWindow : public wxPanel { @@ -55,6 +56,7 @@ class WXDLLIMPEXP_SDK clEditorTipWindow : public wxPanel wxArrayString m_args; wxString m_footer; // the line that says "1 of 2" wxString m_header; // The return value line + int m_lexerId = wxNOT_FOUND; protected: wxSize DoGetTipSize(); @@ -67,7 +69,7 @@ class WXDLLIMPEXP_SDK clEditorTipWindow : public wxPanel void DoMakeMultipleLineTip(); public: - clEditorTipWindow(wxWindow* parent); + explicit clEditorTipWindow(wxStyledTextCtrl* parent); virtual ~clEditorTipWindow(); // API