1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-21 06:55:39 +00:00

Correctly include fixed strings before a prefix wildcard in PCM

In 03ac16ece4 I fixed a bug with the
PCM implementation of substring completion, relating to the handling
of PCM wildcards.
However, this fix was incomplete.  This change completes the fix by
also including a fixed string if it appears before a 'prefix'
wildcard, even if 'try-completion' doesn't discover that fixed
string grows to a unique completion.
I discovered this bug while working on enhancements to PCM
completion related to 'completion-pcm-leading-wildcard'.
* lisp/minibuffer.el (completion-pcm--merge-completions): Include
fixed strings before 'prefix wildcard.  (Bug#72819)
* test/lisp/minibuffer-tests.el (completion-substring-test-5): Add a
test for this behavior.
This commit is contained in:
Spencer Baugh 2024-08-26 10:12:51 -04:00 committed by Eli Zaretskii
parent 57d93d0259
commit 3cda1fdc3b
2 changed files with 21 additions and 11 deletions

View File

@ -4384,18 +4384,21 @@ the same set of elements."
(unique (or (and (eq prefix t) (setq prefix fixed)) (unique (or (and (eq prefix t) (setq prefix fixed))
(and (stringp prefix) (and (stringp prefix)
(eq t (try-completion prefix comps)))))) (eq t (try-completion prefix comps))))))
;; if the common prefix is unique, it also is a common
;; suffix, so we should add it for `prefix' elements
(unless (or (and (eq elem 'prefix) (not unique))
(equal prefix ""))
(push prefix res))
;; If there's only one completion, `elem' is not useful ;; If there's only one completion, `elem' is not useful
;; any more: it can only match the empty string. ;; any more: it can only match the empty string.
;; FIXME: in some cases, it may be necessary to turn an ;; FIXME: in some cases, it may be necessary to turn an
;; `any' into a `star' because the surrounding context has ;; `any' into a `star' because the surrounding context has
;; changed such that string->pattern wouldn't add an `any' ;; changed such that string->pattern wouldn't add an `any'
;; here any more. ;; here any more.
(unless unique (if unique
;; if the common prefix is unique, it also is a common
;; suffix, so we should add it for `prefix' elements
(push prefix res)
;; `prefix' only wants to include the fixed part before the
;; wildcard, not the result of growing that fixed part.
(when (eq elem 'prefix)
(setq prefix fixed))
(push prefix res)
(push elem res) (push elem res)
;; Extract common suffix additionally to common prefix. ;; Extract common suffix additionally to common prefix.
;; Don't do it for `any' since it could lead to a merged ;; Don't do it for `any' since it could lead to a merged

View File

@ -306,13 +306,20 @@
6))) 6)))
(ert-deftest completion-substring-test-5 () (ert-deftest completion-substring-test-5 ()
;; merge-completions needs to work correctly when ;; Normally a `prefix' wildcard ignores the common prefix to its
;; left, since it only grows the common suffix; but if that common
;; prefix is also a common suffix, it should be included.
(should (equal (should (equal
(completion-pcm--merge-completions '("ab" "sab") '(prefix "b")) (completion-pcm--merge-try '(prefix "b") '("ab" "sab") "" "")
'("b" "a" prefix))) '("ab" . 2)))
(should (equal (should (equal
(completion-pcm--merge-completions '("ab" "ab") '(prefix "b")) (completion-pcm--merge-try '(prefix "b") '("ab" "ab") "" "")
'("b" "a"))) '("ab" . 2)))
;; When there's a fixed string before `prefix', that fixed string
;; should always be included.
(should (equal
(completion-pcm--merge-try '("a" prefix "b") '("axb" "ayb") "" "")
'("ab" . 2)))
;; substring completion should successfully complete the entire string ;; substring completion should successfully complete the entire string
(should (equal (should (equal
(completion-substring-try-completion "b" '("ab" "ab") nil 0) (completion-substring-try-completion "b" '("ab" "ab") nil 0)