unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#62115: RFE: eglot: support window.showDocument LSP RPC
@ 2023-03-10 15:34 Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-03-10 15:40 ` bug#62116: " Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 32+ messages in thread
From: Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-03-10 15:34 UTC (permalink / raw)
  To: 62115; +Cc: joaotavora

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

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: 3839 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..cb0bfdf603d 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,45 @@ 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
+                         (funcall
+                         #'(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

^ permalink raw reply related	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2023-05-25  1:03 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` bug#62116: " Alan Donovan via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-03-11 12:56   ` 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

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).