From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#61371: 30.0.50; Adding support for jdt:// file scheme in eglot Date: Wed, 08 Feb 2023 20:09:40 +0100 Message-ID: <87r0v06kjv.fsf@thornhill.no> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15169"; mail-complaints-to="usenet@ciao.gmane.io" Cc: joaotavora@gmail.com To: 61371@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Feb 08 20:11:24 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPpqk-0003iJ-GI for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 08 Feb 2023 20:11:23 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pPpqZ-0007iJ-0K; Wed, 08 Feb 2023 14:11:12 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pPpqQ-0007fJ-Ow for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:11:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pPpqQ-0003Ma-Eo for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:11:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pPpqQ-0000vT-1y; Wed, 08 Feb 2023 14:11:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: joaotavora@gmail.com, bug-gnu-emacs@gnu.org Resent-Date: Wed, 08 Feb 2023 19:11:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 61371 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: joaotavora@gmail.com Original-Received: via spool by submit@debbugs.gnu.org id=B.16758834053487 (code B ref -1); Wed, 08 Feb 2023 19:11:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 8 Feb 2023 19:10:05 +0000 Original-Received: from localhost ([127.0.0.1]:56693 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pPppV-0000uB-3A for submit@debbugs.gnu.org; Wed, 08 Feb 2023 14:10:05 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:49598) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pPppS-0000tw-HD for submit@debbugs.gnu.org; Wed, 08 Feb 2023 14:10:04 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pPppS-0007UW-8V for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:10:02 -0500 Original-Received: from out-121.mta1.migadu.com ([95.215.58.121]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pPppP-0002nI-JB for bug-gnu-emacs@gnu.org; Wed, 08 Feb 2023 14:10:01 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1675883383; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=nLsXmOtUk+8FAFHKEWvP+iDILzw/rDytyJzK3f8aaWE=; b=1x+WO136Xl7e3zltcUYQ+XxgPnU9+h/JoK9ceKluDZosmTcOA6S5389Sz12WbEI1tk+OOI y4jYwkLa5WCKRyDReFKiZeC/bE3nIQ5ZUAr/nCFqdY4eiw17sGI/lrULCd5fmXiBP0TqGu SYdhxxC/RzXyDJCE7K/toduM42vtM16y76xlp1ai9hwL/VVE0mn4PxFxRw4rVxk8Z+x7uy Oj+udhFWdR0KWDmG6p9W2bDFh4xiMYTzFrQ6+07Jr5LJVx6zSjI9UEuXoepN0eU6bDwo4q jxqg/Twg4LTyXpSjJWwfF8OEneeXx7CHhKyQKJZXtMVYL1CqWCWUK0wJGcWFDg== X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=95.215.58.121; envelope-from=theo@thornhill.no; helo=out-121.mta1.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:255160 Archived-At: 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