2016-01-18 03:06:04 +00:00
|
|
|
|
;;; funcs.el --- Go Layer functions File for Spacemacs
|
|
|
|
|
;;
|
2018-01-04 07:00:25 +00:00
|
|
|
|
;; Copyright (c) 2012-2018 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.
|
|
|
|
|
;;
|
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
|
2019-09-30 04:49:44 +00:00
|
|
|
|
(defun spacemacs//go-backend ()
|
|
|
|
|
"Returns selected backend."
|
|
|
|
|
(if go-backend
|
|
|
|
|
go-backend
|
|
|
|
|
(cond
|
|
|
|
|
((configuration-layer/layer-used-p 'lsp) 'lsp)
|
|
|
|
|
(t 'go-mode))))
|
2018-06-09 04:43:52 +00:00
|
|
|
|
|
2018-06-11 22:05:13 +00:00
|
|
|
|
(defun spacemacs//go-setup-backend ()
|
|
|
|
|
"Conditionally setup go backend"
|
2019-09-30 04:49:44 +00:00
|
|
|
|
(pcase (spacemacs//go-backend)
|
2018-06-11 22:05:13 +00:00
|
|
|
|
('lsp (spacemacs//go-setup-backend-lsp))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//go-setup-company ()
|
|
|
|
|
"Conditionally setup go company based on backend"
|
2019-09-30 04:49:44 +00:00
|
|
|
|
(pcase (spacemacs//go-backend)
|
2018-08-12 14:59:50 +00:00
|
|
|
|
('go-mode (spacemacs//go-setup-company-go))
|
2018-06-11 22:05:13 +00:00
|
|
|
|
('lsp (spacemacs//go-setup-company-lsp))))
|
|
|
|
|
|
2019-11-15 16:47:31 +00:00
|
|
|
|
(defun spacemacs//go-setup-eldoc ()
|
|
|
|
|
"Conditionally setup go eldoc based on backend"
|
|
|
|
|
(pcase (spacemacs//go-backend)
|
|
|
|
|
('go-mode (go-eldoc-setup))))
|
|
|
|
|
|
2019-09-30 04:49:44 +00:00
|
|
|
|
|
|
|
|
|
;; go-mode
|
|
|
|
|
|
2018-06-11 22:05:13 +00:00
|
|
|
|
(defun spacemacs//go-setup-company-go ()
|
|
|
|
|
(spacemacs|add-company-backends
|
|
|
|
|
:backends company-go
|
|
|
|
|
:modes go-mode
|
|
|
|
|
:variables company-go-show-annotation t
|
|
|
|
|
:append-hooks nil
|
|
|
|
|
:call-hooks t)
|
|
|
|
|
(company-mode))
|
|
|
|
|
|
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
|
|
|
|
|
;; without setting lsp-prefer-flymake to :none
|
|
|
|
|
;; golangci-lint errors won't be reported
|
2019-09-04 20:37:58 +00:00
|
|
|
|
(when go-use-golangci-lint
|
2019-03-09 11:43:35 +00:00
|
|
|
|
(message "[go] Setting lsp-prefer-flymake :none to enable golangci-lint support.")
|
|
|
|
|
(setq-local lsp-prefer-flymake :none))
|
|
|
|
|
(lsp))
|
2018-06-11 22:05:13 +00:00
|
|
|
|
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//go-setup-company-lsp ()
|
|
|
|
|
"Setup lsp auto-completion"
|
|
|
|
|
(if (configuration-layer/layer-used-p 'lsp)
|
|
|
|
|
(progn
|
|
|
|
|
(spacemacs|add-company-backends
|
|
|
|
|
:backends company-lsp
|
|
|
|
|
:modes go-mode
|
|
|
|
|
:append-hooks nil
|
|
|
|
|
:call-hooks t)
|
|
|
|
|
(company-mode))
|
|
|
|
|
(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
|
|
|
|
|
go-build
|
|
|
|
|
go-test
|
2019-12-09 07:10:53 +00:00
|
|
|
|
go-errcheck
|
|
|
|
|
go-staticcheck
|
|
|
|
|
go-unconvert
|
|
|
|
|
))
|
2019-09-04 20:37:58 +00:00
|
|
|
|
(flycheck-golangci-lint-setup))
|
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)
|
2019-04-01 21:48:10 +00:00
|
|
|
|
(compilation-start (concat "go test " (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
|
|
|
|
|
(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
|
2019-04-04 05:20:48 +00:00
|
|
|
|
(format "go run %s %s"
|
2017-08-14 11:25:31 +00:00
|
|
|
|
(shell-quote-argument (or (file-remote-p (buffer-file-name (buffer-base-buffer)) 'localname)
|
2019-04-04 05:20:48 +00:00
|
|
|
|
(buffer-file-name (buffer-base-buffer))))
|
|
|
|
|
go-run-args)))
|
|
|
|
|
|
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)))
|