Skip to content

Commit 78a3535

Browse files
committed
Make the search prompts suggest and complete what is in the project
The prompts that read something to search for were plain `read-string' calls with one default, sharing the generic minibuffer history. They now go through one reader that knows a little about where it was called from: - M-n offers the function around point, the visited file's name and the last Isearch string after the symbol or region at point. - They keep a history of their own. - TAB completes the word before point against the identifiers that occur in the project, commonest first. The identifiers are looked up with rg (or git grep) for the prefix typed. Listing every identifier of a project up front took 10 to 14 seconds on trees the size of Emacs or OTP, while a prefix lookup stays under half a second there, so nothing is indexed. The prompt stays free text: completion is a completion-at-point function in the minibuffer and not `completing-read', which would let RET pick a candidate over what was typed in the vertical completion UIs. `projectile-find-references' joins the shared prompt, which makes the symbol at point its default instead of its initial input.
1 parent 4e654e7 commit 78a3535

8 files changed

Lines changed: 542 additions & 26 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
### New features
1010

11+
- [#2208](https://github.com/bbatsov/projectile/pull/2208): The search prompts are smarter about what you might be looking for, while staying plain text prompts.
12+
- `TAB` completes the word before point against the identifiers that occur in the project, most common first (`projectile-search-complete-identifiers`).
13+
- `M-n` offers the name of the function around point, the name of the current file and the last Isearch string, after the symbol or region at point.
14+
- They have a history of their own, `projectile-search-history`, instead of sharing the generic minibuffer history.
1115
- [#2182](https://github.com/bbatsov/projectile/pull/2182): Add `projectile-todos-in-projects` and `projectile-multi-occur-in-projects`, so every sibling command now has a public generic taking any list of projects behind it and a command for a group of your own is a two-line wrapper for all five.
1216
- [#2181](https://github.com/bbatsov/projectile/pull/2181): Sapling and Bazaar projects can now list their own ignored files, via the new `projectile-sapling-ignored-command` and `projectile-bzr-ignored-command`.
1317
- [#2180](https://github.com/bbatsov/projectile/pull/2180): Every shell backend can now open in another window (`s-p x 4 <key>`), not just vterm, eat and ghostel: `shell`, `eshell`, `ielm`, `term` and the backend-dispatching `projectile-run` gained `-other-window` commands.
@@ -23,6 +27,7 @@
2327

2428
### Changes
2529

30+
- [#2208](https://github.com/bbatsov/projectile/pull/2208): `projectile-find-references` uses the same prompt as the other search commands, so the symbol at point is the default rather than text already typed into the minibuffer.
2631
- [#2206](https://github.com/bbatsov/projectile/pull/2206): Deleting files inside a project with a persistent cache no longer writes the cache file once per deleted file. The write waits until Emacs is idle, and pending cache writes are flushed when Emacs exits.
2732
- [#2206](https://github.com/bbatsov/projectile/pull/2206): `projectile-find-test-file` is much faster on large projects: the project type and its test conventions are looked up once, not once per file.
2833
- [#2206](https://github.com/bbatsov/projectile/pull/2206): Listing the known projects is faster with `projectile-ignored-projects` set: its entries are symlink-resolved once per listing, not once per project.

‎doc/modules/ROOT/pages/configuration.adoc‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,31 @@ its grep commands once and keeps them. `ugrep` is also one of the programs you
458458
can set `xref-search-program` to.
459459
====
460460

461+
=== The search prompt
462+
463+
Every command that asks what to search for uses the same prompt. It's plain
464+
text - whatever you type is what gets searched for - but it knows a few
465+
things about where you are:
466+
467+
* kbd:[RET] on an empty prompt searches for the region or the symbol at point.
468+
* kbd:[M-n] goes through a few more suggestions: the name of the function
469+
you're in, the name of the current file and the last thing you searched for
470+
with Isearch.
471+
* kbd:[M-p] goes through the terms you've searched for before. The search
472+
prompts have a history of their own (`projectile-search-history`).
473+
* kbd:[TAB] completes the word before point against the identifiers that
474+
occur in the project, most common first.
475+
476+
The identifiers come from running `rg` (or `git grep`, if you don't have
477+
ripgrep) for the prefix you've typed, so there's nothing to index and the
478+
list is never stale. You need to type at least two characters. On a big
479+
project the first kbd:[TAB] can take a moment; if you'd rather not have it:
480+
481+
[source,elisp]
482+
----
483+
(setq projectile-search-complete-identifiers nil)
484+
----
485+
461486
=== Adding your own search backend
462487

463488
Register a backend with `projectile-register-search-backend`. It takes a name,

‎doc/modules/ROOT/pages/configuration_index.adoc‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ and the other reference pages.
287287
| `projectile-search-backend`
288288
| The backend ‘projectile-search’ uses to search the project.
289289

290+
| `projectile-search-complete-identifiers`
291+
| Whether kbd:[TAB] in a search prompt completes the project's identifiers.
292+
290293
| `projectile-search-max-matches`
291294
| Upper bound on how many matches a review buffer collects.
292295

‎projectile.el‎

Lines changed: 219 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8471,20 +8471,225 @@ prompt makes clear which tool will run."
84718471
(format "[%s]" (propertize (format "%s" tool)
84728472
'face 'projectile-search-prompt-tool)))
84738473

8474-
(defun projectile--read-search-string-with-default (prompt-label)
8474+
(defvar projectile-search-history nil
8475+
"History of the terms read by Projectile's search prompts.")
8476+
8477+
(defcustom projectile-search-complete-identifiers t
8478+
"Whether \\`TAB' in a search prompt completes the project's identifiers.
8479+
8480+
When non-nil, the prompts that read something to search for complete the
8481+
word before point against the identifiers that occur in the project,
8482+
most frequent first. The candidates come from a `rg' (or, failing that,
8483+
a `git grep') run for that prefix, so nothing is indexed up front and
8484+
there is a short pause on a large project.
8485+
8486+
The prompt stays free text either way: completion only happens when you
8487+
ask for it, and whatever you type is what gets searched for."
8488+
:group 'projectile
8489+
:type 'boolean
8490+
:package-version '(projectile . "3.5.0"))
8491+
8492+
(defconst projectile--identifier-min-prefix 2
8493+
"How many characters an identifier prefix needs before it is looked up.
8494+
Shorter prefixes match a large part of any project.")
8495+
8496+
(defvar projectile--search-term-map
8497+
(let ((map (make-sparse-keymap)))
8498+
(set-keymap-parent map minibuffer-local-map)
8499+
(define-key map (kbd "TAB") #'completion-at-point)
8500+
map)
8501+
"Keymap of the prompts that read a search term and complete identifiers.
8502+
`minibuffer-local-map' with \\`TAB' completing, as the prompt is free text
8503+
and has no completion of its own.")
8504+
8505+
(defun projectile--identifier-command (prefix roots hyphens)
8506+
"Return the command listing the identifiers under ROOTS that start with PREFIX.
8507+
8508+
The command prints one occurrence per line, each possibly preceded by the
8509+
character in front of it: the pattern has to say where an identifier
8510+
starts itself, as a word boundary would also match after the `-' inside
8511+
one. HYPHENS non-nil lets an identifier contain `-'.
8512+
8513+
Returns nil when PREFIX is not made of identifier characters alone, when
8514+
neither `rg' nor a usable `git' is around, or when ROOTS are remote:
8515+
spawning a process per completion over TRAMP is not worth the wait."
8516+
(when (and (string-match-p "\\`[[:alnum:]_-]+\\'" prefix)
8517+
(not (seq-some #'file-remote-p roots)))
8518+
(cond
8519+
((executable-find "rg")
8520+
(append (list "rg"
8521+
;; the output is parsed, so the user's configuration
8522+
;; must not get to reshape it
8523+
"--no-config"
8524+
"--only-matching" "--no-line-number" "--no-filename"
8525+
"--no-messages" "--color" "never" "--case-sensitive"
8526+
;; dotfiles are project files like any other
8527+
"--hidden" "--glob" "!.git/"
8528+
"--regexp"
8529+
(if hyphens
8530+
(concat "(^|[^\\w-])" prefix "[\\w-]*")
8531+
(concat "(^|\\W)" prefix "\\w*"))
8532+
"--")
8533+
roots))
8534+
((and (executable-find "git")
8535+
(not (cdr roots))
8536+
(eq (projectile-project-vcs (car roots)) 'git))
8537+
(list "git" "--no-pager" "grep" "--only-matching" "-h" "-I"
8538+
"--no-color" "--extended-regexp" "-e"
8539+
(if hyphens
8540+
(concat "(^|[^[:alnum:]_-])" prefix "[[:alnum:]_-]*")
8541+
(concat "(^|[^[:alnum:]_])" prefix "[[:alnum:]_]*")))))))
8542+
8543+
(defun projectile--identifiers (prefix roots hyphens)
8544+
"Return the identifiers under ROOTS that start with PREFIX, commonest first.
8545+
ROOTS is a list of project roots and HYPHENS is as for
8546+
`projectile--identifier-command'. PREFIX is matched case-sensitively, the
8547+
way identifiers are."
8548+
(when-let* (((>= (length prefix) projectile--identifier-min-prefix))
8549+
(command (projectile--identifier-command prefix roots hyphens)))
8550+
(let ((default-directory (car roots))
8551+
(counts (make-hash-table :test 'equal)))
8552+
(with-temp-buffer
8553+
;; the exit status is of no interest: 1 is "nothing matched", and
8554+
;; anything worse leaves nothing to complete, which is fine too
8555+
(ignore-errors
8556+
(apply #'call-process (car command) nil '(t nil) nil (cdr command)))
8557+
(goto-char (point-min))
8558+
(while (not (eobp))
8559+
(let ((match (buffer-substring-no-properties
8560+
(point) (line-end-position))))
8561+
;; drop the character in front of the identifier, and the `-'
8562+
;; of an operator that happened to follow it
8563+
(unless (string-prefix-p prefix match)
8564+
(setq match (substring match (min 1 (length match)))))
8565+
(setq match (string-trim-right match "-+"))
8566+
(when (string-prefix-p prefix match)
8567+
(cl-incf (gethash match counts 0))))
8568+
(forward-line 1)))
8569+
(let (identifiers)
8570+
(maphash (lambda (identifier count) (push (cons identifier count) identifiers))
8571+
counts)
8572+
(mapcar #'car (sort identifiers
8573+
(lambda (a b)
8574+
(or (> (cdr a) (cdr b))
8575+
(and (= (cdr a) (cdr b))
8576+
(string< (car a) (car b)))))))))))
8577+
8578+
(defun projectile--identifier-capf (roots hyphens)
8579+
"Return a `completion-at-point' function for identifiers under ROOTS.
8580+
8581+
It completes the word before point against the identifiers that start
8582+
with it, offered commonest first. The lookup is for that word, whatever
8583+
string a completion style then asks the table about, so the styles filter
8584+
one list instead of each starting a search of their own; the lists are
8585+
remembered for as long as the function is in use.
8586+
8587+
HYPHENS non-nil lets identifiers contain `-'. A word typed with one in it
8588+
is looked up that way regardless."
8589+
(let ((cache (make-hash-table :test 'equal)))
8590+
(lambda ()
8591+
(let* ((end (point))
8592+
(beg (save-excursion
8593+
(skip-chars-backward "[:alnum:]_-" (minibuffer-prompt-end))
8594+
(point)))
8595+
(hyphens (or hyphens
8596+
(and (save-excursion
8597+
(goto-char beg)
8598+
(search-forward "-" end t))
8599+
t)))
8600+
(word (buffer-substring-no-properties beg end))
8601+
(key (cons word hyphens)))
8602+
(when (>= (length word) projectile--identifier-min-prefix)
8603+
(list beg end
8604+
(let ((table (completion-table-dynamic
8605+
(lambda (_string)
8606+
(let ((known (gethash key cache 'unknown)))
8607+
(if (eq known 'unknown)
8608+
(puthash key
8609+
(projectile--identifiers
8610+
word roots hyphens)
8611+
cache)
8612+
known))))))
8613+
(lambda (string predicate action)
8614+
(if (eq action 'metadata)
8615+
'(metadata (category . projectile-identifier)
8616+
(display-sort-function . identity)
8617+
(cycle-sort-function . identity))
8618+
(complete-with-action action table string predicate))))))))))
8619+
8620+
(defun projectile--search-term-default ()
8621+
"Return the region or the symbol at point, or nil when there is neither.
8622+
This is what a search prompt searches for when nothing is typed."
8623+
(let ((default (projectile-symbol-or-selection-at-point)))
8624+
(unless (or (null default) (string-blank-p default))
8625+
default)))
8626+
8627+
(defun projectile--search-term-suggestions ()
8628+
"Return what the current buffer suggests searching for, likeliest first.
8629+
That is the region or the symbol at point, the name of the function
8630+
around point, the base name of the visited file and the last thing
8631+
searched for with Isearch. These are what \\`M-n' offers in a search
8632+
prompt."
8633+
(delete-dups
8634+
(seq-remove
8635+
(lambda (suggestion) (or (null suggestion) (string-blank-p suggestion)))
8636+
(list (projectile-symbol-or-selection-at-point)
8637+
(ignore-errors (add-log-current-defun))
8638+
(and buffer-file-name (file-name-base buffer-file-name))
8639+
(and (car search-ring)
8640+
(substring-no-properties (car search-ring)))))))
8641+
8642+
(defun projectile--read-search-term (prompt &optional roots)
8643+
"Read something to search for with PROMPT and return it.
8644+
8645+
The prompt is free text. \\`M-n' goes through what the current buffer
8646+
suggests (see `projectile--search-term-suggestions') and \\`M-p' through
8647+
`projectile-search-history'. Empty input stands for the region or the
8648+
symbol at point. With
8649+
`projectile-search-complete-identifiers', \\`TAB' completes the word
8650+
before point against the identifiers of the projects at ROOTS, which
8651+
defaults to the current project."
8652+
(let* ((suggestions (projectile--search-term-suggestions))
8653+
(roots (mapcar #'expand-file-name
8654+
(or roots (ensure-list (projectile-project-root)))))
8655+
(complete (and projectile-search-complete-identifiers roots))
8656+
;; `-' is an operator only where a programming mode says so. A
8657+
;; README or a Dired buffer says nothing about the language of the
8658+
;; project, and leaving hyphens out there would cut every Lisp or
8659+
;; CSS identifier short.
8660+
(hyphens (not (and (derived-mode-p 'prog-mode)
8661+
(not (memq (char-syntax ?-) '(?w ?_))))))
8662+
(term (minibuffer-with-setup-hook
8663+
(lambda ()
8664+
(when complete
8665+
(add-hook 'completion-at-point-functions
8666+
(projectile--identifier-capf roots hyphens)
8667+
nil t)))
8668+
(read-from-minibuffer
8669+
prompt nil
8670+
;; without completion `TAB' keeps inserting a tab
8671+
(if complete projectile--search-term-map minibuffer-local-map)
8672+
nil 'projectile-search-history suggestions))))
8673+
(if (string-empty-p term)
8674+
(or (projectile--search-term-default) "")
8675+
term)))
8676+
8677+
(defun projectile--read-search-string-with-default (prompt-label &optional roots)
84758678
"Read a search string, defaulting to the symbol or region at point.
84768679
PROMPT-LABEL is the action shown in the prompt (already carrying any
84778680
faced tool tag its caller wants). The default value, when there is one,
8478-
is appended and faced as `(default VALUE)'."
8681+
is appended and faced as `(default VALUE)'. ROOTS is the projects being
8682+
searched, when that is not just the current one. See
8683+
`projectile--read-search-term' for what the prompt offers."
84798684
(let* ((prompt-label (projectile-prepend-project-name prompt-label))
8480-
(default-value (projectile-symbol-or-selection-at-point))
8481-
(default-label (if (or (not default-value)
8482-
(string= default-value ""))
8483-
""
8484-
(format " (default: %s)"
8485-
(propertize default-value
8486-
'face 'projectile-search-prompt-default)))))
8487-
(read-string (format "%s%s: " prompt-label default-label) nil nil default-value)))
8685+
(default-value (projectile--search-term-default))
8686+
(default-label (if default-value
8687+
(format " (default: %s)"
8688+
(propertize default-value
8689+
'face 'projectile-search-prompt-default))
8690+
"")))
8691+
(projectile--read-search-term
8692+
(format "%s%s: " prompt-label default-label) roots)))
84888693

84898694
;;; Grep command construction
84908695
;;
@@ -8993,9 +9198,8 @@ Projectile project too when `projectile-mode' is enabled."
89939198
(require 'xref)
89949199
(let* ((project-root (projectile-acquire-root))
89959200
(symbol (or symbol
8996-
(read-string
8997-
(projectile-prepend-project-name "Find references to: ")
8998-
(projectile-symbol-or-selection-at-point))))
9201+
(projectile--read-search-string-with-default
9202+
"Find references to")))
89999203
;; `xref-matches-in-directory' reads its IGNORES in project.el's
90009204
;; format, not gitignore's.
90019205
(ignores (mapcar #'projectile--project-el-ignore-glob
@@ -15519,7 +15723,8 @@ them in turn when ripgrep is available."
1551915723
(or prompt (format "Search %d project%s%s for"
1552015724
(length projects)
1552115725
(if (cdr projects) "s" "")
15522-
(if literal "" " regexp")))))
15726+
(if literal "" " regexp")))
15727+
projects))
1552315728
(regexp (if literal (regexp-quote term) term))
1552415729
(case-fold case-fold-search)
1552515730
(candidates (lambda ()

‎test/projectile-grep-test.el‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@
4141
;; the gitignore patterns are respelled in project.el's format
4242
"foo\\.bar" "*" "/my/root/" '("*.elc" "./build/"))))
4343

44+
(describe "projectile-find-references prompt"
45+
(it "asks with the shared search prompt when no symbol is given"
46+
(spy-on 'projectile-acquire-root :and-return-value "/my/root/")
47+
(spy-on 'projectile--project-ignore-globs :and-return-value nil)
48+
(spy-on 'xref-matches-in-directory :and-return-value nil)
49+
(spy-on 'xref-show-xrefs :and-call-fake
50+
(lambda (fetcher &rest _) (funcall fetcher)))
51+
(spy-on 'projectile--read-search-string-with-default :and-return-value "foo")
52+
(projectile-find-references)
53+
(expect 'projectile--read-search-string-with-default
54+
:to-have-been-called-with "Find references to")
55+
(expect (car (spy-calls-args-for 'xref-matches-in-directory 0))
56+
:to-equal "foo")))
57+
4458
(describe "projectile--ripgrep-ignore-globs"
4559
(it "builds unquoted --glob=! exclusions that also work on Windows (#1946)"
4660
(spy-on 'projectile--dirconfig-ignore :and-return-value '("*.log"))
@@ -346,9 +360,9 @@
346360
(current-prefix-arg '-)
347361
(sym (cadr test)))
348362
(spy-on 'projectile-project-vcs :and-return-value 'git)
349-
(spy-on 'read-string :and-call-fake
350-
(lambda (prompt initial-input history default-value &rest args)
351-
(if (should (equal sym default-value)) default-value)))
363+
(spy-on 'read-from-minibuffer :and-call-fake
364+
(lambda (_prompt _initial _keymap _read _history defaults &rest _)
365+
(if (should (equal sym (car defaults))) (car defaults))))
352366
(spy-on 'vc-git-grep :and-call-fake
353367
(lambda (regexp files dir)
354368
(progn (expect regexp :to-equal sym)

‎test/projectile-project-group-test.el‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ the two truename'd roots and `parent' to the directory holding them."
6161

6262
(defun projectile-group-test--search (projects term)
6363
"Run `projectile-search-in-projects' over PROJECTS for literal TERM."
64-
(spy-on 'read-string :and-call-fake (lambda (&rest _) term))
64+
(spy-on 'read-from-minibuffer :and-call-fake (lambda (&rest _) term))
6565
(cl-letf (((symbol-function 'pop-to-buffer) #'ignore))
6666
(projectile-search-in-projects projects t))
6767
(get-buffer projectile-search-buffer-name))
@@ -284,7 +284,7 @@ the two truename'd roots and `parent' to the directory holding them."
284284
(projectile-group-test--with-projects
285285
(projectile-test-use-plain-grep)
286286
(spy-on 'projectile-sibling-projects :and-return-value (list alpha beta))
287-
(spy-on 'read-string :and-call-fake (lambda (&rest _) "needle"))
287+
(spy-on 'read-from-minibuffer :and-call-fake (lambda (&rest _) "needle"))
288288
(cl-letf (((symbol-function 'pop-to-buffer) #'ignore))
289289
(projectile-search-in-sibling-projects))
290290
(expect (projectile-test-match-files

0 commit comments

Comments
 (0)