forked from emacs-lsp/dap-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dap-java.el
320 lines (285 loc) · 13.6 KB
/
dap-java.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
;;; dap-java.el --- Debug Adapter Protocol mode for Java -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Ivan Yonchovski
;; Author: Ivan Yonchovski <[email protected]>
;; Keywords: languages
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;; URL: https://github.com/yyoncho/dap-mode
;; Package-Requires: ((emacs "25.1") (dash "2.14.1") (lsp-mode "4.0") (lsp-java "0.1"))
;; Version: 0.2
;; DAP Adapter for java
;;; Commentary:
;;; Code:
(require 'lsp-mode)
(require 'lsp-java)
(require 'dap-mode)
(defvar dap-java--classpath-separator (if (string= system-type "windows-nt")
";"
":"))
(defvar dap-java--var-format (if (string= system-type "windows-nt")
"%%%s%%"
"$%s"))
(defcustom dap-java-java-command "java"
"Path of the java executable."
:group 'dap-java
:type 'string)
(defcustom dap-java-compile-port 33000
"The debug port which will be used for compile/attach configuration.
If the port is taken, DAP will try the next port."
:group 'dap-java
:type 'number)
(defcustom dap-java-test-runner
(expand-file-name (locate-user-emacs-file "eclipse.jdt.ls/test-runner/junit-platform-console-standalone.jar"))
"DAP Java test runner."
:group 'dap-java-java
:type 'file)
(defcustom dap-java-build 'ask
"Perform build before running project behaviour."
:group 'dap-java
:type '(choice (const ask)
(const always)
(const never)))
(defcustom dap-java-test-additional-args ()
"Additional arguments for JUnit standalone runner."
:group 'dap-java
:type '(list string))
(defcustom dap-java-default-debug-port 1044
"Default debug port."
:group 'dap-java
:type 'number)
(defun dap-java-test-class ()
"Get class FDQN."
(-if-let* ((symbols (lsp--get-document-symbols))
(package-name (-some->> symbols
(-first (-lambda ((&hash "kind")) (= kind 4)))
(gethash "name")))
(class-name (->> symbols
(--first (= (gethash "kind" it) 5))
(gethash "name"))))
(concat package-name "." class-name)
(user-error "No class found")))
(defun dap-java-test-method-at-point ()
"Get method at point."
(-let* ((symbols (lsp--get-document-symbols))
(package-name (-some->> symbols
(-first (-lambda ((&hash "kind")) (= kind 4)))
(gethash "name"))))
(or (->> symbols
(-keep (-lambda ((&hash "children" "kind" "name" class-name))
(and (= kind 5)
(seq-some
(-lambda ((&hash "kind" "range" "selectionRange" selection-range))
(-let (((beg . end) (lsp--range-to-region range)))
(and (= 6 kind ) (<= beg (point) end)
(concat package-name "." class-name "#"
(lsp-region-text selection-range)))))
children))))
(cl-first))
(user-error "No method at point"))))
(defun dap-java--select-main-class ()
"Select main class from the current workspace."
(let* ((main-classes (with-lsp-workspace (lsp-find-workspace 'jdtls)
(lsp-send-execute-command "vscode.java.resolveMainClass")))
(main-classes-count (length main-classes))
current-class)
(cond
((= main-classes-count 0) (error "Unable to find main class.
Please check whether the server is configured propertly"))
((= main-classes-count 1) (cl-first main-classes))
((setq current-class (--first (string= buffer-file-name (gethash "filePath" it))
main-classes))
current-class)
(t (dap--completing-read "Select main class to run: "
main-classes
(lambda (it)
(format "%s(%s)"
(gethash "mainClass" it)
(gethash "projectName" it)))
nil
t)))))
(defun dap-java--populate-launch-args (conf)
"Populate CONF with launch related configurations."
(when (not (and (plist-get conf :mainClass)
(plist-get conf :projectName)))
(-let [(&hash "mainClass" main-class "projectName" project-name) (dap-java--select-main-class)]
(setq conf (plist-put conf :mainClass main-class))
(plist-put conf :projectName project-name)))
(-let [(&plist :mainClass main-class :projectName project-name) conf]
(-> conf
(dap--put-if-absent :args "")
(dap--put-if-absent :cwd (lsp-java--get-root))
(dap--put-if-absent :stopOnEntry :json-false)
(dap--put-if-absent :host "localhost")
(dap--put-if-absent :console "internalConsole")
(dap--put-if-absent :request "launch")
(dap--put-if-absent :modulePaths (vector))
(dap--put-if-absent :classPaths
(or (cl-second
(with-lsp-workspace (lsp-find-workspace 'jdtls)
(lsp-send-execute-command
"vscode.java.resolveClasspath"
(vector main-class project-name))))
(error "Unable to resolve classpath")))
(dap--put-if-absent :name (format "%s (%s)"
(if (string-match ".*\\.\\([[:alnum:]_]*\\)$" main-class)
(match-string 1 main-class)
main-class)
project-name)))))
(defun dap-java--populate-attach-args (conf)
"Populate attach arguments.
CONF - the startup configuration."
(dap--put-if-absent conf :hostName (read-string "Enter host: " "localhost"))
(dap--put-if-absent conf :port (string-to-number (read-string "Enter port: "
(number-to-string dap-java-default-debug-port))))
(dap--put-if-absent conf :host "localhost")
(dap--put-if-absent conf :name (format "%s(%s)"
(plist-get conf :host)
(plist-get conf :port)))
conf)
(defun dap-java--populate-compile-attach-args (conf)
"Populate the CONF for running compile/attach.
Populate the arguments like normal 'Launch' request but then
initiate `compile' and attach to the process."
(dap-java--populate-launch-args conf)
(-let* (((&plist :mainClass :projectName :classPaths classpaths) conf)
(port (dap--find-available-port))
(program-to-start (format "%s -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=%s,quiet=y -cp %s %s"
dap-java-java-command
port
(format dap-java--var-format "CLASSPATH_ARGS")
mainClass)))
(dap-java--populate-attach-args
(list :type "java"
:request "attach"
:hostName "localhost"
:projectName projectName
:host "localhost"
:wait-for-port t
:program-to-start program-to-start
:port port
:environment-variables `(("CLASSPATH_ARGS" . ,(s-join dap-java--classpath-separator classpaths)))))))
(defun dap-java--populate-default-args (conf)
"Populate all of the fields that are not present in CONF."
(setq conf (plist-put conf :type "java"))
(setq conf (pcase (plist-get conf :request)
("launch" (dap-java--populate-launch-args conf))
("attach" (dap-java--populate-attach-args conf))
("compile_attach" (dap-java--populate-compile-attach-args conf))
(_ (dap-java--populate-launch-args conf))))
(plist-put conf :debugServer (with-lsp-workspace (lsp-find-workspace 'jdtls)
(lsp-send-execute-command "vscode.java.startDebugSession")))
(plist-put conf :__sessionId (number-to-string (float-time)))
conf)
(defun dap-java-debug (debug-args)
"Start debug session with DEBUG-ARGS."
(interactive (list (dap-java--populate-default-args nil)))
(dap-start-debugging debug-args))
(defun dap-java--run-unit-test-command (runner run-method?)
"Run debug test with the following arguments.
RUNNER is the test executor. RUN-METHOD? when t it will try to
run the surrounding method. Otherwise it will run the surronding
test."
(-let* ((to-run (if run-method?
(dap-java-test-method-at-point)
(dap-java-test-class)))
(test-class-name (cl-first (s-split "#" to-run)))
(class-path (->> (with-lsp-workspace (lsp-find-workspace 'jdtls)
(lsp-send-execute-command "vscode.java.resolveClasspath"
(vector test-class-name nil)))
cl-second
(s-join dap-java--classpath-separator))))
(list :program-to-start (s-join " "
(cl-list* runner "-jar" dap-java-test-runner
"-cp" (format dap-java--var-format "JUNIT_CLASS_PATH")
(if (and (s-contains? "#" to-run) run-method?) "-m" "-c")
(if run-method? to-run test-class-name)
dap-java-test-additional-args))
:environment-variables `(("JUNIT_CLASS_PATH" . ,class-path))
:name to-run
:cwd (lsp-java--get-root))))
(defun dap-java-run-test-method ()
"Run JUnit test.
If there is no method under cursor it will fallback to test class."
(interactive)
(-> (dap-java--run-unit-test-command dap-java-java-command t)
(plist-put :skip-debug-session t)
dap-start-debugging))
(defun dap-java-debug-test-method (port)
"Debug JUnit test.
If there is no method under cursor it will fallback to test class.
PORT is the port that is going to be used for starting and
attaching to the test."
(interactive (list (dap--find-available-port)))
(-> (list :type "java"
:request "attach"
:hostName "localhost"
:port port
:wait-for-port t)
(append (dap-java--run-unit-test-command
(format "%s -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=%s"
dap-java-java-command
port)
t))
dap-debug))
(defun dap-java-run-test-class ()
"Run JUnit test."
(interactive)
(-> (dap-java--run-unit-test-command dap-java-java-command nil)
(plist-put :skip-debug-session t)
dap-start-debugging))
(defun dap-java-debug-test-class (port)
"Debug JUnit test class.
PORT is the port that is going to be used for starting and
attaching to the test."
(interactive (list (dap--find-available-port)))
(dap-debug
(append (list :type "java"
:request "attach"
:hostName "localhost"
:port port
:wait-for-port t)
(dap-java--run-unit-test-command
(format "%s -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=%s"
dap-java-java-command
port)
nil))))
(dap-register-debug-provider "java" #'dap-java--populate-default-args)
(dap-register-debug-template "Java Run Configuration"
(list :type "java"
:request "launch"
:args ""
:cwd nil
:stopOnEntry :json-false
:host "localhost"
:request "launch"
:modulePaths (vector)
:classPaths nil
:projectName nil
:mainClass nil))
(dap-register-debug-template "Java Run Configuration (compile/attach)"
(list :type "java"
:request "compile_attach"
:args ""
:cwd nil
:host "localhost"
:request "launch"
:modulePaths (vector)
:classPaths nil
:name "Run"
:projectName nil
:mainClass nil))
(dap-register-debug-template "Java Attach"
(list :type "java"
:request "attach"
:hostName "localhost"
:port nil))
(provide 'dap-java)
;;; dap-java.el ends here