nftable-migration
  1;;; programming-go.el --- -*- lexical-binding: t; -*-
  2;;; Commentary:
  3;;; Go programming language configuration
  4;;; Code:
  5
  6(declare-function project-root "project")
  7(declare-function project-current "project")
  8(declare-function vde-project--project-root-or-default-directory "proj-func")
  9(declare-function go-test--get-current-file-tests "gotest")
 10
 11(use-package gotest
 12  :commands (my-gotest-maybe-ts-run go-test--get-current-test-info go-test--get-current-file-tests)
 13  :after go-ts-mode
 14  :custom
 15  (go-test-verbose t)
 16  :hook
 17  (go-test-mode . (lambda () (pop-to-buffer (get-buffer "*Go Test*"))))
 18  (go-mode . (lambda ()(interactive) (setq go-run-args "-v")))
 19  (go-ts-mode . (lambda ()(interactive) (setq go-run-args "-v")))
 20  :config
 21  (defun my-go-test-current-project()
 22    (interactive)
 23    (let ((default-directory (project-root (project-current t))))
 24      (go-test-current-project)))
 25  (defun my-gotest-maybe-ts-run()
 26    (interactive)
 27    (let ((testrunname)
 28          (gotest (cadr (go-test--get-current-test-info))))
 29      (save-excursion
 30        (goto-char (line-beginning-position))
 31        (re-search-forward "name:[[:blank:]]*\"\\([^\"]*\\)\"" (line-end-position) t))
 32      (setq testrunname (match-string-no-properties 1))
 33      (if testrunname
 34          (setq gotest (format "%s/%s" gotest (shell-quote-argument
 35                                               (replace-regexp-in-string " " "_" testrunname)))))
 36      (go-test--go-test (concat "-run " gotest "\\$ .")))))
 37
 38(use-package gotest-ts
 39  :bind (("C-c C-t t" . gotest-ts-run-dwim)))
 40
 41(defun go-mode-p ()
 42  "Return non-nil value when the major mode is `go-mode' or `go-ts-mode'."
 43  (memq major-mode '(go-ts-mode go-mode)))
 44
 45;; TODO (defun run-command-recipe-ko ())
 46
 47(defun run-command-recipe-go ()
 48  "Go `run-command' recipes."
 49  (when (buffer-file-name) ;; no buffer-file-name means virtual buffer (dired, …)
 50    
 51    (let* ((dir (vde-project--project-root-or-default-directory))
 52	   (package (file-name-directory (concat "./" (file-relative-name (buffer-file-name) dir)))))
 53      (when (or (go-mode-p) (file-exists-p (expand-file-name "go.mod" dir)))
 54	(append
 55	 (and (buffer-file-name) (go-mode-p)
 56	      (list
 57	       (list :command-name "gofumpt"
 58		     :command-line (concat "gofumpt -extra -w " (buffer-file-name))
 59		     :working-dir dir
 60		     :display "gofumpt (reformat) file")
 61	       (list :command-name "go-fmt"
 62		     :command-line (concat "go fmt " (buffer-file-name))
 63		     :working-dir dir
 64		     :display "gofmt (reformat) file")
 65	       (list :command-name "go-run"
 66		     :command-line (concat "go run " (buffer-file-name))
 67		     :working-dir dir
 68		     :display "Compile, execute file")))
 69	 (and (string-suffix-p "_test.go" buffer-file-name) (go-mode-p)
 70	      (list
 71	       (let ((runArgs (go-test--get-current-file-tests)))
 72		 ;; go test current test
 73		 ;; go test current file
 74		 (list :command-name "go-test-file"
 75		       :command-line (concat "go test -v " package " -run " (shell-quote-argument runArgs))
 76		       :working-dir dir
 77		       :display (concat "Test file " (concat "./"(file-relative-name (buffer-file-name) dir)))
 78		       :runner 'run-command-runner-compile)
 79		 )))
 80	 ;; TODO: handle test file as well
 81	 (list
 82	  (list :command-name "go-build-project"
 83		:command-line "go build -v ./..."
 84		:working-dir dir
 85		:display "compile package and dependencies"
 86		:runner 'run-command-runner-compile)
 87	  (list :command-name "go-test-project"
 88		:command-line "go test ./..."
 89		:working-dir dir
 90		:display "test all"
 91		:runner 'run-command-runner-compile)
 92	  (list :command-name "go-test-package"
 93		:command-line (concat "go test -v " package)
 94		:working-dir dir
 95		:display (concat "Test package " package)
 96		:runner 'run-command-runner-compile)))))))
 97
 98(with-eval-after-load 'run-command
 99  (add-to-list 'run-command-recipes 'run-command-recipe-go))
100
101(use-package go-ts-mode
102  :mode (("\\.go$" . go-ts-mode)
103         ("\\.go" . go-ts-mode)
104         ("\\.go\\'" . go-ts-mode)))
105
106(provide 'programming-go)
107;;; programming-go.el ends here