Skip to content

Commit a7ad0e2

Browse files
committed
fix: cancel inline completion UI on non mapped commands
On some situations, keybindings such as M-x are not caught by the default handler (lsp-inline-completion-cancel-with-input, via overriding-terminal-local-map). This results in the tooltip not being hidden when it should. The consequence is that the user has to explicitly call lsp-inline-completion-cancel to hide the completion overlay and continue working in the buffer This change fixes that by placing a symbol property on the commands that should not cancel the UI (accept, next, prev, recenter-top-botton and ignore). On pre-command hook, we check for that property and cancel the completion if needed.
1 parent 1472333 commit a7ad0e2

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

lsp-inline-completion.el

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,60 @@ InlineCompletionItem objects"
6262

6363
;;;;;; Default UI -- overlay
6464

65+
(defun lsp-inline-completion--define-key (keymap key def &optional remove keep)
66+
"Defines key on map. With non-nil keep, will mark it as a command that
67+
should not cancel the inline completion UI"
68+
(define-key keymap key def remove)
69+
(when keep
70+
(pcase def
71+
;; Command is OK
72+
((pred symbolp)
73+
(put def 'lsp-inline-completion-keep t))
74+
;; ("str" . command) is OK
75+
(`(,_str . ,defn)
76+
(if (symbolp defn)
77+
(put defn 'lsp-inline-completion-keep t)
78+
(error "Unexpected type (str . defn) key definition %S" (type-of defn))))
79+
;; Keymaps and macros are unsupported
80+
(t (error "Unsupported definition with keep -- %S" def)))))
81+
6582
(defvar lsp-inline-completion-active-map
6683
(let ((map (make-sparse-keymap)))
6784
;; accept
68-
(define-key map (kbd "C-<return>") #'lsp-inline-completion-accept)
69-
(define-key map [mouse-1] #'lsp-inline-completion-accept-on-click)
85+
(lsp-inline-completion--define-key map (kbd "C-<return>") #'lsp-inline-completion-accept nil 'keep)
86+
(lsp-inline-completion--define-key map [mouse-1] #'lsp-inline-completion-accept-on-click nil 'keep)
7087
;; navigate
71-
(define-key map (kbd "C-n") #'lsp-inline-completion-next)
72-
(define-key map (kbd "C-p") #'lsp-inline-completion-prev)
88+
(lsp-inline-completion--define-key map (kbd "C-n") #'lsp-inline-completion-next nil 'keep)
89+
(lsp-inline-completion--define-key map (kbd "C-p") #'lsp-inline-completion-prev nil 'keep)
7390
;; cancel
74-
(define-key map (kbd "C-g") #'lsp-inline-completion-cancel)
75-
(define-key map (kbd "<escape>") #'lsp-inline-completion-cancel)
76-
(define-key map (kbd "C-c C-k") #'lsp-inline-completion-cancel)
91+
(lsp-inline-completion--define-key map (kbd "C-g") #'lsp-inline-completion-cancel)
92+
(lsp-inline-completion--define-key map (kbd "<escape>") #'lsp-inline-completion-cancel)
93+
(lsp-inline-completion--define-key map (kbd "C-c C-k") #'lsp-inline-completion-cancel)
7794
;; useful -- recenter without loosing the completion
78-
(define-key map (kbd "C-l") #'recenter-top-bottom)
95+
(lsp-inline-completion--define-key map (kbd "C-l") #'recenter-top-bottom nil 'keep)
7996
;; ignore
80-
(define-key map [down-mouse-1] #'ignore)
81-
(define-key map [up-mouse-1] #'ignore)
82-
(define-key map [mouse-movement] #'ignore)
97+
(lsp-inline-completion--define-key map [down-mouse-1] #'ignore nil 'keep)
98+
(lsp-inline-completion--define-key map [up-mouse-1] #'ignore nil 'keep)
99+
(lsp-inline-completion--define-key map [mouse-movement] #'ignore nil 'keep)
83100
;; Any event outside of the map, cancel and use it
84101
(define-key map [t] #'lsp-inline-completion-cancel-with-input)
85102
map)
86-
"Keymap active when showing inline code suggestions.")
103+
"Keymap active when showing inline code suggestions.
104+
105+
When adding new bindings to this map, prefer using
106+
lsp-inline-completion--define-key. Use a non-nil keep unless the command
107+
should cancel the completion UI")
108+
109+
(defsubst lsp-inline-completion--keep (cmd)
110+
"Returns t if the command should not cancel the current completion"
111+
(and (symbolp cmd) (get cmd 'lsp-inline-completion-keep)))
112+
113+
(defun lsp-inline-completion--pre-command-hook ()
114+
"Cancels the inline completio before a non-keep command"
115+
(when (and (lsp-inline-completion--active-p)
116+
(not (lsp-inline-completion--keep this-command)))
117+
(lsp-inline-completion-cancel)))
118+
87119

88120
(defface lsp-inline-completion-overlay-face
89121
'((t :inherit shadow))
@@ -413,13 +445,16 @@ lsp-inline-completion-mode is active."
413445
:lighter nil
414446
(cond
415447
((and lsp-inline-completion-mode lsp--buffer-workspaces)
448+
(add-hook 'pre-command-hook #'lsp-inline-completion--pre-command-hook nil t)
416449
(add-hook 'lsp-on-change-hook #'lsp-inline-completion--after-change nil t))
450+
417451
(t
418452
(when lsp-inline-completion--idle-timer
419453
(cancel-timer lsp-inline-completion--idle-timer))
420454

421455
(lsp-inline-completion-cancel)
422456

457+
(remove-hook 'pre-command-hook #'lsp-inline-completion--pre-command-hook t)
423458
(remove-hook 'lsp-on-change-hook #'lsp-inline-completion--after-change t))))
424459

425460
(defun lsp-inline-completion--maybe-display (original-buffer original-point)

0 commit comments

Comments
 (0)