From 61caf12e82a6085bae57a40b52bc43dac2727a12 Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 29 Mar 2021 10:45:30 +0200 Subject: [PATCH] Improved and simplified folder exclusion for grep and ctags --- src/ctags.cpp | 7 +++-- src/grep.cpp | 12 ++++----- src/project_build.cpp | 60 ++++++++++++------------------------------- src/project_build.hpp | 11 +------- src/window.cpp | 7 ++--- 5 files changed, 29 insertions(+), 68 deletions(-) diff --git a/src/ctags.cpp b/src/ctags.cpp index 5550c3a..e144249 100644 --- a/src/ctags.cpp +++ b/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)) { auto build = Project::Build::create(path); 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; - for(auto &exclude_path : build->get_exclude_paths()) - exclude += " --exclude=" + filesystem::escape_argument(filesystem::get_relative_path(exclude_path, project_path).string()); - } else project_path = path; command = Config::get().project.ctags_command + options + fields + exclude + " -R *"; diff --git a/src/grep.cpp b/src/grep.cpp index 8d3401d..f6216f3 100644 --- a/src/grep.cpp +++ b/src/grep.cpp @@ -10,16 +10,14 @@ Grep::Grep(const boost::filesystem::path &path, const std::string &pattern, bool return; auto build = Project::Build::create(path); std::string exclude; - if(!build->project_path.empty()) { - project_path = build->project_path; - for(auto &exclude_path : build->get_exclude_paths()) { + for(auto &exclude_folder : build->get_exclude_folders()) #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 - 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 - } - } + if(!build->project_path.empty()) + project_path = build->project_path; else project_path = path; diff --git a/src/project_build.cpp b/src/project_build.cpp index 09fba20..933a0d7 100644 --- a/src/project_build.cpp +++ b/src/project_build.cpp @@ -93,10 +93,22 @@ boost::filesystem::path Project::Build::get_debug_path() { return filesystem::get_normal_path(debug_build_path); } -std::vector Project::Build::get_exclude_paths() { - if(!project_path.empty()) - return {project_path / ".git"}; - return {}; +std::vector Project::Build::get_exclude_folders() { + auto default_build_path = Config::get().project.default_build_path; + boost::replace_all(default_build_path, "", ""); + + auto debug_build_path = Config::get().project.debug_build_path; + boost::replace_all(debug_build_path, "", Config::get().project.default_build_path); + boost::replace_all(debug_build_path, "", ""); + + return std::vector{ + ".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) { @@ -146,13 +158,6 @@ bool Project::CMakeBuild::is_valid() { return true; } -std::vector 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_path = meson.project_path; } @@ -197,39 +202,6 @@ bool Project::MesonBuild::is_valid() { return true; } -std::vector 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 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() { return Config::get().project.cargo_command + " build"; } - -std::vector 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 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 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; -} diff --git a/src/project_build.hpp b/src/project_build.hpp index 16bfb2a..f1677b6 100644 --- a/src/project_build.hpp +++ b/src/project_build.hpp @@ -21,7 +21,7 @@ namespace Project { /// Returns true if the project path reported by build system is correct virtual bool is_valid() { return true; } - virtual std::vector get_exclude_paths(); + std::vector get_exclude_folders(); static std::unique_ptr create(const boost::filesystem::path &path); }; @@ -39,8 +39,6 @@ namespace Project { boost::filesystem::path get_executable(const boost::filesystem::path &path) override; bool is_valid() override; - - std::vector get_exclude_paths() override; }; class MesonBuild : public Build { @@ -56,13 +54,10 @@ namespace Project { boost::filesystem::path get_executable(const boost::filesystem::path &path) override; bool is_valid() override; - - std::vector get_exclude_paths() override; }; class CompileCommandsBuild : public Build { public: - std::vector get_exclude_paths() override; }; class CargoBuild : public Build { @@ -74,17 +69,13 @@ namespace Project { std::string get_compile_command() override; boost::filesystem::path get_executable(const boost::filesystem::path &path) override { return get_debug_path() / project_path.filename(); } - - std::vector get_exclude_paths() override; }; class NpmBuild : public Build { public: - std::vector get_exclude_paths() override; }; class PythonMain : public Build { public: - std::vector get_exclude_paths() override; }; } // namespace Project diff --git a/src/window.cpp b/src/window.cpp index 39df5d2..6826cd8 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -924,7 +924,7 @@ void Window::set_menu_actions() { menu.add_action("source_find_file", []() { auto view_folder = Project::get_preferably_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()) view_folder = build->project_path; @@ -945,8 +945,9 @@ void Window::set_menu_actions() { auto path = iter->path(); // ignore folders 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) { - return path == exclude_path; + auto filename = path.filename(); + if(std::any_of(exclude_folders.begin(), exclude_folders.end(), [&filename](const std::string &exclude_folder) { + return filename == exclude_folder; })) iter.no_push(); continue;