projectile怎么忽略目录/文件?

例如忽略target vendor *.lock 我尝试了设置projectile-globally-ignored-directories和在.projectile中加入

-target 
-vendor 
-*.lock

但都无效

如果是在 find 的时候忽略应该直接加参数就可以, 不过我没用过 projectile,不知道这个怎么弄

说个可能不符合答案的: projectile 会自动读取当前 project 下的 .gitignore 会自动根据这个文件作忽略。

这是我目前的配置

(setq projectile-switch-project-action #'projectile-find-file-dwim
        projectile-completion-system 'ivy
        ;; projectile-enable-caching t
        projectile-project-root-files-functions #'(projectile-root-top-down
                                                   projectile-root-top-down-recurring
                                                   projectile-root-bottom-up
                                                   projectile-root-local)
        projectile-ignored-project-function (lambda (project-root)
                                              (cl-dolist (deny '("\\.git" "\\.rustup" "\\.cargo" "go/pkg" "vendor" ".emacs.d/ignore" ".emacs.d/elpa"))
                                                (when (string-match-p deny project-root)
                                                  (cl-return t)))))

在tg群里问到办法了(setq projectile-indexing-method 'hybrid projectile-enable-caching t)

你可能是因为重启或刷新了 cache 碰巧解决了问题。而且很可能是之前早就开启了缓存,所以导致 projectile-globally-ignored-directories 没有立即生效,从你描述的现象看,这种乌龙的可能性更大。

要想证明问题的存在,最好是创建可重复的方法:

⋊> emacsq.sh -P projectile,ido -M ido-mode --eval "\
   (add-hook 'emacs-startup-hook
             (lambda ()
               (toggle-debug-on-error)
               (view-echo-area-messages)
               (let ((default-directory (make-temp-file \"test-projectile--\" 'dir \"/\")))
                 (with-temp-buffer
                   (unless (zerop (call-process-shell-command \"touch Cask file1 file2 ; mkdir ignore-me\" nil))
                     (error (buffer-string))))
                 (add-to-list 'projectile-globally-ignored-directories \"ignore-me\" )
                 (with-current-buffer \"*Messages*\"
                   (toggle-truncate-lines)
                   (let ((inhibit-read-only t)) (erase-buffer)))
                 (message \"   ignored dirs: %S\" projectile-globally-ignored-directories)
                 (message \"directory files: %S\" (directory-files default-directory))
                 (message \"  project files: %S\" (projectile-project-files default-directory))
                 (call-interactively 'projectile-find-file))))" -nw --debug-init

这段代码里没有设置 index method 和 cache,但过忽略规则有效:

test-emacs--projectile-ignore-dires

emacsq.sh 脚本在这里

不是的,只是因为没设置projectile-indexing-method 'hybrid。 设置cache这个我只是想让它更快一点,不然实在是太卡了,,projectile-find-file 能卡个两秒,,,

你运行我上边没有设置 indexing method 的脚本,看看 hybrid 是不是必须。

我的环境中 projectile-indexing-method 是alien,一样是有效的

2 个赞

projectile 这方面的行为挺迷惑的, 很久之前折腾过, 结论似乎是这样:

native indexing 全部用 elisp 实现, projectile-globally-ignored-directoriesprojectile-sort-order 这类变量, 以及 .projectile 文件的配置可以生效. 但是 .gitignore 不影响结果.

alien 完全依赖外部命令 (git, 如果不是 git repo 就用 find), elisp 这边除了展示不做任何处理. 所以 .gitignore 会生效, 但是 elisp 变量和 .projectile 文件都不生效.

hybrid 应该是先用外部命令, 然后通过 elisp 做筛选和排序, 所以 .gitignore, elisp 变量和 .projectile 文件都生效.

参考:

https://docs.projectile.mx/projectile/configuration.html#project-indexing-method https://docs.projectile.mx/projectile/2.2/projects.html#ignoring-files

1 个赞

刚测试了下,这个回答是最全面的。alien确实不行,因为在.ignore中也设置了看起来是可以。alien最快,hybrid其次,native最慢。为了速度还是选了alien。

我尝试emacs -q设置projectile-globally-ignored-directories让它忽略vendor,但是好像并没有什么用,projectile-find-file还是会出现vendor的东西

From Projects :: Projectile

The contents of .projectile are ignored when using the alien project indexing method.

1 个赞

我无法重现这个问题:

⋊> ls -a
./  ../  .git/  .projectile  LICENSE  README.md  emacsq.sh*  ignore-me/

⋊> cat .projectile
-/ignore-me

⋊> emacsq.sh -P projectile,ido -M ido-mode --eval "\
   (add-hook 'emacs-startup-hook
             (lambda ()
               (projectile-mode 1)
               (cl-assert (equal projectile-indexing-method 'alien))
               (cl-assert (not (member \"ignore-me\" (projectile-project-files default-directory))))
               (call-interactively 'projectile-find-file)))" -nw

Pasted_Image_20_05_2021__5_12_PM

1 个赞

可能是你的目录下没有.gitignore所以.projectile会生效?

1 个赞

projectile-project-files 这个函数用了 find . -type f -print0, 所以本来就没有 directory. 可以试下在 ignore-me 里面创建个文件, 感觉应该不会被 ignore 掉.

1 个赞

结果如你所说,不过命令是 git ls-files -zco --exclude-standard

1 个赞