summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mendler <mail@daniel-mendler.de>2023-12-08 08:20:46 +0100
committerDaniel Mendler <mail@daniel-mendler.de>2023-12-08 08:21:01 +0100
commita878347ebd20e4e410133f3547f16a925b7c6d16 (patch)
treee2d20668e1f58d818329204d3daa797fe435433c
parente6235535281cd7f40213ad78ed9f03e8d078fbff (diff)
cape-company-to-capf: Overhaul candidate lookup in :exit-function
The :exit-function looks up the candidate in order to restore text properties. This lookup is not necessary with Corfu, which guarantees that the candidates are propertized. The lookup even hurts since duplicate candidates lose their specificity, they would get replaced by their first occurrence in the candidate list.
-rw-r--r--cape.el27
1 files changed, 17 insertions, 10 deletions
diff --git a/cape.el b/cape.el
index 4c9ec3c..531e4a8 100644
--- a/cape.el
+++ b/cape.el
@@ -786,7 +786,7 @@ changed. The function `cape-company-to-capf' is experimental."
(let* ((end (point)) (beg (- end (length initial-input)))
(valid (if (cape--company-call backend 'no-cache initial-input)
#'equal (or valid #'string-prefix-p)))
- candidates)
+ restore-props)
(list beg end
(funcall
(if (cape--company-call backend 'ignore-case)
@@ -796,8 +796,15 @@ changed. The function `cape-company-to-capf' is experimental."
(cape--dynamic-table
beg end
(lambda (input)
- (setq candidates (cape--company-call backend 'candidates input))
- (cons (apply-partially valid input) candidates)))
+ (let ((cands (cape--company-call backend 'candidates input)))
+ ;; The candidate string including text properties should be
+ ;; restored in the :exit-function, if the UI does not
+ ;; guarantee this itself. Restoration is not necessary for
+ ;; Corfu since the introduction of `corfu--exit-function'.
+ (unless (and (eq completion-in-region-function 'corfu--in-region)
+ (fboundp 'corfu--exit-function))
+ (setq restore-props cands))
+ (cons (apply-partially valid input) cands))))
:category backend
:sort (not (cape--company-call backend 'sorted))))
:exclusive 'no
@@ -809,13 +816,13 @@ changed. The function `cape-company-to-capf' is experimental."
:company-kind (lambda (x) (cape--company-call backend 'kind x))
:annotation-function (lambda (x)
(when-let (ann (cape--company-call backend 'annotation x))
- (if (string-match-p "^[ \t]" ann)
- ann
- (concat " " ann))))
- :exit-function
- (lambda (x _status)
- (cape--company-call backend 'post-completion
- (or (car (member x candidates)) x))))))))
+ (concat " " (string-trim ann))))
+ :exit-function (lambda (x _status)
+ ;; Restore the candidate string including
+ ;; properties if restore-props is non-nil. See
+ ;; the comment above.
+ (setq x (or (car (member x restore-props)) x))
+ (cape--company-call backend 'post-completion x)))))))
;;;###autoload
(defun cape-interactive (&rest capfs)