|
18 | 18 | ;; You should have received a copy of the GNU General Public License
|
19 | 19 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
20 | 20 |
|
21 |
| -;; URL: https://github.com/emacs-lsp/dap-mode |
22 |
| -;; Package-Requires: ((emacs "25.1") (dash "2.14.1") (lsp-mode "4.0") (f "0.20.0")) |
23 |
| -;; Version: 0.1 |
24 |
| - |
25 | 21 | ;;; Commentary:
|
26 | 22 | ;; Adapter for https://github.com/go-delve/delve
|
27 | 23 |
|
|
36 | 32 |
|
37 | 33 | (defcustom dap-dlv-go-delve-path
|
38 | 34 | (or (executable-find "dlv")
|
39 |
| - (expand-file-name |
40 |
| - "dlv" (expand-file-name "bin" (or (getenv "GOPATH") |
41 |
| - (f-join (getenv "HOME") "go"))))) |
| 35 | + (expand-file-name |
| 36 | + "dlv" (expand-file-name "bin" (or (getenv "GOPATH") |
| 37 | + (f-join (getenv "HOME") "go"))))) |
42 | 38 | "The path to the delve command."
|
43 | 39 | :group 'dap-dlv-go
|
44 | 40 | :type 'string)
|
|
55 | 51 | (defun dap-dlv-go--populate-default-args (conf)
|
56 | 52 | "Populate CONF with the default arguments."
|
57 | 53 | (setq conf
|
58 |
| - (pcase (plist-get conf :mode) |
59 |
| - ("auto" |
60 |
| - (dap-dlv-go--populate-auto-args conf)) |
61 |
| - ("test" |
62 |
| - (dap-dlv-go--populate-test-args conf)) |
63 |
| - ("debug" |
64 |
| - (dap--put-if-absent |
65 |
| - conf :program (f-dirname (buffer-file-name)))) |
66 |
| - ("exec" |
67 |
| - (dap--put-if-absent |
68 |
| - conf :program |
69 |
| - (f-expand (read-file-name "enter path to executable: ")))) |
70 |
| - ("remote" |
71 |
| - (dap--put-if-absent conf :host (read-string "enter host: " "127.0.0.1")) |
72 |
| - (dap--put-if-absent conf :debugPort |
73 |
| - (string-to-number (read-string "enter port: " "2345")))) |
74 |
| - ("local" |
75 |
| - (dap--put-if-absent conf :cwd (f-dirname (buffer-file-name))) |
76 |
| - (dap--put-if-absent |
77 |
| - conf :processId (string-to-number (read-string "enter pid: " "2345")))))) |
| 54 | + (pcase (plist-get conf :mode) |
| 55 | + ("auto" |
| 56 | + (dap-dlv-go--populate-auto-args conf)) |
| 57 | + ("test" |
| 58 | + (dap-dlv-go--populate-test-args conf)) |
| 59 | + ("debug" |
| 60 | + (dap--put-if-absent |
| 61 | + conf :program (f-dirname (buffer-file-name)))) |
| 62 | + ("exec" |
| 63 | + (dap--put-if-absent |
| 64 | + conf :program |
| 65 | + (f-expand (read-file-name "enter path to executable: ")))) |
| 66 | + ("remote" |
| 67 | + (dap--put-if-absent conf :host (read-string "enter host: " "127.0.0.1")) |
| 68 | + (dap--put-if-absent conf :debugPort |
| 69 | + (string-to-number (read-string "enter port: " "2345")))) |
| 70 | + ("local" |
| 71 | + (dap--put-if-absent conf :cwd (f-dirname (buffer-file-name))) |
| 72 | + (dap--put-if-absent |
| 73 | + conf :processId (string-to-number (read-string "enter pid: " "2345")))))) |
78 | 74 |
|
79 | 75 | (when-let ((env-file (plist-get conf :envFile)))
|
80 |
| - (plist-put conf :env (dap-dlv-go--parse-env-file env-file))) |
| 76 | + (plist-put conf :env (dap-dlv-go--parse-env-file env-file))) |
81 | 77 |
|
82 | 78 | (let ((debug-port (if (string= (plist-get conf :mode)
|
83 |
| - "remote") |
84 |
| - (plist-get conf :debugPort) |
85 |
| - (dap--find-available-port)))) |
86 |
| - (dap--put-if-absent conf :host "localhost") |
87 |
| - (when (not (string= "remote" (plist-get conf :mode))) |
88 |
| - (plist-put |
89 |
| - conf :program-to-start |
90 |
| - (format "%s dap --listen 127.0.0.1:%s %s" dap-dlv-go-delve-path debug-port dap-dlv-go-extra-args))) |
91 |
| - (plist-put conf :debugServer debug-port)) |
| 79 | + "remote") |
| 80 | + (plist-get conf :debugPort) |
| 81 | + (dap--find-available-port)))) |
| 82 | + (dap--put-if-absent conf :host "localhost") |
| 83 | + (when (not (string= "remote" (plist-get conf :mode))) |
| 84 | + (plist-put |
| 85 | + conf :program-to-start |
| 86 | + (format "%s dap --listen 127.0.0.1:%s %s" dap-dlv-go-delve-path debug-port dap-dlv-go-extra-args))) |
| 87 | + (plist-put conf :debugServer debug-port)) |
92 | 88 |
|
93 | 89 | (if (stringp (plist-get conf :args)) (plist-put conf :args (split-string (plist-get conf :args))) ())
|
94 | 90 |
|
95 | 91 | (when (string= (plist-get conf :name) "Test function")
|
96 |
| - (-when-let (name (dap-dlv-go--extract-current--method-or-function-name t)) |
97 |
| - (dap--put-if-absent conf :args (list (format "-test.run=^%s$" name))))) |
| 92 | + (-when-let (name (dap-dlv-go--extract-current--method-or-function-name t)) |
| 93 | + (dap--put-if-absent conf :args (list (format "-test.run=^%s$" name))))) |
98 | 94 |
|
99 | 95 | (when (string= (plist-get conf :name) "Test subtest")
|
100 |
| - (-when-let (name (concat |
101 |
| - (dap-dlv-go--extract-current--method-or-function-name t) |
102 |
| - "/" |
103 |
| - (shell-quote-argument (dap-dlv-go--extract-current-subtest-name t)))) |
104 |
| - (dap--put-if-absent conf :args (list (format "-test.run=^%s" name))))) |
| 96 | + (-when-let (name (concat |
| 97 | + (dap-dlv-go--extract-current--method-or-function-name t) |
| 98 | + "/" |
| 99 | + (shell-quote-argument (dap-dlv-go--extract-current-subtest-name t)))) |
| 100 | + (dap--put-if-absent conf :args (list (format "-test.run=^%s" name))))) |
105 | 101 |
|
106 | 102 | (-> conf
|
107 |
| - (dap--put-if-absent :dlvToolPath dap-dlv-go-delve-path) |
| 103 | + (dap--put-if-absent :dlvToolPath dap-dlv-go-delve-path) |
108 | 104 |
|
109 |
| - (dap--put-if-absent :type "go") |
110 |
| - (dap--put-if-absent :name "Go Dlv Debug"))) |
| 105 | + (dap--put-if-absent :type "go") |
| 106 | + (dap--put-if-absent :name "Go Dlv Debug"))) |
111 | 107 |
|
112 | 108 | (defun dap-dlv-go--populate-auto-args (conf)
|
113 | 109 | "Populate auto arguments according to CONF."
|
114 | 110 | (dap--put-if-absent conf :program (buffer-file-name))
|
115 | 111 |
|
116 | 112 | (if (string-suffix-p "_test.go" (buffer-file-name))
|
117 |
| - (plist-put conf :mode "test") |
118 |
| - (plist-put conf :mode "debug"))) |
| 113 | + (plist-put conf :mode "test") |
| 114 | + (plist-put conf :mode "debug"))) |
119 | 115 |
|
120 | 116 | (defun dap-dlv-go--populate-test-args (conf)
|
121 | 117 | "Populate auto arguments according to CONF."
|
|
124 | 120 | (defun dap-dlv-go--extract-current--method-or-function-name (&optional no-signal?)
|
125 | 121 | "Extract current method or function name."
|
126 | 122 | (let ((symbols (lsp--get-document-symbols)))
|
127 |
| - (or (->> symbols |
128 |
| - (-keep |
129 |
| - (-lambda ((&DocumentSymbol :kind :range :selection-range)) |
130 |
| - (-let (((beg . end) (lsp--range-to-region range))) |
131 |
| - (and (or (= lsp/symbol-kind-method kind) |
132 |
| - (= lsp/symbol-kind-function kind)) |
133 |
| - (<= beg (point) end) |
134 |
| - (lsp-region-text selection-range))))) |
135 |
| - (car)) |
136 |
| - (unless no-signal? |
137 |
| - (user-error "No method or function at point"))))) |
| 123 | + (or (->> symbols |
| 124 | + (-keep |
| 125 | + (-lambda ((&DocumentSymbol :kind :range :selection-range)) |
| 126 | + (-let (((beg . end) (lsp--range-to-region range))) |
| 127 | + (and (or (= lsp/symbol-kind-method kind) |
| 128 | + (= lsp/symbol-kind-function kind)) |
| 129 | + (<= beg (point) end) |
| 130 | + (lsp-region-text selection-range))))) |
| 131 | + (car)) |
| 132 | + (unless no-signal? |
| 133 | + (user-error "No method or function at point"))))) |
138 | 134 |
|
139 | 135 | (defun dap-dlv-go--extract-current-subtest-name (&optional no-signal?)
|
140 | 136 | "Extract current subtest name."
|
141 | 137 | (save-excursion
|
142 |
| - (save-restriction |
143 |
| - (search-backward-regexp "^[[:space:]]*{" nil t) |
144 |
| - (search-forward-regexp "name:[[:space:]]+[\"`]\\(.*\\)[\"`]\," nil t) |
145 |
| - (or (match-string-no-properties 1) |
146 |
| - (unless no-signal? |
147 |
| - (user-error "No subtest at point")))))) |
| 138 | + (save-restriction |
| 139 | + (search-backward-regexp "^[[:space:]]*{" nil t) |
| 140 | + (search-forward-regexp "name:[[:space:]]+[\"`]\\(.*\\)[\"`]\," nil t) |
| 141 | + (or (match-string-no-properties 1) |
| 142 | + (unless no-signal? |
| 143 | + (user-error "No subtest at point")))))) |
148 | 144 |
|
149 | 145 | (defun dap-dlv-go--parse-env-file (file)
|
150 | 146 | "Parse env FILE."
|
151 | 147 | (with-temp-buffer
|
152 |
| - (save-match-data |
153 |
| - (find-file file) |
154 |
| - (setq-local buffer-file-name nil) |
155 |
| - (replace-regexp "[[:space:]]*#.*$" "" nil (point-min) (point-max)) |
156 |
| - (let ((res (make-hash-table))) |
157 |
| - (goto-char (point-min)) |
158 |
| - (while (search-forward-regexp "\\(^[^=].*\\)=\\(.*\\)$" nil t) |
159 |
| - (ht-set res (match-string 1) (match-string 2))) |
160 |
| - (kill-buffer) |
161 |
| - res)))) |
| 148 | + (save-match-data |
| 149 | + (find-file file) |
| 150 | + (setq-local buffer-file-name nil) |
| 151 | + (replace-regexp "[[:space:]]*#.*$" "" nil (point-min) (point-max)) |
| 152 | + (let ((res (make-hash-table))) |
| 153 | + (goto-char (point-min)) |
| 154 | + (while (search-forward-regexp "\\(^[^=].*\\)=\\(.*\\)$" nil t) |
| 155 | + (ht-set res (match-string 1) (match-string 2))) |
| 156 | + (kill-buffer) |
| 157 | + res)))) |
162 | 158 |
|
163 | 159 | (defun dap-dlv-go--get-cmd-pid (cmd)
|
164 | 160 | "Return pid of CMD."
|
165 | 161 | (string-to-number
|
166 | 162 | (cadr
|
167 |
| - (s-split-words |
168 |
| - (car |
169 |
| - (seq-filter |
170 |
| - (lambda(s) (s-contains? cmd s)) |
171 |
| - (process-lines (executable-find "ps") "aux"))))))) |
| 163 | + (s-split-words |
| 164 | + (car |
| 165 | + (seq-filter |
| 166 | + (lambda(s) (s-contains? cmd s)) |
| 167 | + (process-lines (executable-find "ps") "aux"))))))) |
172 | 168 |
|
173 | 169 | (defun dap-dlv-go--run-cmd-in-vterm (cmd buf)
|
174 | 170 | "Run CMD with vterm in BUF."
|
175 | 171 | (with-current-buffer buf
|
176 |
| - (require 'vterm) |
177 |
| - (let ((vterm-shell cmd) |
178 |
| - (vterm-kill-buffer-on-exit nil)) |
179 |
| - (vterm-mode)))) |
| 172 | + (require 'vterm) |
| 173 | + (let ((vterm-shell cmd) |
| 174 | + (vterm-kill-buffer-on-exit nil)) |
| 175 | + (vterm-mode)))) |
180 | 176 |
|
181 | 177 | (defun dap-dlv-go--run-cmd-in-vterm-get-pid (cmd buf)
|
182 | 178 | "Run CMD in vterm inside BUF and return pid."
|
|
188 | 184 | With `C-u' you can edit command before run."
|
189 | 185 | (interactive)
|
190 | 186 | (let* ((exe (f-expand (read-file-name "enter path to executable: ")))
|
191 |
| - (cmd (if (equal (car current-prefix-arg) 4) |
192 |
| - (read-string "command: " exe) |
193 |
| - exe)) |
194 |
| - (buf (generate-new-buffer |
195 |
| - (format "*%s console*" |
196 |
| - (f-base exe)))) |
197 |
| - (debug-port (dap--find-available-port)) |
198 |
| - (pid (dap-dlv-go--run-cmd-in-vterm-get-pid cmd buf))) |
199 |
| - (dap-start-debugging-noexpand (list :type "go" |
200 |
| - :request "attach" |
201 |
| - :name "Attach to running process" |
202 |
| - :mode "local" |
203 |
| - :host "localhost" |
204 |
| - :debugServer debug-port |
205 |
| - :processId pid |
206 |
| - :dlvToolPath dap-dlv-go-delve-path |
207 |
| - :program-to-start |
208 |
| - (format |
209 |
| - "%s dap --listen 127.0.0.1:%s %s" |
210 |
| - dap-dlv-go-delve-path |
211 |
| - debug-port |
212 |
| - dap-dlv-go-extra-args))) |
213 |
| - (display-buffer buf) |
214 |
| - (dap-ui--show-buffer buf))) |
| 187 | + (cmd (if (equal (car current-prefix-arg) 4) |
| 188 | + (read-string "command: " exe) |
| 189 | + exe)) |
| 190 | + (buf (generate-new-buffer |
| 191 | + (format "*%s console*" |
| 192 | + (f-base exe)))) |
| 193 | + (debug-port (dap--find-available-port)) |
| 194 | + (pid (dap-dlv-go--run-cmd-in-vterm-get-pid cmd buf))) |
| 195 | + (dap-start-debugging-noexpand (list :type "go" |
| 196 | + :request "attach" |
| 197 | + :name "Attach to running process" |
| 198 | + :mode "local" |
| 199 | + :host "localhost" |
| 200 | + :debugServer debug-port |
| 201 | + :processId pid |
| 202 | + :dlvToolPath dap-dlv-go-delve-path |
| 203 | + :program-to-start |
| 204 | + (format |
| 205 | + "%s dap --listen 127.0.0.1:%s %s" |
| 206 | + dap-dlv-go-delve-path |
| 207 | + debug-port |
| 208 | + dap-dlv-go-extra-args))) |
| 209 | + (display-buffer buf) |
| 210 | + (dap-ui--show-buffer buf))) |
215 | 211 |
|
216 | 212 | (dap-register-debug-provider "go" 'dap-dlv-go--populate-default-args)
|
217 | 213 |
|
218 | 214 | (dap-register-debug-template "Go Dlv Launch File Configuration"
|
219 |
| - (list :type "go" |
220 |
| - :request "launch" |
221 |
| - :name "Launch File" |
222 |
| - :mode "auto" |
223 |
| - :program nil |
224 |
| - :buildFlags nil |
225 |
| - :args nil |
226 |
| - :env nil)) |
| 215 | + (list :type "go" |
| 216 | + :request "launch" |
| 217 | + :name "Launch File" |
| 218 | + :mode "auto" |
| 219 | + :program nil |
| 220 | + :buildFlags nil |
| 221 | + :args nil |
| 222 | + :env nil)) |
227 | 223 |
|
228 | 224 | (dap-register-debug-template "Go Dlv Attach Configuration"
|
229 |
| - (list :type "go" |
230 |
| - :request "attach" |
231 |
| - :name "Attach to running process" |
232 |
| - :mode "auto")) |
| 225 | + (list :type "go" |
| 226 | + :request "attach" |
| 227 | + :name "Attach to running process" |
| 228 | + :mode "auto")) |
233 | 229 |
|
234 | 230 | (dap-register-debug-template "Go Dlv Launch Executable Configuration"
|
235 |
| - (list :type "go" |
236 |
| - :request "launch" |
237 |
| - :name "Launch Executable" |
238 |
| - :mode "exec" |
239 |
| - :program nil |
240 |
| - :args nil |
241 |
| - :env nil)) |
| 231 | + (list :type "go" |
| 232 | + :request "launch" |
| 233 | + :name "Launch Executable" |
| 234 | + :mode "exec" |
| 235 | + :program nil |
| 236 | + :args nil |
| 237 | + :env nil)) |
242 | 238 |
|
243 | 239 | (dap-register-debug-template "Go Dlv Remote Debug"
|
244 |
| - (list :type "go" |
245 |
| - :request "attach" |
246 |
| - :name "Dlv Remote Debug" |
247 |
| - :mode "remote")) |
| 240 | + (list :type "go" |
| 241 | + :request "attach" |
| 242 | + :name "Dlv Remote Debug" |
| 243 | + :mode "remote")) |
248 | 244 |
|
249 | 245 | (dap-register-debug-template "Go Dlv Test Current Function Configuration"
|
250 |
| - (list :type "go" |
251 |
| - :request "launch" |
252 |
| - :name "Test function" |
253 |
| - :mode "test" |
254 |
| - :program nil |
255 |
| - :args nil |
256 |
| - :env nil)) |
| 246 | + (list :type "go" |
| 247 | + :request "launch" |
| 248 | + :name "Test function" |
| 249 | + :mode "test" |
| 250 | + :program nil |
| 251 | + :args nil |
| 252 | + :env nil)) |
257 | 253 |
|
258 | 254 | (dap-register-debug-template "Go Dlv Test Current Subtest Configuration"
|
259 |
| - (list :type "go" |
260 |
| - :request "launch" |
261 |
| - :name "Test subtest" |
262 |
| - :mode "test" |
263 |
| - :program nil |
264 |
| - :args nil |
265 |
| - :env nil)) |
| 255 | + (list :type "go" |
| 256 | + :request "launch" |
| 257 | + :name "Test subtest" |
| 258 | + :mode "test" |
| 259 | + :program nil |
| 260 | + :args nil |
| 261 | + :env nil)) |
266 | 262 | (provide 'dap-dlv-go)
|
267 | 263 | ;;; dap-dlv-go.el ends here
|
0 commit comments