programming-go.el (3978B)
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 (defun go-mode-p () 39 "Return non-nil value when the major mode is `go-mode' or `go-ts-mode'." 40 (memq major-mode '(go-ts-mode go-mode))) 41 42 ;; TODO (defun run-command-recipe-ko ()) 43 44 (defun run-command-recipe-go () 45 "Go `run-command' recipes." 46 (when (buffer-file-name) ;; no buffer-file-name means virtual buffer (dired, …) 47 48 (let* ((dir (vde-project--project-root-or-default-directory)) 49 (package (file-name-directory (concat "./" (file-relative-name (buffer-file-name) dir))))) 50 (when (or (go-mode-p) (file-exists-p (expand-file-name "go.mod" dir))) 51 (append 52 (and (buffer-file-name) (go-mode-p) 53 (list 54 (list :command-name "gofumpt" 55 :command-line (concat "gofumpt -extra -w " (buffer-file-name)) 56 :working-dir dir 57 :display "gofumpt (reformat) file") 58 (list :command-name "go-fmt" 59 :command-line (concat "go fmt " (buffer-file-name)) 60 :working-dir dir 61 :display "gofmt (reformat) file") 62 (list :command-name "go-run" 63 :command-line (concat "go run " (buffer-file-name)) 64 :working-dir dir 65 :display "Compile, execute file"))) 66 (and (string-suffix-p "_test.go" buffer-file-name) (go-mode-p) 67 (list 68 (let ((runArgs (go-test--get-current-file-tests))) 69 ;; go test current test 70 ;; go test current file 71 (list :command-name "go-test-file" 72 :command-line (concat "go test -v " package " -run " (shell-quote-argument runArgs)) 73 :working-dir dir 74 :display (concat "Test file " (concat "./"(file-relative-name (buffer-file-name) dir))) 75 :runner 'run-command-runner-compile) 76 ))) 77 ;; TODO: handle test file as well 78 (list 79 (list :command-name "go-build-project" 80 :command-line "go build -v ./..." 81 :working-dir dir 82 :display "compile package and dependencies" 83 :runner 'run-command-runner-compile) 84 (list :command-name "go-test-project" 85 :command-line "go test ./..." 86 :working-dir dir 87 :display "test all" 88 :runner 'run-command-runner-compile) 89 (list :command-name "go-test-package" 90 :command-line (concat "go test -v " package) 91 :working-dir dir 92 :display (concat "Test package " package) 93 :runner 'run-command-runner-compile))))))) 94 95 (with-eval-after-load 'run-command 96 (add-to-list 'run-command-recipes 'run-command-recipe-go)) 97 98 (use-package go-ts-mode 99 :mode (("\\.go$" . go-ts-mode) 100 ("\\.go" . go-ts-mode) 101 ("\\.go\\'" . go-ts-mode))) 102 103 (provide 'programming-go) 104 ;;; programming-go.el ends here