(defvar-local copilot--cached-completion-returns '())
(defun copilot--completion-doc-buf (completions key)
(let ((completion (alist-get key completions)))
(when completion
(with-current-buffer (get-buffer-create "*copilot-doc*")
(erase-buffer)
(insert (plist-get completion :text))
(current-buffer)))))
(defun copilot-completion-at-point()
"send completion request to copilot and return cached completions"
;; send request each time triggered
(copilot--get-completion
(jsonrpc-lambda (&key completions &allow-other-keys)
(when (not (seq-empty-p completions))
(add-to-list 'copilot--cached-completion-returns completions t)
(when (length> copilot--cached-completion-returns 3)
(setq copilot--cached-completion-returns
(cdr copilot--cached-completion-returns))))))
(let* ((bounds (bounds-of-thing-at-point 'symbol))
(start (or (car bounds) (point)))
(end (or (cdr bounds) (point)))
(completions '()))
;; preprocess cached completion data keep completion entry unique
(cl-loop for completion-return in copilot--cached-completion-returns
do
(seq-doseq (completion completion-return)
(let ((key (plist-get completion :text)))
(setf (alist-get key completions) completion))))
;; discard prefix from completion probably
(let* ((discardpos (- start (line-beginning-position)))
(prefix-suber
(lambda (foo)
(let* ((key (car foo))
(subpose (min discardpos (length key))))
(setf (car foo) (substring key subpose))))))
(mapc prefix-suber completions))
;; build completion data
(list
start
end
completions
:exclusive 'no
:company-kind (lambda (_) 'copilot)
:company-doc-buffer (apply-partially
#'copilot--completion-doc-buf
completions))))
用一个buf缓存最近3次的copilot返回, 异步的,目前自己测了是不会卡手的。
感觉copilot最近降智了,返回默认只给一条。