unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 68875@debbugs.gnu.org
Subject: bug#68875: [PATCH] ; Fix mid-symbol updating/cycling completion preview
Date: Thu, 01 Feb 2024 18:06:56 +0100	[thread overview]
Message-ID: <m1msskgllb.fsf@dazzs-mbp.home> (raw)

[-- Attachment #1: Type: text/plain, Size: 704 bytes --]

Tags: patch

The attached patch fixes an issue where the completion preview overlay
(of Completion Preview mode) could at certain cases appear in the wrong
place when point is in the middle of a symbol.

To observe the effect of this patch:

1. emacs -Q
2. In the *scratch* buffer, say M-x completion-preview-mode RET
3. Type "defaul-di"
4. C-3 C-b to place point before the hyphen
5. Type "t"
   The completion preview overlay appears after "-di", showing "rectory".
   So far so good.
6. M-x completion-preview-next-candidate RET
   Before this patch, the preview now shows "-directory", thus repeating
   the existing suffix "-di".  With this patch, the preview shows just
   "rectory", as expected.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-mid-symbol-updating-cycling-completion-preview.patch --]
[-- Type: text/x-patch, Size: 5112 bytes --]

From e64361b0fbd78bb48b859658f9d0c1bb36916f50 Mon Sep 17 00:00:00 2001
From: Eshel Yaron <me@eshelyaron.com>
Date: Thu, 1 Feb 2024 12:30:24 +0100
Subject: [PATCH] ; 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): Use recorded 'completion-preview-end'
position instead of current point.

* test/lisp/completion-preview-tests.el (completion-preview-mid-symbol-cycle):
New test.
---
 lisp/completion-preview.el            | 24 ++++++++++++------------
 test/lisp/completion-preview-tests.el | 15 +++++++++++++++
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/lisp/completion-preview.el b/lisp/completion-preview.el
index 6fd60f3c416..d7d3e9bf9f7 100644
--- a/lisp/completion-preview.el
+++ b/lisp/completion-preview.el
@@ -302,21 +302,21 @@ completion-preview--show
     ;; never display a stale preview and that the preview doesn't
     ;; flicker, even with slow completion backends.
     (let* ((beg (completion-preview--get 'completion-preview-beg))
+           (end (max (point) (completion-preview--get 'completion-preview-end)))
            (cands (completion-preview--get 'completion-preview-cands))
            (index (completion-preview--get 'completion-preview-index))
            (cand (nth index cands))
-           (len (length cand))
-           (end (+ beg len))
-           (cur (point))
-           (face (get-text-property 0 'face (completion-preview--get 'after-string))))
-      (if (and (< beg cur end) (string-prefix-p (buffer-substring beg cur) cand))
+           (after (completion-preview--get 'after-string))
+           (face (get-text-property 0 'face after)))
+      (if (and (<= beg (point) end (1- (+ beg (length cand))))
+               (string-prefix-p (buffer-substring beg end) cand))
           ;; The previous preview is still applicable, update it.
           (overlay-put (completion-preview--make-overlay
-                        cur (propertize (substring cand (- cur beg))
+                        end (propertize (substring cand (- end beg))
                                         'face face
                                         'mouse-face 'completion-preview-highlight
                                         'keymap completion-preview--mouse-map))
-                       'completion-preview-end cur)
+                       'completion-preview-end end)
         ;; The previous preview is no longer applicable, hide it.
         (completion-preview-active-mode -1))))
   ;; Run `completion-at-point-functions' to get a new candidate.
@@ -366,16 +366,16 @@ completion-preview-next-candidate
   (interactive "p")
   (when completion-preview-active-mode
     (let* ((beg (completion-preview--get 'completion-preview-beg))
+           (end (completion-preview--get 'completion-preview-end))
            (all (completion-preview--get 'completion-preview-cands))
            (cur (completion-preview--get 'completion-preview-index))
            (len (length all))
            (new (mod (+ cur direction) len))
-           (str (nth new all))
-           (pos (point)))
-      (while (or (<= (+ beg (length str)) pos)
-                 (not (string-prefix-p (buffer-substring beg pos) str)))
+           (str (nth new all)))
+      (while (or (<= (+ beg (length str)) end)
+                 (not (string-prefix-p (buffer-substring beg end) str)))
         (setq new (mod (+ new direction) len) str (nth new all)))
-      (let ((aft (propertize (substring str (- pos beg))
+      (let ((aft (propertize (substring str (- end beg))
                              'face (if (< 1 len)
                                        'completion-preview
                                      '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 @@ completion-preview-capf-errors
       (completion-preview--post-command))
     (completion-preview-tests--check-preview "barbaz" 'exact)))
 
+(ert-deftest completion-preview-mid-symbol-cycle ()
+  "Test cycling the completion preview with point at the middle of a symbol."
+  (with-temp-buffer
+    (setq-local completion-at-point-functions
+                (list
+                 (completion-preview-tests--capf
+                  '("foobar" "foobaz"))))
+    (insert "fooba")
+    (forward-char -2)
+    (let ((this-command 'self-insert-command))
+      (completion-preview--post-command))
+    (completion-preview-tests--check-preview "r")
+    (completion-preview-next-candidate 1)
+    (completion-preview-tests--check-preview "z")))
+
 ;;; completion-preview-tests.el ends here
-- 
2.42.0


             reply	other threads:[~2024-02-01 17:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-01 17:06 Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-02-02 12:54 ` bug#68875: [PATCH] ; Fix mid-symbol updating/cycling completion preview Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-21 16:59   ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m1msskgllb.fsf@dazzs-mbp.home \
    --to=bug-gnu-emacs@gnu.org \
    --cc=68875@debbugs.gnu.org \
    --cc=me@eshelyaron.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).