unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#61371: 30.0.50; Adding support for jdt:// file scheme in eglot
@ 2023-02-08 19:09 Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-02-09  9:47 ` Michael Albinus
  0 siblings, 1 reply; 7+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-02-08 19:09 UTC (permalink / raw)
  To: 61371; +Cc: joaotavora


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





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

end of thread, other threads:[~2023-09-06  0:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-08 19:09 bug#61371: 30.0.50; Adding support for jdt:// file scheme in eglot Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-09  9:47 ` 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

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