Java 代码跳转

我写的项目可能比较low,基本上都是接口对应的实现类方法一一对应的,所以我希望能够直接跳转到实现类,并关闭 xref buffer,写了下面的代码。

(defun lucius/xref-show-xrefs (fetcher display-action)
  "Display some Xref values produced by FETCHER using DISPLAY-ACTION.
After jumping to the first xref, close the xref window."
  (let ((buf (xref--show-xref-buffer fetcher
                                     `((window . ,(selected-window))
                                       (display-action . ,display-action)
                                       (auto-jump . t)))))
    (when xref-auto-jump-to-first-xref
      (let ((window (get-buffer-window buf)))
        (when window
          (quit-window nil window))))))

(setq xref-auto-jump-to-first-xref t)
(setq xref-show-xrefs-function #'lucius/xref-show-xrefs)

有试过lsp-bridge吗?我没写java,但我用lsp-bridge, lsp-bridge支持java语法。

尝试过,还是更喜欢 eglot。

然后简单的加了个 跳转 mapper.xml 的函数。

(defun lucius/java-to-xml-mapper ()
  "Jump from a Java mapper file to the corresponding XML mapper file.
If the cursor is on a method name in the Java file, jump to the corresponding
method definition in the XML file."
  (let* ((java-file (buffer-file-name))
         (xml-file (concat (file-name-sans-extension java-file) ".xml"))
         (method-name (thing-at-point 'symbol t)))
    (if (file-exists-p xml-file)
          (find-file xml-file)
          (goto-char (point-min))
          (if (re-search-forward (concat "id=\"\\(" method-name "\\)\"") nil t)
              (message "Jumped to method: %s" method-name)
            (message "Method '%s' not found in XML file." method-name)))
      (message "No corresponding XML file found."))))