summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mendler <mail@daniel-mendler.de>2022-02-17 11:24:04 +0100
committerDaniel Mendler <mail@daniel-mendler.de>2022-02-17 11:31:03 +0100
commit11c6d687996c0901a854f65b78e993b6639d79b9 (patch)
treeec4be3c45051ebaa96ef72593f627069d5d680e3
parentf3511af46862c5204424ccc47184accf7eb2034b (diff)
corfu--capf-wrapper: During auto complete, check the completion prefix early on
It turns out that for non-exclusive Capfs, the completion table was still called during the exclusivity check despite a larger prefix length `corfu-prefix`. Most of the sanity checking is also moved to the `corfu--capf-wrapper` now. See the discussion in #115. Thank you, @ronniedroid!
-rw-r--r--corfu.el22
1 files changed, 12 insertions, 10 deletions
diff --git a/corfu.el b/corfu.el
index b92813c..9048714 100644
--- a/corfu.el
+++ b/corfu.el
@@ -1147,12 +1147,7 @@ See `completion-in-region' for the arguments BEG, END, TABLE, PRED."
(when (and (not completion-in-region-mode) (equal tick (corfu--auto-tick)))
(pcase (while-no-input ;; Interruptible capf query
(run-hook-wrapped 'completion-at-point-functions #'corfu--capf-wrapper))
- ((and `(,fun ,beg ,end ,table . ,plist)
- (guard (integer-or-marker-p beg))
- (guard (<= beg (point) end))
- (guard
- (let ((len (or (plist-get plist :company-prefix-length) (- (point) beg))))
- (or (eq len t) (>= len corfu-auto-prefix)))))
+ (`(,fun ,beg ,end ,table . ,plist)
(let ((completion-in-region-mode-predicate
(lambda () (eq beg (car-safe (funcall fun)))))
(completion-extra-properties plist))
@@ -1201,13 +1196,20 @@ Auto completion is only performed if the tick did not change."
(remove-hook 'post-command-hook #'corfu--auto-post-command 'local)
(kill-local-variable 'completion-in-region-function))))
-(defun corfu--capf-wrapper (fun)
+(defun corfu--capf-wrapper (fun &optional prefix)
"Wrapper for `completion-at-point' FUN.
-Determines if the capf is applicable at the current position."
+The wrapper determines if the capf is applicable at the current position
+and performs sanity checking on the returned result. PREFIX is a prefix
+length override, set to t for manual completion."
(pcase (funcall fun)
((and res `(,beg ,end ,table . ,plist))
(and (integer-or-marker-p beg) ;; Valid capf result
- (<= beg (point) end) ;; Sanity checking
+ (<= beg (point) end) ;; Sanity checking
+ ;; When auto completing, check the prefix length!
+ (let ((len (or prefix
+ (plist-get plist :company-prefix-length)
+ (- (point) beg))))
+ (or (eq len t) (>= len corfu-auto-prefix)))
;; For non-exclusive capfs, check for valid completion.
(or (not (eq 'no (plist-get plist :exclusive)))
(let* ((str (buffer-substring-no-properties beg end))
@@ -1224,7 +1226,7 @@ Determines if the capf is applicable at the current position."
(defun corfu--capf-wrapper-advice (orig fun which)
"Around advice for `completion--capf-wrapper'.
The ORIG function takes the FUN and WHICH arguments."
- (if corfu-mode (corfu--capf-wrapper fun) (funcall orig fun which)))
+ (if corfu-mode (corfu--capf-wrapper fun t) (funcall orig fun which)))
;;;###autoload
(define-globalized-minor-mode corfu-global-mode corfu-mode corfu--on :group 'corfu)