Skip to content

Commit

Permalink
Filter "self", "&mut self" etc only when the current editor is of Rus…
Browse files Browse the repository at this point in the history
…t or Python
  • Loading branch information
eranif committed Apr 8, 2024
1 parent 92f1bf5 commit db09c57
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
91 changes: 48 additions & 43 deletions CodeLite/clFilesCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "fileutils.h"

#include <queue>
#include <unordered_set>
#include <vector>
#include <wx/dir.h>
#include <wx/filename.h>
Expand Down Expand Up @@ -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<wxString> 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;
}
}
Expand All @@ -67,7 +72,7 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector<wxString>& 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;
}
Expand All @@ -85,18 +90,18 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector<wxString>& 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;
Expand All @@ -115,16 +120,16 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector<wxString>& 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);
}
Expand All @@ -137,7 +142,7 @@ size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector<wxString>& f
size_t clFilesScanner::Scan(const wxString& rootFolder, const wxString& filespec, const wxString& excludeFilespec,
const wxString& excludeFoldersSpec, std::function<bool(const wxString&)>&& collect_cb)
{
if(!wxFileName::DirExists(rootFolder)) {
if (!wxFileName::DirExists(rootFolder)) {
clDEBUG() << "clFilesScanner: No such directory:" << rootFolder << clEndl;
return 0;
}
Expand All @@ -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 {
Expand All @@ -199,34 +204,34 @@ 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;
}
wxString dirWithSep = dir.GetNameWithSep();

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;
Expand All @@ -246,7 +251,7 @@ size_t clFilesScanner::ScanNoRecurse(const wxString& rootFolder, clFilesScanner:
void clFilesScanner::ScanWithCallbacks(const wxString& rootFolder, std::function<bool(const wxString&)>&& on_folder_cb,
std::function<void(const wxArrayString&)>&& on_file_cb, size_t search_flags)
{
if(!wxFileName::DirExists(rootFolder)) {
if (!wxFileName::DirExists(rootFolder)) {
clDEBUG() << "clFilesScanner: No such directory:" << rootFolder << clEndl;
return;
}
Expand All @@ -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;
}

Expand All @@ -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);
}
}
Expand Down
15 changes: 10 additions & 5 deletions Plugin/cl_editor_tip_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion Plugin/cl_editor_tip_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <wx/arrstr.h>
#include <wx/bitmap.h>
#include <wx/panel.h> // Base class: wxPanel
#include <wx/stc/stc.h>

class WXDLLIMPEXP_SDK clEditorTipWindow : public wxPanel
{
Expand All @@ -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();
Expand All @@ -67,7 +69,7 @@ class WXDLLIMPEXP_SDK clEditorTipWindow : public wxPanel
void DoMakeMultipleLineTip();

public:
clEditorTipWindow(wxWindow* parent);
explicit clEditorTipWindow(wxStyledTextCtrl* parent);
virtual ~clEditorTipWindow();

// API
Expand Down

0 comments on commit db09c57

Please sign in to comment.