Browse Source

Improved and simplified folder exclusion for grep and ctags

pipelines/280567345
eidheim 5 years ago
parent
commit
61caf12e82
  1. 7
      src/ctags.cpp
  2. 12
      src/grep.cpp
  3. 60
      src/project_build.cpp
  4. 11
      src/project_build.hpp
  5. 7
      src/window.cpp

7
src/ctags.cpp

@ -22,11 +22,10 @@ Ctags::Ctags(const boost::filesystem::path &path, bool enable_scope, bool enable
if(boost::filesystem::is_directory(path, ec)) { if(boost::filesystem::is_directory(path, ec)) {
auto build = Project::Build::create(path); auto build = Project::Build::create(path);
std::string exclude; std::string exclude;
if(!build->project_path.empty()) { for(auto &exclude_folder : build->get_exclude_folders())
exclude += " --exclude=\"" + exclude_folder + "/*\" --exclude=\"*/" + exclude_folder + "/*\"";
if(!build->project_path.empty())
project_path = build->project_path; project_path = build->project_path;
for(auto &exclude_path : build->get_exclude_paths())
exclude += " --exclude=" + filesystem::escape_argument(filesystem::get_relative_path(exclude_path, project_path).string());
}
else else
project_path = path; project_path = path;
command = Config::get().project.ctags_command + options + fields + exclude + " -R *"; command = Config::get().project.ctags_command + options + fields + exclude + " -R *";

12
src/grep.cpp

@ -10,16 +10,14 @@ Grep::Grep(const boost::filesystem::path &path, const std::string &pattern, bool
return; return;
auto build = Project::Build::create(path); auto build = Project::Build::create(path);
std::string exclude; std::string exclude;
if(!build->project_path.empty()) { for(auto &exclude_folder : build->get_exclude_folders())
project_path = build->project_path;
for(auto &exclude_path : build->get_exclude_paths()) {
#ifdef JUCI_USE_GREP_EXCLUDE #ifdef JUCI_USE_GREP_EXCLUDE
exclude += " --exclude=" + filesystem::escape_argument(filesystem::get_relative_path(exclude_path, build->project_path).string()) + "/*"; exclude += " --exclude=\"" + exclude_folder + "/*\" --exclude=\"*/" + exclude_folder + "/*\""; // BSD grep does not support --exclude-dir
#else #else
exclude += " --exclude-dir=" + filesystem::escape_argument(filesystem::get_relative_path(exclude_path, build->project_path).string()); exclude += " --exclude-dir=\"" + exclude_folder + '"'; // Need to use --exclude-dir on Linux for some reason (could not get --exclude to work)
#endif #endif
} if(!build->project_path.empty())
} project_path = build->project_path;
else else
project_path = path; project_path = path;

60
src/project_build.cpp

@ -93,10 +93,22 @@ boost::filesystem::path Project::Build::get_debug_path() {
return filesystem::get_normal_path(debug_build_path); return filesystem::get_normal_path(debug_build_path);
} }
std::vector<boost::filesystem::path> Project::Build::get_exclude_paths() { std::vector<std::string> Project::Build::get_exclude_folders() {
if(!project_path.empty()) auto default_build_path = Config::get().project.default_build_path;
return {project_path / ".git"}; boost::replace_all(default_build_path, "<project_directory_name>", "");
return {};
auto debug_build_path = Config::get().project.debug_build_path;
boost::replace_all(debug_build_path, "<default_build_path>", Config::get().project.default_build_path);
boost::replace_all(debug_build_path, "<project_directory_name>", "");
return std::vector<std::string>{
".git", "build", "debug", // Common exclude folders
boost::filesystem::path(default_build_path).filename().string(), boost::filesystem::path(debug_build_path).filename().string(), // C/C++
"target", // Rust
"node_modules", "dist", "coverage", ".expo", // JavaScript
".mypy_cache",
"__pycache__" // Python
};
} }
Project::CMakeBuild::CMakeBuild(const boost::filesystem::path &path) : Project::Build(), cmake(path) { Project::CMakeBuild::CMakeBuild(const boost::filesystem::path &path) : Project::Build(), cmake(path) {
@ -146,13 +158,6 @@ bool Project::CMakeBuild::is_valid() {
return true; return true;
} }
std::vector<boost::filesystem::path> Project::CMakeBuild::get_exclude_paths() {
auto exclude_paths = Project::Build::get_exclude_paths();
exclude_paths.emplace_back(get_default_path());
exclude_paths.emplace_back(get_debug_path());
return exclude_paths;
}
Project::MesonBuild::MesonBuild(const boost::filesystem::path &path) : Project::Build(), meson(path) { Project::MesonBuild::MesonBuild(const boost::filesystem::path &path) : Project::Build(), meson(path) {
project_path = meson.project_path; project_path = meson.project_path;
} }
@ -197,39 +202,6 @@ bool Project::MesonBuild::is_valid() {
return true; return true;
} }
std::vector<boost::filesystem::path> Project::MesonBuild::get_exclude_paths() {
auto exclude_paths = Project::Build::get_exclude_paths();
exclude_paths.emplace_back(get_default_path());
exclude_paths.emplace_back(get_debug_path());
return exclude_paths;
}
std::vector<boost::filesystem::path> Project::CompileCommandsBuild::get_exclude_paths() {
auto exclude_paths = Project::Build::get_exclude_paths();
exclude_paths.emplace_back(get_default_path());
exclude_paths.emplace_back(get_debug_path());
return exclude_paths;
}
std::string Project::CargoBuild::get_compile_command() { std::string Project::CargoBuild::get_compile_command() {
return Config::get().project.cargo_command + " build"; return Config::get().project.cargo_command + " build";
} }
std::vector<boost::filesystem::path> Project::CargoBuild::get_exclude_paths() {
auto exclude_paths = Project::Build::get_exclude_paths();
exclude_paths.emplace_back(project_path / "target");
return exclude_paths;
}
std::vector<boost::filesystem::path> Project::NpmBuild::get_exclude_paths() {
auto exclude_paths = Project::Build::get_exclude_paths();
exclude_paths.emplace_back(project_path / "node_modules");
return exclude_paths;
}
std::vector<boost::filesystem::path> Project::PythonMain::get_exclude_paths() {
auto exclude_paths = Project::Build::get_exclude_paths();
exclude_paths.emplace_back(project_path / ".mypy_cache");
exclude_paths.emplace_back(project_path / "__pycache__");
return exclude_paths;
}

11
src/project_build.hpp

@ -21,7 +21,7 @@ namespace Project {
/// Returns true if the project path reported by build system is correct /// Returns true if the project path reported by build system is correct
virtual bool is_valid() { return true; } virtual bool is_valid() { return true; }
virtual std::vector<boost::filesystem::path> get_exclude_paths(); std::vector<std::string> get_exclude_folders();
static std::unique_ptr<Build> create(const boost::filesystem::path &path); static std::unique_ptr<Build> create(const boost::filesystem::path &path);
}; };
@ -39,8 +39,6 @@ namespace Project {
boost::filesystem::path get_executable(const boost::filesystem::path &path) override; boost::filesystem::path get_executable(const boost::filesystem::path &path) override;
bool is_valid() override; bool is_valid() override;
std::vector<boost::filesystem::path> get_exclude_paths() override;
}; };
class MesonBuild : public Build { class MesonBuild : public Build {
@ -56,13 +54,10 @@ namespace Project {
boost::filesystem::path get_executable(const boost::filesystem::path &path) override; boost::filesystem::path get_executable(const boost::filesystem::path &path) override;
bool is_valid() override; bool is_valid() override;
std::vector<boost::filesystem::path> get_exclude_paths() override;
}; };
class CompileCommandsBuild : public Build { class CompileCommandsBuild : public Build {
public: public:
std::vector<boost::filesystem::path> get_exclude_paths() override;
}; };
class CargoBuild : public Build { class CargoBuild : public Build {
@ -74,17 +69,13 @@ namespace Project {
std::string get_compile_command() override; std::string get_compile_command() override;
boost::filesystem::path get_executable(const boost::filesystem::path &path) override { return get_debug_path() / project_path.filename(); } boost::filesystem::path get_executable(const boost::filesystem::path &path) override { return get_debug_path() / project_path.filename(); }
std::vector<boost::filesystem::path> get_exclude_paths() override;
}; };
class NpmBuild : public Build { class NpmBuild : public Build {
public: public:
std::vector<boost::filesystem::path> get_exclude_paths() override;
}; };
class PythonMain : public Build { class PythonMain : public Build {
public: public:
std::vector<boost::filesystem::path> get_exclude_paths() override;
}; };
} // namespace Project } // namespace Project

7
src/window.cpp

@ -924,7 +924,7 @@ void Window::set_menu_actions() {
menu.add_action("source_find_file", []() { menu.add_action("source_find_file", []() {
auto view_folder = Project::get_preferably_view_folder(); auto view_folder = Project::get_preferably_view_folder();
auto build = Project::Build::create(view_folder); auto build = Project::Build::create(view_folder);
auto exclude_paths = build->get_exclude_paths(); auto exclude_folders = build->get_exclude_folders();
if(!build->project_path.empty()) if(!build->project_path.empty())
view_folder = build->project_path; view_folder = build->project_path;
@ -945,8 +945,9 @@ void Window::set_menu_actions() {
auto path = iter->path(); auto path = iter->path();
// ignore folders // ignore folders
if(!boost::filesystem::is_regular_file(path, ec)) { if(!boost::filesystem::is_regular_file(path, ec)) {
if(std::any_of(exclude_paths.begin(), exclude_paths.end(), [&path](const boost::filesystem::path &exclude_path) { auto filename = path.filename();
return path == exclude_path; if(std::any_of(exclude_folders.begin(), exclude_folders.end(), [&filename](const std::string &exclude_folder) {
return filename == exclude_folder;
})) }))
iter.no_push(); iter.no_push();
continue; continue;

Loading…
Cancel
Save