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