(cl-defmethod project-root ((project (head transient)))
(cdr project))
+(defvar project-files-relative-names nil
+ "When non-nil, `project-files' is allowed to return relative names.
+The names will be relative to the project root. And this can only
+happen when all returned files are in the same directory. Meaning, the
+DIRS argument has to be nil or have only one element.")
+
(cl-defgeneric project-files (project &optional dirs)
"Return a list of files in directories DIRS in PROJECT.
DIRS is a list of absolute directories; it should be some
;; expanded and not left for the shell command
;; to interpret.
(localdir (file-name-unquote (file-local-name (expand-file-name dir))))
- (dfn (directory-file-name localdir))
(command (format "%s -H . %s -type f %s -print0"
find-program
(xref--find-ignores-arguments ignores "./")
(error "File listing failed: %s" (buffer-string))))
(goto-char pt)
(while (search-forward "\0" nil t)
- (push (buffer-substring-no-properties (1+ pt) (1- (point)))
+ (push (buffer-substring-no-properties (+ pt 2) (1- (point)))
res)
(setq pt (point)))))
- (project--remote-file-names
- (mapcar (lambda (s) (concat dfn s))
- (sort res #'string<)))))
+ (if project-files-relative-names
+ (sort res #'string<)
+ (project--remote-file-names
+ (mapcar (lambda (s) (concat localdir s))
+ (sort res #'string<))))))
(defun project--remote-file-names (local-files)
"Return LOCAL-FILES as if they were on the system of `default-directory'.
(mapcar
(lambda (file)
(unless (member file submodules)
- (concat default-directory file)))
+ (if project-files-relative-names
+ file
+ (concat default-directory file))))
(split-string
(apply #'vc-git--run-command-string nil "ls-files" args)
"\0" t))))
dir))
(args (list (concat "-mcard" (and include-untracked "u"))
"--no-status"
- "-0")))
+ "-0"))
+ files)
(when extra-ignores
(setq args (nconc args
(mapcan
extra-ignores))))
(with-temp-buffer
(apply #'vc-hg-command t 0 "." "status" args)
- (mapcar
- (lambda (s) (concat default-directory s))
- (split-string (buffer-string) "\0" t)))))))
+ (setq files (split-string (buffer-string) "\0" t))
+ (unless project-files-relative-names
+ (setq files (mapcar
+ (lambda (s) (concat default-directory s))
+ files)))
+ files)))))
(defun project--vc-merge-submodules-p (dir)
(project--value-in-dir
(cl-defmethod xref-backend-restore ((_backend (head project-dir)) _context))
(cl-defmethod xref-backend-apropos ((backend (head project-dir)) pattern)
(project--find-regexp-in-files
- pattern (project--files-in-directory (nth 1 backend) nil (nth 2 backend))))
+ pattern
+ (let ((project-files-relative-names t))
+ (project--files-in-directory (nth 1 backend) nil (nth 2 backend)))))
(cl-defmethod xref-backend-context ((_backend (head project-ext)) _id _kind))
(cl-defmethod xref-backend-restore ((_backend (head project-ext)) _context))
(cl-defmethod xref-backend-apropos ((backend (head project-ext)) pattern)
(let ((pr (cdr backend)))
(project--find-regexp-in-files
- pattern (project-files pr (cons (project-root pr)
- (project-external-roots pr))))))
+ pattern
+ ;; TODO: Make use of `project-files-relative-names' by
+ ;; searching each root separately (maybe in parallel, too).
+ (project-files pr (cons (project-root pr)
+ (project-external-roots pr))))))
;;;###autoload
(defun project-find-regexp (regexp &optional dir pattern)
(interactive "P")
(let* ((pr (project-current t))
(root (project-root pr))
- (dirs (list root)))
+ (dirs (list root))
+ (project-files-relative-names t))
(project-find-file-in
(or (thing-at-point 'filename)
(and buffer-file-name (project--find-default-from buffer-file-name pr)))
(if (> (length common-prefix) 0)
(file-name-directory common-prefix))))
(cpd-length (length common-parent-directory))
- (prompt (if (zerop cpd-length)
+ (common-parent-directory (if (file-name-absolute-p (car all-files))
+ common-parent-directory
+ (concat default-directory common-parent-directory)))
+ (prompt (if (and (zerop cpd-length)
+ all-files
+ (file-name-absolute-p (car all-files)))
prompt
(concat prompt (format " in %s" common-parent-directory))))
(included-cpd (when (member common-parent-directory all-files)
(defun project--read-file-absolute (prompt
all-files &optional predicate
hist mb-default)
- (project--completing-read-strict prompt
- (project--file-completion-table all-files)
- predicate
- hist mb-default))
+ (let* ((new-prompt (if (file-name-absolute-p (car all-files))
+ prompt
+ (concat prompt " in " default-directory)))
+ ;; FIXME: Map relative names to absolute?
+ (ct (project--file-completion-table all-files))
+ (file
+ (project--completing-read-strict new-prompt
+ ct
+ predicate
+ hist mb-default)))
+ (unless (file-name-absolute-p file)
+ (setq file (expand-file-name file)))
+ file))
(defun project--read-file-name ( project prompt
all-files &optional predicate
dirs)
(project-files project dirs)))
(completion-ignore-case read-file-name-completion-ignore-case)
+ (default-directory (project-root project))
(file (project--read-file-name
project "Find file"
all-files nil 'file-name-history
(should-not (null project))
(should (string-match-p "/test/lisp/progmodes/project-resources/\\'" (project-root project)))))
+(ert-deftest project-find-regexp ()
+ "Check the happy path."
+ (skip-unless (executable-find find-program))
+ (skip-unless (executable-find "xargs"))
+ (skip-unless (executable-find "grep"))
+ (let* ((directory (ert-resource-directory))
+ (project-find-functions nil)
+ (project (cons 'transient directory)))
+ (add-hook 'project-find-functions (lambda (_dir) project))
+ (should (eq (project-current) project))
+ (let* ((matches nil)
+ (xref-search-program 'grep)
+ (xref-show-xrefs-function
+ (lambda (fetcher _display)
+ (setq matches (funcall fetcher)))))
+ (project-find-regexp "etc")
+ (should (equal (mapcar (lambda (item)
+ (file-name-base
+ (xref-location-group (xref-item-location item))))
+ matches)
+ '(".dir-locals" "etc")))
+ (should (equal (sort (mapcar #'xref-item-summary matches) #'string<)
+ '("((nil . ((project-vc-ignores . (\"etc\")))))" "etc"))))))
+
;;; project-tests.el ends here