Skip to content

Commit 9b841c3

Browse files
committed
Honor ignored prefixes when clearing overlay
Prefix commands like C-u were clearing the Copilot overlay even when their command symbols (e.g., 'universal-argument) were listed in 'copilot-clear-overlay-ignore-commands'. * copilot.el (copilot--post-command): Check both 'this-command and the command bound to the current key sequence. * test/copilot-overlay-tests.el (copilot-post-command-honors-key-binding-ignore) (copilot-post-command-clears-when-not-ignored): New tests. * Eask: Add script entry to run the tests.
1 parent 8e43edf commit 9b841c3

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Eask

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
(package-file "copilot.el")
1111

12-
(script "test" "echo \"Error: no test specified\" && exit 1")
12+
(script "test" "emacs -Q --batch -L . -l ert -l test/copilot-overlay-tests.el -f ert-run-tests-batch-and-exit")
1313

1414
(source 'gnu)
1515
(source 'melpa)

copilot.el

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,9 @@ Copilot will show completions only if all predicates return t."
12231223
(or
12241224
(string-prefix-p "copilot-" (symbol-name this-command))
12251225
(member this-command copilot-clear-overlay-ignore-commands)
1226+
(let ((key-command (key-binding (this-command-keys-vector) t)))
1227+
(and (symbolp key-command)
1228+
(member key-command copilot-clear-overlay-ignore-commands)))
12261229
(copilot--self-insert this-command)))))
12271230
(copilot-clear-overlay)
12281231
(when copilot--post-command-timer

test/copilot-overlay-tests.el

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
;;; copilot-overlay-tests.el --- overlay command detection tests -*- lexical-binding: t; -*-
2+
3+
(require 'cl-lib)
4+
(require 'ert)
5+
6+
(unless (require 'f nil 'noerror)
7+
(defun f-join (&rest segments)
8+
(let ((path (or (car segments) "")))
9+
(dolist (segment (cdr segments) path)
10+
(setq path (expand-file-name segment path)))
11+
path))
12+
(provide 'f))
13+
14+
(unless (featurep 'editorconfig)
15+
(defvar editorconfig-indentation-alist nil)
16+
(provide 'editorconfig))
17+
18+
(unless (featurep 'track-changes)
19+
(provide 'track-changes))
20+
21+
(defconst copilot-test--root
22+
(file-name-directory (directory-file-name (file-name-directory load-file-name)))
23+
"Root directory of copilot.el tests.")
24+
25+
(load-file (expand-file-name "copilot.el" copilot-test--root))
26+
(require 'copilot)
27+
28+
(ert-deftest copilot-post-command-honors-key-binding-ignore ()
29+
"copilot--post-command keeps the overlay when the key binding is ignored."
30+
(with-temp-buffer
31+
(let* ((copilot-clear-overlay-ignore-commands '(universal-argument-more))
32+
(copilot-idle-delay nil)
33+
(copilot--overlay (make-overlay (point) (point)))
34+
(copilot--post-command-timer nil)
35+
(this-command 'self-insert-command)
36+
(real-this-command 'self-insert-command)
37+
(cleared nil))
38+
(cl-letf (((symbol-function 'this-command-keys-vector) (lambda () [?]))
39+
((symbol-function 'key-binding) (lambda (&rest _) 'universal-argument-more))
40+
((symbol-function 'copilot-clear-overlay)
41+
(lambda (&optional _) (setq cleared t)))
42+
((symbol-function 'copilot--self-insert) (lambda (&rest _) nil)))
43+
(copilot--post-command)
44+
(should-not cleared)))))
45+
46+
(ert-deftest copilot-post-command-clears-when-not-ignored ()
47+
"copilot--post-command clears overlay for non-ignored prefixes."
48+
(with-temp-buffer
49+
(let* ((copilot-clear-overlay-ignore-commands nil)
50+
(copilot-idle-delay nil)
51+
(copilot--overlay (make-overlay (point) (point)))
52+
(copilot--post-command-timer nil)
53+
(this-command 'self-insert-command)
54+
(real-this-command 'self-insert-command)
55+
(cleared nil))
56+
(cl-letf (((symbol-function 'this-command-keys-vector) (lambda () [?]))
57+
((symbol-function 'key-binding) (lambda (&rest _) 'universal-argument-more))
58+
((symbol-function 'copilot-clear-overlay)
59+
(lambda (&optional _) (setq cleared t)))
60+
((symbol-function 'copilot--self-insert) (lambda (&rest _) nil)))
61+
(copilot--post-command)
62+
(should cleared)))))
63+
64+
(provide 'copilot-overlay-tests)
65+
66+
;;; copilot-overlay-tests.el ends here

0 commit comments

Comments
 (0)