unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Alan Donovan via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 62116@debbugs.gnu.org
Cc: joaotavora@gmail.com
Subject: bug#62116: RFE: eglot: support window.showDocument LSP RPC
Date: Fri, 10 Mar 2023 10:40:01 -0500	[thread overview]
Message-ID: <CAPVWWDUKRzSGfSRjJnMZBfhEZF3S29P8qGyQRUMABbpF9z40MA@mail.gmail.com> (raw)
In-Reply-To: <CAPVWWDXQiFwuFi3G0jOOFSwj6YJ-Rmd8n0cZSOphiKK+RuQnZA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 923 bytes --]

Apologies, that patch contained debugging stuff. Please use this one:



On Fri, 10 Mar 2023 at 10:34, Alan Donovan <adonovan@google.com> wrote:
>
> Attn: Joao Tavora
>
> The attached patch adds basic support to eglot for the
> window.showDocument downcall, added in LSP 3.16, which enables the
> server to request that
> the client open an URL either in an external browser (e.g. as if by
> the open(1) or xdg-open(1) command) or internally, in the editor.
> https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showDocument
>
> See also https://github.com/joaotavora/eglot/discussions/1180.
>
> Thanks to @nemethf, whose unmerged PR
> https://github.com/joaotavora/eglot/pull/855 provided numerous
> improvements over my own first draft.
>
> This patch can be applied to the base commit of 8ee205d. Please let me
> know if you'd like it in some other form.
>
> cheers
> alan

[-- Attachment #2: eglot-showDocument.patch --]
[-- Type: application/octet-stream, Size: 3802 bytes --]


This patch adds basic support to eglot for the window.showDocument
downcall, added in LSP 3.16, which enables the server to request that
the client open an URL either in an external browser (e.g. as if by
the open(1) or xdg-open(1) command) or internally, in the editor.

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showDocument

See also https://github.com/joaotavora/eglot/discussions/1180.

Thanks to @nemethf, whose unmerged PR
https://github.com/joaotavora/eglot/pull/855 provided numerous
improvements over my own first draft.

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 2f8d2002cd3..fb0c5cb1199 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -819,6 +819,7 @@ treated as in `eglot--dbind'."
                                          [,@(mapcar
                                              #'car eglot--tag-faces)])))
             :general (list :positionEncodings ["utf-32" "utf-8" "utf-16"])
+            :window '(:showDocument (:support t))
             :experimental eglot--{})))
 
 (cl-defgeneric eglot-workspace-folders (server)
@@ -2143,6 +2144,44 @@ COMMAND is a symbol naming the command."
   (_server (_method (eql window/logMessage)) &key _type _message)
   "Handle notification window/logMessage.") ;; noop, use events buffer
 
+(cl-defmethod eglot-handle-request
+  (_server (_method (eql window/showDocument)) &key uri external takeFocus selection
+           &allow-other-keys)
+  "Handle a window/showDocument server->client request by opening the
+URL in a browser or within Emacs."
+  ;; Note: browse-url on a "file:" URL will execute open(1) or xdg-open(1),
+  ;; which may end up opening the file in Emacs (or some other editor that
+  ;; has registered the *.go extension), ignoring the optional selection.
+  ;; Typically servers send "External: false" for files.
+  (if (and external (not (eq external :json-false)))
+      (browse-url uri)
+    ;; Don't call find-file immediately (within the RPC handler) since
+    ;; find-file's go-mode hooks issue more LSP RPCs (e.g.
+    ;; textDocument/documentSymbol) from within this one, which then
+    ;; gets stuck. (Is that a bug in gopls?)
+    ;; So, make the call asynchronously from the idle loop.
+    ;; Of course this means we can't respond with the proper success value.
+    (run-with-idle-timer 0 nil
+                         #'(lambda (filename noselect selection)
+                                   (with-current-buffer (if noselect
+                                                            (find-file-noselect filename)
+                                                          (x-focus-frame nil)
+                                                          (find-file filename))
+                                     (when selection
+                                       (save-restriction
+                                         (widen)
+                                         (pcase-let ((`(,beg . ,end) (eglot--range-region selection)))
+                                           (if (equal beg end)
+                                               (goto-char beg)
+                                             (goto-char end)
+                                             (set-mark-command nil)
+                                             (goto-char beg))
+                                           (recenter))))))
+                         (eglot--uri-to-path uri) ; filename
+                         (or (null takeFocus) (eq takeFocus :json-false)) ; noselect
+                         selection))
+  '(:success t))
+
 (cl-defmethod eglot-handle-notification
   (_server (_method (eql telemetry/event)) &rest _any)
   "Handle notification telemetry/event.") ;; noop, use events buffer

  reply	other threads:[~2023-03-10 15:40 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-10 15:34 bug#62115: RFE: eglot: support window.showDocument LSP RPC Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-03-10 15:40 ` Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-03-11 12:56   ` bug#62116: " João Távora
2023-03-11 13:17     ` Eli Zaretskii
2023-03-11 20:20       ` João Távora
2023-03-12  6:26         ` Eli Zaretskii
2023-04-22  9:08           ` Felician Nemeth
2023-05-05  6:03             ` Eli Zaretskii
2023-05-05  7:35               ` João Távora
2023-05-05 16:51               ` João Távora
2023-05-05 17:06                 ` Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-05 17:19                   ` João Távora
2023-05-05 17:35                     ` Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-05 17:36                       ` Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-11 23:12                       ` João Távora
2023-05-06 12:46                 ` Felician Nemeth
2023-05-08 13:23                   ` Felician Nemeth
2023-05-08 16:36                     ` João Távora
2023-05-09 17:03                       ` Felician Nemeth
2023-05-09 17:13                         ` Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-11 22:41                           ` João Távora
2023-05-12  0:54                         ` João Távora
2023-05-12 20:46                           ` Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-15  8:48                             ` João Távora
2023-05-13 10:21                           ` Felician Nemeth
2023-05-13 11:57                             ` João Távora
2023-05-14 19:02                               ` Felician Nemeth
2023-05-14 19:19                                 ` João Távora
2023-05-15 10:45                                   ` João Távora
2023-05-16 18:34                                     ` João Távora
2023-05-24 22:13                                       ` Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-25  1:03                                       ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAPVWWDUKRzSGfSRjJnMZBfhEZF3S29P8qGyQRUMABbpF9z40MA@mail.gmail.com \
    --to=bug-gnu-emacs@gnu.org \
    --cc=62116@debbugs.gnu.org \
    --cc=adonovan@google.com \
    --cc=joaotavora@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).