all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Felician Nemeth <felician.nemeth@gmail.com>
To: "João Távora" <joaotavora@gmail.com>
Cc: 62116@debbugs.gnu.org, adonovan@google.com,
	sebastian.poeplau@mailbox.org
Subject: bug#62116: RFE: eglot: support window.showDocument LSP RPC
Date: Sat, 06 May 2023 14:46:44 +0200	[thread overview]
Message-ID: <87ttwpbpaj.fsf@betli.tmit.bme.hu> (raw)
In-Reply-To: <87ttwqsow9.fsf@gmail.com> ("João Távora"'s message of "Fri, 05 May 2023 17:51:02 +0100")

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

> Felicián, can you show your full patch again, perhaps with a unit test
> using the ada language server?

I've attached the patch.

However, I failed to write a unit test.  At the end of
eglot-handle-request buffer-name shows the expected main.adb, but at the
end of the my ert-deftest it is the old, incorrect value.  This puzzles
my, because if I interactively repeat the test steps, everything is OK.
I copy the test code at the end of the email.

> Also, can you comment on why you think Alan Donovan's patch has that
> non-synchronous find-file mechanism?  Since Alan, doesn't reply, what do
> you conjecture is the reason his patch goes through this trouble?
>
> I'd also like someone to address my comments of that patch, which is
> similar to yours:
> https://debbugs.gnu.org/cgi/bugreport.cgi?bug=62116#10.  Among others
>
> 1. if you think the Emacs frame should be raised and/or focused when a
>    window.showDocument comes in.

I always use Emacs in single frame, so I have no experience in this
area, but I don't understand why x-focus-frame is necessary.  The
showDocument request should not come out of the blue, and if the user is
actively working on a source file, the frame is already in focus.

> 2. if browse-url should be used for non-file: urls (it might open a
>    browser window outside of Emacs, which IMO is fine.)

My original patch used browse-url as well.  browse-url is highly
customizable, and I think it is right thing to use here.  Users can
customize it to relay the URL to eww, firefox, etc even for specific
URLs.

If I understand it correctly, you argued that if takeFocus is false,
Eglot should still show the requested file, but the point should remain
were it was.  I think you're right, but I haven't updated my patch yet.

Alan's patch also contains a recenter call, which I think is unnecessary
and would probably annoy me, when the target point was already visible
before the showDocument request.

On the other hand, if the showDocument request contains a selection,
both patches could be enhanced to better visualize this selection.

Because of the timers, Alan's eglot-handle-request returns success even
if the inner lambda fails.  It should probably check if filename exists
before calling run-with-idle-timer.

However, I don't like approach of using timers.  I think a better
alterative is to put a modified lambda function in an event queue, and
Eglot should process the queue after it sent its reply to the client
request.

Finally, I think Alan's copyright status is still unclear.

And this is the my failed attempt at writing a test:

(ert-deftest eglot-test-window/showDocument ()
  "Test handling a window/showDocument server request."
  (skip-unless (executable-find "ada_language_server"))
  (let (server)
    (eglot--with-fixture
     '(("project" .
        (("project.gpr" .
          "Project P is\n   for Main use (\"main.adb\");\nend P;")
         ("main.ads" . "procedure Main;")
         ("main.adb" . "procedure Main is null;"))))
     (with-current-buffer
         (eglot--find-file-noselect "project/main.ads")
       (eglot--sniffing (:server-requests s-requests
                         :server-replies s-replies
                         :client-requests c-requests
                         :client-replies c-replies)
         (should (setq server (eglot--tests-connect)))
         (eglot-execute-command server "als-other-file"
                                (vector (eglot--TextDocumentIdentifier)))
         (let (cmd-id show-doc-id)
           (eglot--wait-for (c-requests 2)
               (&key id method &allow-other-keys)
               (setq cmd-id id)
               (string= method "workspace/executeCommand"))
           (eglot--wait-for (s-requests 2)
               (&key id method &allow-other-keys)
             (setq show-doc-id id)
             (string= method "window/showDocument"))
           (eglot--wait-for (c-replies 1)
               (&key id error &allow-other-keys)
               (and (eq id show-doc-id) (null error)))
           (eglot--wait-for (s-replies 1)
               (&key id &allow-other-keys)
               (eq id cmd-id))))
         (should (string-equal (buffer-name) "main.adb"))))))


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Eglot-support-window-showDocument-request.patch --]
[-- Type: text/x-diff, Size: 2207 bytes --]

From 468a8104183ba46f92d0a194862761c81943c505 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felici=C3=A1n=20N=C3=A9meth?= <felician.nemeth@gmail.com>
Date: Sat, 22 Apr 2023 10:49:17 +0200
Subject: [PATCH] Eglot: support window/showDocument request

* eglot.el (eglot-client-capabilities): Add showDocument support.
(eglot-handle-request window/showDocument): New cl-defmethod.
---
 lisp/progmodes/eglot.el | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 3134d55fc0..daabb4ff7b 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -830,7 +830,8 @@ eglot-client-capabilities
                                        `(:valueSet
                                          [,@(mapcar
                                              #'car eglot--tag-faces)])))
-            :window `(:workDoneProgress t)
+            :window `(:showDocument (:support t)
+                      :workDoneProgress t)
             :general (list :positionEncodings ["utf-32" "utf-8" "utf-16"])
             :experimental eglot--{})))
 
@@ -2345,6 +2346,28 @@ eglot-handle-request
   "Handle server request workspace/workspaceFolders."
   (eglot-workspace-folders server))
 
+(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 ((filename (eglot--uri-to-path uri)))
+      (if (eq takeFocus t)
+          (find-file filename)
+        (find-file-noselect filename))
+      (when selection
+        (with-current-buffer (get-file-buffer filename)
+          (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))))))))
+  '(:success t))
+
 (defun eglot--TextDocumentIdentifier ()
   "Compute TextDocumentIdentifier object for current buffer."
   `(:uri ,(eglot--path-to-uri (or buffer-file-name
-- 
2.30.2


  parent reply	other threads:[~2023-05-06 12:46 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 ` 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 [this message]
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

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

  git send-email \
    --in-reply-to=87ttwpbpaj.fsf@betli.tmit.bme.hu \
    --to=felician.nemeth@gmail.com \
    --cc=62116@debbugs.gnu.org \
    --cc=adonovan@google.com \
    --cc=joaotavora@gmail.com \
    --cc=sebastian.poeplau@mailbox.org \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.