]> git.eshelyaron.com Git - completion-preview.git/commitdiff
Use a trivial completion table instead of a bespoke function
authorEshel Yaron <me@eshelyaron.com>
Wed, 1 Nov 2023 21:32:36 +0000 (22:32 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 1 Nov 2023 21:32:36 +0000 (22:32 +0100)
Completion at point functions are encouraged to return well formed
completion table with metadata instead of a completion function.

* completion-preview.el (completion-preview-show): Simplify.
(completion-preview-insert-function):
(completion-preview--insert) Remove.
(completion-preview-insert): New function.
(completion-preview-active-mode): Adapt.

completion-preview.el

index ad3f9cf3a4c6b2d4a5e5c8da195cb277a0213cf9..9c6b8ea2341876d4165389a29d3795f9995fdf5f 100644 (file)
@@ -125,21 +125,20 @@ Compatibility definition for `minibuffer--sort-by-length-alpha'."
               (prefix (substring string base)))
          (when last
            (setcdr last nil)
-           (let* ((filtered (seq-filter (apply-partially #'string-prefix-p prefix)
-                                        all))
+           (let* ((filtered
+                   (seq-filter (apply-partially #'string-prefix-p prefix) all))
                   (sorted (funcall sort-fn filtered))
                   (multi (cadr sorted)) ; multiple candidates
                   (cand (car sorted)))
-             (when (and cand
-                        (not (and multi completion-preview-exact-match-only)))
+             (when (and cand (not (and multi completion-preview-exact-match-only)))
                (let* ((face (if multi 'completion-preview 'completion-preview-exact))
                       (after (propertize (substring cand (length prefix)) 'face face)))
                  (unless (string-empty-p after)
                    (add-text-properties 0 1 '(cursor 1) after)
                    (setq completion-preview--overlay (make-overlay end end))
                    (overlay-put completion-preview--overlay 'after-string after)
-                   (overlay-put completion-preview--overlay 'completion-preview-plist plist)
-                   (overlay-put completion-preview--overlay 'completion-preview-string cand)
+                   (overlay-put completion-preview--overlay 'completion-preview-data
+                                (append (list beg end (list cand)) plist))
                    (completion-preview-active-mode)))))))))))
 
 (defun completion-preview--post-command ()
@@ -149,33 +148,19 @@ Compatibility definition for `minibuffer--sort-by-length-alpha'."
     (while-no-input
       (completion-preview-show))))
 
-(defun completion-preview--insert ()
-  "Insert the completion preview suggestion."
-  (let ((after (substring-no-properties
-                (overlay-get completion-preview--overlay 'after-string)))
-        (string (overlay-get completion-preview--overlay
-                             'completion-preview-string))
-        (plist (overlay-get completion-preview--overlay
-                            'completion-preview-plist)))
-    (completion-preview-active-mode -1)
-    (insert after)
-    (when-let ((exit-fn (plist-get plist :exit-function)))
-      (funcall exit-fn string 'finished))))
-
-(defun completion-preview-insert-function ()
-  "Return a function inserting the completion preview."
-  #'completion-preview--insert)
-
 (define-minor-mode completion-preview-active-mode
   "Mode for when the completion preview is active."
   :interactive nil
   (if completion-preview-active-mode
-      (add-hook 'completion-at-point-functions
-                #'completion-preview-insert-function -1 t)
-    (remove-hook 'completion-at-point-functions
-                 #'completion-preview-insert-function t)
+      (add-hook 'completion-at-point-functions #'completion-preview-insert -1 t)
+    (remove-hook 'completion-at-point-functions #'completion-preview-insert t)
     (completion-preview-hide)))
 
+(defun completion-preview-insert ()
+  "Completion at point function for inserting the current preview."
+  (when completion-preview-active-mode
+    (overlay-get completion-preview--overlay 'completion-preview-data)))
+
 ;;;###autoload
 (define-minor-mode completion-preview-mode
   "Show in-buffer completion preview as you type."