Subject: bug#62116: RFE: eglot: support window.showDocument LSP RPC

From: Felician Nemeth <[email-masked]>
Date: Sat, 13 May 2023 12:21:10 +0200

João Távora <[email-masked]> writes:

> I've now followed Alan's recipe and played around a bit with this.
> Starting from your version, I came up with this simpler patch.
>
> I was about to push it, but let's hear your opinions first

It is great, and I'd be happy if it went in as is.  Thank you.

However, ...

> +(cl-defmethod eglot-handle-request
> +  (_server (_method (eql window/showDocument)) &key
> +           uri external takeFocus selection)
> +  "Handle request window/showDocument."
> +  (if (eq external t) (browse-url uri)
> +    (let* (;; requests run with a let-bound `eglot--cached-server',
> +           ;; but when finding files from handlers, this fools
> +           ;; `eglot--maybe-activate-editing-mode'.
> +           (eglot--cached-server nil))

(I don't understand this part.)

> +      (with-current-buffer (find-file-noselect (eglot--uri-to-path uri))
> +        (cond (takeFocus
> +               (pop-to-buffer (current-buffer))

Somehow switch-to-buffer feels more natural to me.  But pop-to-buffer is
probably a better choice.

> +               (select-frame-set-input-focus (selected-frame)))
> +              ((display-buffer (current-buffer))))

Even when takeFous is nil, shouldn't we still at least raise the frame?
Otherwise Emacs might not show the document.

> +        (when selection
> +          (eglot--widening
> +           (pcase-let ((`(,beg . ,end) (eglot--range-region selection)))
> +             (goto-char beg)
> +             (pulse-momentary-highlight-region beg end 'highlight)))))))

This is a nice detail.

> +  '(:success t))

Regarding Alan's problem, one could argue that it can also be fixed on
the server-side.  However, there might be a general, but complex fix on
Emacs' side as well: imenu can be modified to asynchronously generate
its index when it just wants to show it in the menu (and not to users'
direct request).  Then the request of :textDocument/documentSymbol can
be non-blocking as well.  A simpler fix is to delay the opening of the
file.