Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use a transient map to avoid overlay keymaps issues #4676

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 41 additions & 38 deletions lsp-inline-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ InlineCompletionItem objects"
;; useful -- recenter without loosing the completion
(define-key map (kbd "C-l") #'recenter-top-bottom)
;; ignore
(define-key map [down-mouse-1] #'ignore)
(define-key map [down-mouse-1] #'ignore)
(define-key map [up-mouse-1] #'ignore)
(define-key map [mouse-movement] #'ignore)
;; Any event outside of the map, cancel and use it
Expand Down Expand Up @@ -109,10 +109,11 @@ InlineCompletionItem objects"

(defcustom lsp-inline-completion-accepted-functions nil
"Functions executed after accepting a code suggestion.
The functions receive the text range that was updated by the completion."
The functions receive the inserted text and the range that was updated by the completion."
:type 'hook
:group 'lsp-mode)


(defcustom lsp-inline-completion-cancelled-hook nil
"Hooks executed after cancelling the completion UI."
:type 'hook
Expand All @@ -138,28 +139,16 @@ The functions receive the text range that was updated by the completion."
(integer :tag "Secondary")))
:group 'lsp-mode)

(defsubst lsp-inline-completion--overlay-visible ()
"Return whether the `overlay' is avaiable."
(and (overlayp lsp-inline-completion--overlay)
(overlay-buffer lsp-inline-completion--overlay)))
(defsubst lsp-inline-completion--active-p ()
"Returns whether we are in an active completion"
(overlayp lsp-inline-completion--overlay))

(defun lsp-inline-completion--clear-overlay ()
"Hide the suggestion overlay."
(when (lsp-inline-completion--overlay-visible)
(delete-overlay lsp-inline-completion--overlay))
(setq lsp-inline-completion--overlay nil))


(defun lsp-inline-completion--get-overlay (beg end)
"Build the suggestions overlay."
(when (overlayp lsp-inline-completion--overlay)
(lsp-inline-completion--clear-overlay))

(setq lsp-inline-completion--overlay (make-overlay beg end nil nil t))
(overlay-put lsp-inline-completion--overlay 'keymap lsp-inline-completion-active-map)
(overlay-put lsp-inline-completion--overlay 'priority lsp-inline-completion-overlay-priority)

lsp-inline-completion--overlay)
(delete-overlay lsp-inline-completion--overlay))
(setq lsp-inline-completion--overlay nil)
(internal-pop-keymap lsp-inline-completion-active-map 'overriding-terminal-local-map))


(defun lsp-inline-completion--show-keys ()
Expand Down Expand Up @@ -189,6 +178,19 @@ The functions receive the text range that was updated by the completion."
keys)
"/"))))))))


(defun lsp-inline-completion--get-overlay (beg end)
"Build the suggestions overlay."
(lsp-inline-completion--clear-overlay)

(setq lsp-inline-completion--overlay (make-overlay beg end nil nil t))
(overlay-put lsp-inline-completion--overlay 'priority lsp-inline-completion-overlay-priority)
(internal-push-keymap lsp-inline-completion-active-map 'overriding-terminal-local-map)
(lsp-inline-completion--show-keys)

lsp-inline-completion--overlay)


(defun lsp-inline-completion-show-overlay ()
"Makes the suggestion overlay visible."
(unless (and lsp-inline-completion--items
Expand Down Expand Up @@ -239,7 +241,6 @@ The functions receive the text range that was updated by the completion."

(goto-char target-position)

(lsp-inline-completion--show-keys)
(run-hooks 'lsp-inline-completion-shown-hook)))

(defun lsp-inline-completion--insert-sugestion (text kind start end command?)
Expand Down Expand Up @@ -277,10 +278,11 @@ The functions receive the text range that was updated by the completion."
(defun lsp-inline-completion-accept ()
"Accepts the current suggestion."
(interactive)
(unless (lsp-inline-completion--overlay-visible)
(error "Not showing suggestions"))
(unless (lsp-inline-completion--active-p)
(error "Not showing suggestions"))

(lsp-inline-completion--clear-overlay)

(-let* ((suggestion (elt lsp-inline-completion--items lsp-inline-completion--current))
((&InlineCompletionItem? :insert-text :range? :command?) suggestion)
((kind . text) (cond
Expand Down Expand Up @@ -310,32 +312,29 @@ The functions receive the text range that was updated by the completion."
(defun lsp-inline-completion-cancel ()
"Close the suggestion overlay."
(interactive)
(when (lsp-inline-completion--overlay-visible)

(message nil) ;; clear echo
(let ((was-active (lsp-inline-completion--active-p)))
(lsp-inline-completion--clear-overlay)

(when lsp-inline-completion--start-point
(goto-char lsp-inline-completion--start-point))
(when was-active
(goto-char lsp-inline-completion--start-point)
(run-hooks 'lsp-inline-completion-cancelled-hook))))

(run-hooks 'lsp-inline-completion-cancelled-hook)))

(defun lsp-inline-completion-cancel-with-input (event &optional arg)
(defun lsp-inline-completion-cancel-with-input (event)
"Cancel the inline completion and executes whatever event was received."
(interactive (list last-input-event current-prefix-arg))
(interactive (list last-input-event))

(lsp-inline-completion-cancel)

(let ((command (lookup-key (current-active-maps) (vector event)))
(current-prefix-arg arg))

(when (commandp command)
(call-interactively command))))
(setq unread-command-events (nconc unread-command-events (list event))))

(defun lsp-inline-completion-next ()
"Display the next inline completion."
(interactive)
(unless (lsp-inline-completion--overlay-visible)
(unless (lsp-inline-completion--active-p)
(error "Not showing suggestions"))

(setq lsp-inline-completion--current
(mod (1+ lsp-inline-completion--current)
(length lsp-inline-completion--items)))
Expand All @@ -345,8 +344,10 @@ The functions receive the text range that was updated by the completion."
(defun lsp-inline-completion-prev ()
"Display the previous inline completion."
(interactive)
(unless (lsp-inline-completion--overlay-visible)

(unless (lsp-inline-completion--active-p)
(error "Not showing suggestions"))

(setq lsp-inline-completion--current
(mod (1- lsp-inline-completion--current)
(length lsp-inline-completion--items)))
Expand All @@ -359,7 +360,9 @@ The functions receive the text range that was updated by the completion."
(interactive)

(unless implicit
(lsp--spinner-start) )
(lsp--spinner-start))

(run-hooks 'lsp-before-inline-completion-hook)

(condition-case err
(unwind-protect
Expand Down