Commit 7474b0110843

Vincent Demeester <vincent@sbr.pm>
2024-02-21 19:20:49
tools/emacs: dape configuration…
stolen from chmouel Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 8e4b514
Changed files (1)
tools
tools/emacs/config/programming-config.el
@@ -75,6 +75,63 @@
   :after flymake
   :hook (prog-mode . flymake-codespell-setup-backend))
 
+(defun my-gotest-get-current-test()
+  "Get the current test name, if we have a subtest (starting with name) then use it."
+  (interactive)
+  (require 'which-func)
+  (let ((subtest (when-let* ((subtest
+                              (progn
+                                (save-excursion
+                                  (goto-char (line-beginning-position))
+                                  (re-search-forward "name:[[:blank:]]*\"\\([^\"]*\\)\"" (line-end-position) t)))))
+                   (if subtest
+                       (shell-quote-argument (replace-regexp-in-string " " "_" (match-string-no-properties 1))))))
+        (gotest (when-let* ((test-name (which-function)))
+                  (if test-name test-name
+                    (error "No test selected")))))
+    (concat (format "^%s%s$" gotest (if subtest (concat "/" subtest) "")))))
+
+(use-package dape
+  :commands (my-dape-go-test-at-point)
+  :after go-ts-mode
+  :bind
+  (:map go-ts-mode-map
+        ("<f5>" . (lambda()(interactive)
+                    (if (dape--live-connections)
+                        (call-interactively 'dape-continue)
+                      (call-interactively 'dape))))
+        ("S-<f5>"   . dape-stop)
+        ("C-S-<f5>" . dape-restart)
+        ("<f9>"     . dape-breakpoint-toggle)
+        ("<f10>"    . dape-next)
+        ("<f11>"    . dape-step-in)
+        ("S-<f11>"    . dape-step-out))
+  :hook
+  (go-ts-mode . (lambda()
+                  (interactive)
+                  (if (string-suffix-p "_test.go"  (buffer-name))
+                      (setq-local dape-command '(delve-unit-test)))))
+  :config
+  (defun my-dape-go-test-at-point ()
+    (interactive)
+    (dape (dape--config-eval-1
+           `(modes (go-mode go-ts-mode)
+                   ensure dape-ensure-command
+                   fn dape-config-autoport
+                   command "dlv"
+                   command-args ("dap" "--listen" "127.0.0.1::autoport")
+                   command-cwd dape-cwd-fn
+                   port :autoport
+                   :type "debug"
+                   :request "launch"
+                   :mode "test"
+                   :cwd dape-cwd-fn
+                   :program (lambda () (concat "./" (file-relative-name default-directory (funcall dape-cwd-fn))))
+                   :args (lambda ()
+                           (when-let* ((test-name (my-gotest-get-current-test)))
+                             (if test-name `["-test.run" ,test-name]
+                               (error "No test selected")))))))))
+
 ;; (use-package copilot
 ;;   :preface
 ;;   (unless (package-installed-p 'copilot)