From 6ebc6e12cfa8909655e3c0e722d3c5727ea418d8 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Sun, 18 Apr 2021 21:47:53 +0200 Subject: [PATCH] Add quoted filename support to 'project-find-regexp' (Bug#47799). This is only a band-aid; it would be better to fix xref.el to work with quoted filenames as well. * lisp/progmodes/project.el (project--find-regexp-in-files): Unquote filenames before passing them to 'xref-matches-in-files'. * test/lisp/progmodes/project-tests.el (project/quoted-directory): Also test 'project-find-regexp'. --- lisp/progmodes/project.el | 6 +++++- test/lisp/progmodes/project-tests.el | 31 ++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index 1023b75e668..1d0d1bc58a4 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -787,7 +787,11 @@ pattern to search for." (defun project--find-regexp-in-files (regexp files) (unless files (user-error "Empty file list")) - (let ((xrefs (xref-matches-in-files regexp files))) + (let ((xrefs (xref-matches-in-files + regexp + ;; FIXME: `xref-matches-in-files' should work with + ;; quoted filenames. + (mapcar #'file-name-unquote files)))) (unless xrefs (user-error "No matches for: %s" regexp)) xrefs)) diff --git a/test/lisp/progmodes/project-tests.el b/test/lisp/progmodes/project-tests.el index 829f52adecc..bb58f80d181 100644 --- a/test/lisp/progmodes/project-tests.el +++ b/test/lisp/progmodes/project-tests.el @@ -28,11 +28,15 @@ (require 'project) (require 'ert) +(require 'grep) +(require 'xref) (ert-deftest project/quoted-directory () - "Check that `project-files' deals with quoted directory -names (Bug#47799)." + "Check that `project-files' and `project-find-regexp' deal with +quoted directory names (Bug#47799)." (skip-unless (executable-find find-program)) + (skip-unless (executable-find "xargs")) + (skip-unless (executable-find "grep")) (let ((directory (make-temp-file "project-tests-" :directory))) (unwind-protect (let ((default-directory directory) @@ -42,11 +46,30 @@ names (Bug#47799)." (expand-file-name "projects" directory)) (project (cons 'transient (file-name-quote directory))) (file (expand-file-name "file" directory))) - (make-empty-file file) (add-hook 'project-find-functions (lambda (_dir) project)) (should (eq (project-current) project)) + (write-region "contents" nil file nil nil nil 'excl) (should (equal (project-files project) - (list (file-name-quote file))))) + (list (file-name-quote file)))) + (let* ((references nil) + (xref-search-program 'grep) + (xref-show-xrefs-function + (lambda (fetcher _display) + (push (funcall fetcher) references)))) + (project-find-regexp "tent") + (pcase references + (`((,item)) + (should + ;; FIXME: Shouldn't `xref-match-item' be a subclass of + ;; `xref-item'? + (cl-typep item '(or xref-item xref-match-item))) + (should + (file-equal-p + (xref-location-group (xref-item-location item)) + file))) + (otherwise + (ert-fail (format-message "Unexpected references: %S" + otherwise)))))) (delete-directory directory :recursive)))) ;;; project-tests.el ends here -- 2.39.5