Skip to content

Commit 0bb28ac

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 11f7b17 commit 0bb28ac

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

lsp-inline-completion.el

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,53 @@ 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)))
87112

88113
(defface lsp-inline-completion-overlay-face
89114
'((t :inherit shadow))
@@ -383,6 +408,12 @@ The functions receive the inserted text and the range that was updated by the co
383408
(lsp--spinner-stop)))
384409
(t (lsp--error "Could not fetch completions: %s" err))))
385410

411+
(defun lsp-inline-completion--pre-command-hook ()
412+
"Cancels the inline completio before a non-keep command"
413+
(when (and (lsp-inline-completion--active-p)
414+
(not (lsp-inline-completion--keep this-command)))
415+
(lsp-inline-completion-cancel)))
416+
386417

387418
;; Inline Completion Mode
388419
;;;###autoload
@@ -413,13 +444,16 @@ lsp-inline-completion-mode is active."
413444
:lighter nil
414445
(cond
415446
((and lsp-inline-completion-mode lsp--buffer-workspaces)
447+
(add-hook 'pre-command-hook #'lsp-inline-completion--pre-command-hook nil t)
416448
(add-hook 'lsp-on-change-hook #'lsp-inline-completion--after-change nil t))
449+
417450
(t
418451
(when lsp-inline-completion--idle-timer
419452
(cancel-timer lsp-inline-completion--idle-timer))
420453

421454
(lsp-inline-completion-cancel)
422455

456+
(remove-hook 'pre-command-hook #'lsp-inline-completion--pre-command-hook t)
423457
(remove-hook 'lsp-on-change-hook #'lsp-inline-completion--after-change t))))
424458

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

0 commit comments

Comments
 (0)