unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 61371@debbugs.gnu.org
Cc: joaotavora@gmail.com
Subject: bug#61371: 30.0.50; Adding support for jdt:// file scheme in eglot
Date: Wed, 08 Feb 2023 20:09:40 +0100	[thread overview]
Message-ID: <87r0v06kjv.fsf@thornhill.no> (raw)


Hi!

When using Eglot along with Java there are some hacks that needs to be
made to make things functional.  One of the more important things is to
add support for the jdt:// file scheme that is sent when you try to
go-to-definition on a system or third party lib.

For completeness sake - consider:

```
import java.util.Li|st;

public Foo {
  void foo() {
    List foo;
  }
}
```

Cursor is now where | is, and if you try to M-., you won't get far.
This is because jdtls uses a convoluted way to retrieve this information
on demand, see [0].  Now the client has to query the language server an
additional time to get the actual content, load it somewhere, _then_
goto-def.  I've created one such hack for my own config, but maybe this
is something that could be mainlined, even though eglot itself is trying
hard to be language agnostic?  I'm not sure the code needs to live in
eglot, it could live some place other file-handlers live, if such a
place exists.  Anyway, the code I use to fix this now looks like this:

```
(defclass eglot-java (eglot-lsp-server) ()
  :documentation "A custom class for Java")

(cl-defmethod eglot-execute-command
  ((_server eglot-java) (_cmd (eql java.apply.workspaceEdit)) arguments)
  "Eclipse JDT breaks spec and replies with edits as arguments."
  (mapc #'eglot--apply-workspace-edit arguments))

(cl-defmethod eglot-initialization-options ((server eglot-java))
  "Passes through required java initialization options"
  `( :settings ,eglot-java-config
     :workspaceFolders [,(eglot--path-to-uri (project-root (project-current)))]
     :extendedClientCapabilities (:classFileContentsSupport t)))

(defun jdt-file-name-handler (operation &rest args)
  "Support Eclipse jdtls `jdt://' uri scheme."
  (let* ((uri (car args))
         (cache-dir "/tmp/.eglot")
         (source-file
          (expand-file-name
           (file-name-concat
            cache-dir
            (save-match-data
              (when (string-match "jdt://contents/\\(.*?\\)/\\(.*\\)\.class\\?" uri)
                (format "%s.java" (replace-regexp-in-string "/" "." (match-string 2 uri) t t))))))))
    (unless (file-readable-p source-file)
      (let ((content (jsonrpc-request (eglot-current-server) :java/classFileContents (list :uri uri)))
            (metadata-file (format "%s.%s.metadata"
                                   (file-name-directory source-file)
                                   (file-name-base source-file))))
        (unless (file-directory-p cache-dir) (make-directory cache-dir t))
        (with-temp-file source-file (insert content))
        (with-temp-file metadata-file (insert uri))))
    source-file))

(add-to-list
 'eglot-server-programs
 `(java-mode . (eglot-java . ("jdtls"))))

(add-to-list
 'eglot-server-programs
 `(java-ts-mode . (eglot-java . ("jdtls"))))
```

As you can see, we need to query the server with
:java/classFileContents, and also register with the server to send it
with the :extendedClientCapabilities on initialization.

Is there a place this code could live? Maybe in the new java-ts-mode?

Theo

[0]: https://github.com/eclipse/eclipse.jdt.ls/issues/2322#issuecomment-1313953316





             reply	other threads:[~2023-02-08 19:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-08 19:09 Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-02-09  9:47 ` bug#61371: 30.0.50; Adding support for jdt:// file scheme in eglot Michael Albinus
2023-02-09 10:42   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-10 20:32     ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-15 18:05       ` Danny Freeman via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-15 18:09         ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-06  0:05           ` Stefan Kangas

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=87r0v06kjv.fsf@thornhill.no \
    --to=bug-gnu-emacs@gnu.org \
    --cc=61371@debbugs.gnu.org \
    --cc=joaotavora@gmail.com \
    --cc=theo@thornhill.no \
    /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).