From 1bbcc10c5b23d63dc8454113403c2d834a69d803 Mon Sep 17 00:00:00 2001 From: Jules Tamagnan Date: Sat, 22 Jun 2024 11:40:09 -0700 Subject: [PATCH 2/2] [Cont] Add new completion-preview-insert-{word,sexp} commands --- lisp/completion-preview.el | 37 ++++++++++++++++++++------- test/lisp/completion-preview-tests.el | 4 +-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lisp/completion-preview.el b/lisp/completion-preview.el index 3a7fa37afe0..637778caadb 100644 --- a/lisp/completion-preview.el +++ b/lisp/completion-preview.el @@ -456,18 +456,37 @@ completion-preview--insert (end (completion-preview--get 'completion-preview-end)) (efn (plist-get (completion-preview--get 'completion-preview-props) :exit-function)) - (ful (completion-preview--get 'after-string)) - (aft (with-temp-buffer - (insert ful) + (aft (completion-preview--get 'after-string)) + (ful (with-temp-buffer + (insert aft) (goto-char (point-min)) (funcall action) - (buffer-substring-no-properties (point-min) (point))))) - (completion-preview-active-mode -1) + (cons (buffer-substring-no-properties (point-min) (point)) + (buffer-substring (point) (point-max))))) + (ins (car ful)) + (suf (cdr ful))) + ;; If the completion is a full completion (there is no suffix) + ;; deactivate the preview + (when (string-empty-p suf) + (completion-preview-active-mode -1)) + + ;; Insert the new text (goto-char end) - (insert aft) - (when (and (functionp efn) (string= ful aft)) - ;; If we've inserted a full completion call the exit-function - (funcall efn (concat (buffer-substring-no-properties beg end) aft) 'finished))) + (insert ins) + + ;; If we are not inserting a full completion update the preview + (when (not (string-empty-p suf)) + (let ((pos (point))) + (completion-preview--inhibit-update) + (overlay-put (completion-preview--make-overlay + pos (propertize suf + 'mouse-face 'completion-preview-highlight + 'keymap completion-preview--mouse-map)) + 'completion-preview-end pos))) + + ;; If we've inserted a full completion call the exit-function + (when (and (functionp efn) (string-empty-p suf)) + (funcall efn (concat (buffer-substring-no-properties beg end) ins) 'finished))) (user-error "No current completion preview"))) (defun completion-preview-insert () diff --git a/test/lisp/completion-preview-tests.el b/test/lisp/completion-preview-tests.el index dedd135da73..54ba566ad3c 100644 --- a/test/lisp/completion-preview-tests.el +++ b/test/lisp/completion-preview-tests.el @@ -325,7 +325,7 @@ completion-preview-insert-word (completion-preview-tests--check-preview "bar-1 2" 'completion-preview-common) (completion-preview-insert-word) (should (string= (buffer-string) "foobar")) - (should-not completion-preview--overlay) + (completion-preview-tests--check-preview "-1 2" 'completion-preview) (should-not exit-fn-called) (should-not exit-fn-args)))) @@ -347,7 +347,7 @@ completion-preview-insert-sexp (completion-preview-tests--check-preview "bar-1 2" 'completion-preview-common) (completion-preview-insert-sexp) (should (string= (buffer-string) "foobar-1")) - (should-not completion-preview--overlay) + (completion-preview-tests--check-preview " 2" 'completion-preview) (should-not exit-fn-called) (should-not exit-fn-args)))) -- 2.45.1