2016-01-18 03:06:04 +00:00
|
|
|
|
;;; funcs.el --- Go Layer functions File for Spacemacs
|
|
|
|
|
;;
|
2021-03-22 20:11:29 +00:00
|
|
|
|
;; Copyright (c) 2012-2021 Sylvain Benner & Contributors
|
2016-01-18 03:06:04 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
2021-03-24 03:31:44 +00:00
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
;;
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
;;
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2016-01-18 03:06:04 +00:00
|
|
|
|
|
2018-06-11 22:05:13 +00:00
|
|
|
|
(defun spacemacs//go-setup-backend ()
|
|
|
|
|
"Conditionally setup go backend"
|
2021-03-18 04:11:06 +00:00
|
|
|
|
(when (eq go-backend 'lsp)
|
|
|
|
|
(spacemacs//go-setup-backend-lsp)))
|
2018-06-11 22:05:13 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//go-setup-company ()
|
|
|
|
|
"Conditionally setup go company based on backend"
|
2021-03-18 04:11:06 +00:00
|
|
|
|
(pcase go-backend
|
2020-09-20 21:33:44 +00:00
|
|
|
|
('go-mode (spacemacs|add-company-backends
|
|
|
|
|
:backends company-go
|
|
|
|
|
:modes go-mode
|
|
|
|
|
:variables company-go-show-annotation t
|
|
|
|
|
:append-hooks nil
|
|
|
|
|
:call-hooks t))
|
2021-03-18 04:11:06 +00:00
|
|
|
|
('lsp (spacemacs|add-company-backends
|
2020-09-20 21:33:44 +00:00
|
|
|
|
:backends company-capf
|
|
|
|
|
:modes go-mode))))
|
2018-06-11 22:05:13 +00:00
|
|
|
|
|
2019-11-15 16:47:31 +00:00
|
|
|
|
(defun spacemacs//go-setup-eldoc ()
|
|
|
|
|
"Conditionally setup go eldoc based on backend"
|
2021-03-18 04:11:06 +00:00
|
|
|
|
(when (eq go-backend 'go-mode)
|
|
|
|
|
(go-eldoc-setup)))
|
2019-11-15 16:47:31 +00:00
|
|
|
|
|
2020-03-25 16:02:29 +00:00
|
|
|
|
(defun spacemacs//go-setup-dap ()
|
|
|
|
|
"Conditionally setup go DAP integration."
|
|
|
|
|
;; currently DAP is only available using LSP
|
2021-03-18 04:11:06 +00:00
|
|
|
|
(when (eq go-backend 'lsp)
|
|
|
|
|
(require 'dap-go)
|
|
|
|
|
(dap-go-setup)))
|
2018-06-11 22:05:13 +00:00
|
|
|
|
|
2021-04-17 07:41:06 +00:00
|
|
|
|
(defun spacemacs//go-setup-format ()
|
|
|
|
|
"Conditionally setup format on save."
|
|
|
|
|
(if go-format-before-save
|
|
|
|
|
(add-hook 'before-save-hook 'gofmt-before-save)
|
|
|
|
|
(remove-hook 'before-save-hook 'gofmt-before-save)))
|
|
|
|
|
|
2019-09-30 04:49:44 +00:00
|
|
|
|
|
|
|
|
|
;; lsp
|
|
|
|
|
|
2018-06-11 22:05:13 +00:00
|
|
|
|
(defun spacemacs//go-setup-backend-lsp ()
|
|
|
|
|
"Setup lsp backend"
|
|
|
|
|
(if (configuration-layer/layer-used-p 'lsp)
|
2019-03-09 11:43:35 +00:00
|
|
|
|
(progn
|
2020-09-18 18:31:30 +00:00
|
|
|
|
;; without setting lsp-diagnostics-provider to :none
|
2019-03-09 11:43:35 +00:00
|
|
|
|
;; golangci-lint errors won't be reported
|
2019-09-04 20:37:58 +00:00
|
|
|
|
(when go-use-golangci-lint
|
2020-09-18 18:31:30 +00:00
|
|
|
|
(message "[go] Setting lsp-diagnostics-provider :none to enable golangci-lint support.")
|
|
|
|
|
(setq-local lsp-diagnostics-provider :none))
|
2021-06-06 20:25:00 +00:00
|
|
|
|
(lsp-deferred))
|
2018-06-11 22:05:13 +00:00
|
|
|
|
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
|
|
|
|
|
|
2019-09-30 04:49:44 +00:00
|
|
|
|
|
|
|
|
|
;; flycheck
|
|
|
|
|
|
2019-09-04 20:37:58 +00:00
|
|
|
|
(defun spacemacs//go-enable-flycheck-golangci-lint ()
|
|
|
|
|
"Enable `flycheck-golangci-linter' and disable overlapping `flycheck' linters."
|
|
|
|
|
(setq flycheck-disabled-checkers '(go-gofmt
|
|
|
|
|
go-golint
|
|
|
|
|
go-vet
|
2020-05-24 21:34:18 +00:00
|
|
|
|
;; go-build
|
2020-05-25 22:19:38 +00:00
|
|
|
|
;; go-test
|
2019-12-09 07:10:53 +00:00
|
|
|
|
go-errcheck
|
|
|
|
|
go-staticcheck
|
2020-05-25 22:19:38 +00:00
|
|
|
|
go-unconvert))
|
2020-05-24 21:34:18 +00:00
|
|
|
|
(flycheck-golangci-lint-setup)
|
|
|
|
|
|
|
|
|
|
;; Make sure to only run golangci after go-build
|
|
|
|
|
;; to ensure we show at least basic errors in the buffer
|
2020-05-25 22:19:38 +00:00
|
|
|
|
;; when golangci fails. Make also sure to run go-test if possible.
|
2020-05-24 21:34:18 +00:00
|
|
|
|
;; See #13580 for details
|
2020-05-27 16:16:47 +00:00
|
|
|
|
(flycheck-add-next-checker 'go-build '(warning . golangci-lint) t)
|
|
|
|
|
(flycheck-add-next-checker 'go-test '(warning . golangci-lint) t)
|
2020-05-25 22:19:38 +00:00
|
|
|
|
|
|
|
|
|
;; Set basic checkers explicitly as flycheck will
|
|
|
|
|
;; select the better golangci-lint automatically.
|
|
|
|
|
;; However if it fails we require these as fallbacks.
|
2020-05-27 16:16:47 +00:00
|
|
|
|
(cond ((flycheck-may-use-checker 'go-test) (flycheck-select-checker 'go-test))
|
|
|
|
|
((flycheck-may-use-checker 'go-build) (flycheck-select-checker 'go-build))))
|
2018-06-07 19:26:51 +00:00
|
|
|
|
|
2019-09-30 04:49:44 +00:00
|
|
|
|
|
|
|
|
|
;; run
|
|
|
|
|
|
2017-05-25 11:20:36 +00:00
|
|
|
|
(defun spacemacs/go-run-tests (args)
|
|
|
|
|
(interactive)
|
2020-06-10 22:12:56 +00:00
|
|
|
|
(compilation-start (concat go-test-command " " (when go-test-verbose "-v ") args " " go-use-test-args)
|
2017-05-25 11:20:36 +00:00
|
|
|
|
nil (lambda (n) go-test-buffer-name) nil))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/go-run-package-tests ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/go-run-tests ""))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/go-run-package-tests-nested ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/go-run-tests "./..."))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/go-run-test-current-function ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(if (string-match "_test\\.go" buffer-file-name)
|
2019-08-07 14:06:12 +00:00
|
|
|
|
(save-excursion
|
2020-06-26 10:28:34 +00:00
|
|
|
|
(move-end-of-line nil)
|
2019-08-07 14:06:12 +00:00
|
|
|
|
(re-search-backward "^func[ ]+\\(([[:alnum:]]*?[ ]?[*]?\\([[:alnum:]]+\\))[ ]+\\)?\\(Test[[:alnum:]_]+\\)(.*)")
|
|
|
|
|
(spacemacs/go-run-tests
|
|
|
|
|
(cond (go-use-testify-for-testing (concat "-run='Test" (match-string-no-properties 2) "' -testify.m='" (match-string-no-properties 3) "'"))
|
2019-09-12 12:00:15 +00:00
|
|
|
|
(go-use-gocheck-for-testing (concat "-check.f='" (match-string-no-properties 3) "$'"))
|
|
|
|
|
(t (concat "-run='" (match-string-no-properties 3) "$'")))))
|
2017-05-25 11:20:36 +00:00
|
|
|
|
(message "Must be in a _test.go file to run go-run-test-current-function")))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/go-run-test-current-suite ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(if (string-match "_test\.go" buffer-file-name)
|
2019-08-07 14:06:12 +00:00
|
|
|
|
(if (or go-use-testify-for-testing go-use-gocheck-for-testing)
|
|
|
|
|
(let ((test-method (if go-use-gocheck-for-testing
|
|
|
|
|
"-check.f='"
|
|
|
|
|
"-run='Test")))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(re-search-backward "^func[ ]+\\(([[:alnum:]]*?[ ]?[*]?\\([[:alnum:]]+\\))[ ]+\\)?\\(Test[[:alnum:]_]+\\)(.*)")
|
|
|
|
|
(spacemacs/go-run-tests (concat test-method (match-string-no-properties 2) "'"))))
|
|
|
|
|
(message "Testify or Gocheck is needed to test the current suite"))
|
2017-05-25 11:20:36 +00:00
|
|
|
|
(message "Must be in a _test.go file to run go-test-current-suite")))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/go-run-main ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(shell-command
|
2021-05-29 20:33:59 +00:00
|
|
|
|
(concat go-run-command " . " go-run-args)))
|
2021-05-29 14:00:48 +00:00
|
|
|
|
|
2019-09-30 04:49:44 +00:00
|
|
|
|
;; misc
|
2018-03-29 10:19:21 +00:00
|
|
|
|
(defun spacemacs/go-packages-gopkgs ()
|
|
|
|
|
"Return a list of all Go packages, using `gopkgs'."
|
2018-06-09 04:43:52 +00:00
|
|
|
|
(sort (process-lines "gopkgs") #'string<))
|
2019-09-30 04:49:44 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//go-set-tab-width ()
|
|
|
|
|
"Set the tab width."
|
|
|
|
|
(when go-tab-width
|
|
|
|
|
(setq-local tab-width go-tab-width)))
|