Commit e1559d53bcd0

Vincent Demeester <vincent@sbr.pm>
2024-07-09 18:19:46
tools/emacs: add run-command go recipes…
it is not complete, but it is a start. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent af1fe00
Changed files (1)
tools
emacs
tools/emacs/config/programming-go.el
@@ -3,15 +3,19 @@
 ;;; Go programming language configuration
 ;;; Code:
 
+(declare-function project-root "project")
+(declare-function project-current "project")
+(declare-function vde-project--project-root-or-default-directory "proj-func")
 
 (use-package gotest
   :commands (my-gotest-maybe-ts-run go-test--get-current-test-info)
-  :after go-mode
+  :after go-ts-mode
   :custom
   (go-test-verbose t)
   :hook
   (go-test-mode . (lambda () (pop-to-buffer (get-buffer "*Go Test*"))))
   (go-mode . (lambda ()(interactive) (setq go-run-args "-v")))
+  (go-ts-mode . (lambda ()(interactive) (setq go-run-args "-v")))
   :config
   (defun my-go-test-current-project()
     (interactive)
@@ -30,6 +34,57 @@
                                                (replace-regexp-in-string " " "_" testrunname)))))
       (go-test--go-test (concat "-run " gotest "\\$ .")))))
 
+(defun go-mode-p ()
+  "Return non-nil value when the major mode is `go-mode' or `go-ts-mode'."
+  (memq major-mode '(go-ts-mode go-mode)))
+
+;; TODO (defun run-command-recipe-ko ())
+
+(defun run-command-recipe-go ()
+  "Go `run-command' recipes."
+  (let ((dir (vde-project--project-root-or-default-directory)))
+    (when (or (go-mode-p) (file-exists-p (expand-file-name "go.mod" dir)))
+      (append
+       (and (buffer-file-name) (go-mode-p)
+	    (list
+	     (list :command-name "gofumpt"
+		   :command-line (concat "gofumpt -extra -w " (buffer-file-name))
+		   :working-dir dir
+		   :display "gofumpt (reformat) file")
+	     (list :command-name "go-fmt"
+		   :command-line (concat "go fmt " (buffer-file-name))
+		   :working-dir dir
+		   :display "gofmt (reformat) file")
+	     (list :command-name "go-run"
+		   :command-line (concat "go run " (buffer-file-name))
+		   :working-dir dir
+		   :display "Compile, execute file")))
+       (and (string-suffix-p "_test.go" buffer-file-name) (go-mode-p)
+	    (list
+	     ;; go-test--get-current-file-tests
+	     (let ((runArgs (go-test--get-current-file-tests))
+		   (package (file-name-directory (concat "./"(file-relative-name (buffer-file-name) dir)))))
+		 (list :command-name "go-test-file"
+		       :command-line (concat "go test -v " package " -run " (shell-quote-argument runArgs))
+		       :working-dir dir
+		       :display (concat "Test file " (concat "./"(file-relative-name (buffer-file-name) dir)))
+		       :runner 'run-command-runner-compile))))
+       ;; TODO: handle test file as well
+       (list
+	(list :command-name "go-build-project"
+	      :command-line "go build -v ./..."
+	      :working-dir dir
+	      :display "compile package and dependencies"
+	      :runner 'run-command-runner-compile)
+	(list :command-name "go-test-project"
+	      :command-line "go test ./..."
+	      :working-dir dir
+	      :display "test all"
+	      :runner 'run-command-runner-compile))))))
+
+(with-eval-after-load 'run-command
+  (add-to-list 'run-command-recipes 'run-command-recipe-go))
+
 (use-package go-ts-mode
   :mode (("\\.go$" . go-ts-mode)
          ("\\.go" . go-ts-mode)