aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEshel Yaron2024-02-01 12:30:24 +0100
committerEshel Yaron2024-02-21 17:47:12 +0100
commite6882a5cc89d9375dfa73156db6836af19ef7b8a (patch)
tree446eca7a76257b0a54e376856e11cb58a31dbab4
parent35d99b1ec7c56d4a5c09af36e6bbd7f0f959cccc (diff)
downloademacs-e6882a5cc89d9375dfa73156db6836af19ef7b8a.tar.gz
emacs-e6882a5cc89d9375dfa73156db6836af19ef7b8a.zip
; Fix mid-symbol updating/cycling completion preview
This fixes an issue where 'completion-preview-next-candidate' would fail to take into account the part of the symbol that follows point (the suffix) when point is at the middle of a symbol, as well as a similar issue in 'completion-preview--show' that would manifest with slow 'completion-at-point-functions'. * lisp/completion-preview.el (completion-preview-next-candidate) (completion-preview--show): Ensure that the completion preview remains at the end of a symbol, when updating it while point is in the middle of that symbol. * test/lisp/completion-preview-tests.el (completion-preview-mid-symbol-cycle): New test. (Bug#68875)
-rw-r--r--lisp/completion-preview.el24
-rw-r--r--test/lisp/completion-preview-tests.el15
2 files changed, 27 insertions, 12 deletions
diff --git a/lisp/completion-preview.el b/lisp/completion-preview.el
index 6fd60f3c416..e827da43a08 100644
--- a/lisp/completion-preview.el
+++ b/lisp/completion-preview.el
@@ -302,21 +302,21 @@ point, otherwise hide it."
302 ;; never display a stale preview and that the preview doesn't 302 ;; never display a stale preview and that the preview doesn't
303 ;; flicker, even with slow completion backends. 303 ;; flicker, even with slow completion backends.
304 (let* ((beg (completion-preview--get 'completion-preview-beg)) 304 (let* ((beg (completion-preview--get 'completion-preview-beg))
305 (end (max (point) (overlay-start completion-preview--overlay)))
305 (cands (completion-preview--get 'completion-preview-cands)) 306 (cands (completion-preview--get 'completion-preview-cands))
306 (index (completion-preview--get 'completion-preview-index)) 307 (index (completion-preview--get 'completion-preview-index))
307 (cand (nth index cands)) 308 (cand (nth index cands))
308 (len (length cand)) 309 (after (completion-preview--get 'after-string))
309 (end (+ beg len)) 310 (face (get-text-property 0 'face after)))
310 (cur (point)) 311 (if (and (<= beg (point) end (1- (+ beg (length cand))))
311 (face (get-text-property 0 'face (completion-preview--get 'after-string)))) 312 (string-prefix-p (buffer-substring beg end) cand))
312 (if (and (< beg cur end) (string-prefix-p (buffer-substring beg cur) cand))
313 ;; The previous preview is still applicable, update it. 313 ;; The previous preview is still applicable, update it.
314 (overlay-put (completion-preview--make-overlay 314 (overlay-put (completion-preview--make-overlay
315 cur (propertize (substring cand (- cur beg)) 315 end (propertize (substring cand (- end beg))
316 'face face 316 'face face
317 'mouse-face 'completion-preview-highlight 317 'mouse-face 'completion-preview-highlight
318 'keymap completion-preview--mouse-map)) 318 'keymap completion-preview--mouse-map))
319 'completion-preview-end cur) 319 'completion-preview-end end)
320 ;; The previous preview is no longer applicable, hide it. 320 ;; The previous preview is no longer applicable, hide it.
321 (completion-preview-active-mode -1)))) 321 (completion-preview-active-mode -1))))
322 ;; Run `completion-at-point-functions' to get a new candidate. 322 ;; Run `completion-at-point-functions' to get a new candidate.
@@ -366,16 +366,16 @@ prefix argument and defaults to 1."
366 (interactive "p") 366 (interactive "p")
367 (when completion-preview-active-mode 367 (when completion-preview-active-mode
368 (let* ((beg (completion-preview--get 'completion-preview-beg)) 368 (let* ((beg (completion-preview--get 'completion-preview-beg))
369 (end (completion-preview--get 'completion-preview-end))
369 (all (completion-preview--get 'completion-preview-cands)) 370 (all (completion-preview--get 'completion-preview-cands))
370 (cur (completion-preview--get 'completion-preview-index)) 371 (cur (completion-preview--get 'completion-preview-index))
371 (len (length all)) 372 (len (length all))
372 (new (mod (+ cur direction) len)) 373 (new (mod (+ cur direction) len))
373 (str (nth new all)) 374 (str (nth new all)))
374 (pos (point))) 375 (while (or (<= (+ beg (length str)) end)
375 (while (or (<= (+ beg (length str)) pos) 376 (not (string-prefix-p (buffer-substring beg end) str)))
376 (not (string-prefix-p (buffer-substring beg pos) str)))
377 (setq new (mod (+ new direction) len) str (nth new all))) 377 (setq new (mod (+ new direction) len) str (nth new all)))
378 (let ((aft (propertize (substring str (- pos beg)) 378 (let ((aft (propertize (substring str (- end beg))
379 'face (if (< 1 len) 379 'face (if (< 1 len)
380 'completion-preview 380 'completion-preview
381 'completion-preview-exact) 381 'completion-preview-exact)
diff --git a/test/lisp/completion-preview-tests.el b/test/lisp/completion-preview-tests.el
index 190764e9125..5b2c28bd3dd 100644
--- a/test/lisp/completion-preview-tests.el
+++ b/test/lisp/completion-preview-tests.el
@@ -181,4 +181,19 @@ instead."
181 (completion-preview--post-command)) 181 (completion-preview--post-command))
182 (completion-preview-tests--check-preview "barbaz" 'exact))) 182 (completion-preview-tests--check-preview "barbaz" 'exact)))
183 183
184(ert-deftest completion-preview-mid-symbol-cycle ()
185 "Test cycling the completion preview with point at the middle of a symbol."
186 (with-temp-buffer
187 (setq-local completion-at-point-functions
188 (list
189 (completion-preview-tests--capf
190 '("foobar" "foobaz"))))
191 (insert "fooba")
192 (forward-char -2)
193 (let ((this-command 'self-insert-command))
194 (completion-preview--post-command))
195 (completion-preview-tests--check-preview "r")
196 (completion-preview-next-candidate 1)
197 (completion-preview-tests--check-preview "z")))
198
184;;; completion-preview-tests.el ends here 199;;; completion-preview-tests.el ends here