diff --git a/CHANGELOG.org b/CHANGELOG.org index 5dc6fd7aadb..acf7e1ca1cf 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -34,6 +34,7 @@ * Support ~labelDetails~ of Lsp Specification 3.17 * Add Gleam support * Drop deprecated rust-analyzer variable lsp-rust-analyzer-import-merge-behaviour + * Added run and debug code lenses to ~lsp-kotlin~ ** Release 8.0.0 * Add ~lsp-clients-angular-node-get-prefix-command~ to get the Angular server from another location which is still has ~/lib/node_modules~ in it. * Set ~lsp-clients-angular-language-server-command~ after the first connection to speed up subsequent connections. diff --git a/clients/lsp-kotlin.el b/clients/lsp-kotlin.el index 1b71a9948f7..ecf9e3f1720 100644 --- a/clients/lsp-kotlin.el +++ b/clients/lsp-kotlin.el @@ -25,6 +25,7 @@ ;;; Code: (require 'lsp-mode) +(require 'cl-lib) (defgroup lsp-kotlin nil "LSP support for Kotlin, using KotlinLanguageServer." @@ -122,6 +123,64 @@ to Kotlin." "kotlin-language-server")) "The path to store the language server at if necessary.") + +;; Debug and running +(declare-function dap-debug "ext:dap-mode" (template) t) + +(defun lsp-kotlin-run-main (main-class project-root debug?) + (require 'dap-kotlin) + (dap-debug (list :type "kotlin" + :request "launch" + :mainClass main-class + :projectRoot project-root + :noDebug (not debug?)))) + +(defun lsp-kotlin-lens-backend (_modified? callback) + (when lsp-kotlin-debug-adapter-enabled + (lsp-request-async + "kotlin/mainClass" + (list :uri (lsp--buffer-uri)) + (lambda (mainInfo) + (let ((main-class (lsp-get mainInfo :mainClass)) + (project-root (lsp-get mainInfo :projectRoot)) + (range (lsp-get mainInfo :range))) + (funcall callback + (list (lsp-make-code-lens :range range + :command + (lsp-make-command + :title "Run" + :command (lambda () + (interactive) + (lsp-kotlin-run-main main-class project-root nil)))) + (lsp-make-code-lens :range range + :command + (lsp-make-command + :title "Debug" + :command (lambda () + (interactive) + (lsp-kotlin-run-main main-class project-root t))))) + lsp--cur-version))) + :mode 'tick))) + +(defvar lsp-lens-backends) +(declare-function lsp-lens-refresh "lsp-lens" (buffer-modified? &optional buffer)) + +(define-minor-mode lsp-kotlin-lens-mode + "Toggle run/debug overlays." + :group 'lsp-kotlin + :global nil + :init-value nil + :lighter nil + (cond + (lsp-kotlin-lens-mode + (require 'lsp-lens) + ;; set lens backends so they are available is lsp-lens-mode is activated + ;; backend does not support lenses, and block our other ones from showing. When backend support lenses again, we can use cl-pushnew to add it to lsp-lens-backends instead of overwriting + (setq-local lsp-lens-backends (list #'lsp-kotlin-lens-backend)) + (lsp-lens-refresh t)) + (t (setq-local lsp-lens-backends (delete #'lsp-kotlin-lens-backend lsp-lens-backends))))) + + (lsp-dependency 'kotlin-language-server `(:system ,lsp-clients-kotlin-server-executable)