-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathfuncs.el
162 lines (132 loc) · 5.43 KB
/
funcs.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
;;; funcs.el --- Go Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs//go-backend ()
"Returns selected backend."
(if go-backend
go-backend
(cond
((configuration-layer/layer-used-p 'lsp) 'lsp)
(t 'go-mode))))
(defun spacemacs//go-setup-backend ()
"Conditionally setup go backend"
(pcase (spacemacs//go-backend)
('lsp (spacemacs//go-setup-backend-lsp))))
(defun spacemacs//go-setup-company ()
"Conditionally setup go company based on backend"
(pcase (spacemacs//go-backend)
('go-mode (spacemacs//go-setup-company-go))))
(defun spacemacs//go-setup-eldoc ()
"Conditionally setup go eldoc based on backend"
(pcase (spacemacs//go-backend)
('go-mode (go-eldoc-setup))))
(defun spacemacs//go-setup-dap ()
"Conditionally setup go DAP integration."
;; currently DAP is only available using LSP
(pcase (spacemacs//go-backend)
(`lsp (spacemacs//go-setup-lsp-dap))))
;; go-mode
(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))
;; lsp
(defun spacemacs//go-setup-backend-lsp ()
"Setup lsp backend"
(if (configuration-layer/layer-used-p 'lsp)
(progn
;; without setting lsp-diagnostic-package to :none
;; golangci-lint errors won't be reported
(when go-use-golangci-lint
(message "[go] Setting lsp-diagnostic-package :none to enable golangci-lint support.")
(setq-local lsp-diagnostic-package :none))
(lsp))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
(defun spacemacs//go-setup-dap ()
"Conditionally setup go DAP integration."
;; currently DAP is only available using LSP
(pcase (spacemacs//go-backend)
(`lsp (spacemacs//go-setup-lsp-dap))))
(defun spacemacs//go-setup-lsp-dap ()
"Setup DAP integration."
(require 'dap-go)
(dap-go-setup))
;; flycheck
(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
go-errcheck
go-staticcheck
go-unconvert)
flycheck-golangci-lint-tests t
flycheck-golangci-lint-enable-all t)
(flycheck-golangci-lint-setup)
;; Make sure to only run golangci after go-build
;; to ensure we show at least basic errors in the buffer
;; when golangci fails.
;; See #13580 for details
(flycheck-add-next-checker 'go-build 'golangci-lint t)
(flycheck-select-checker 'go-build))
;; run
(defun spacemacs/go-run-tests (args)
(interactive)
(compilation-start (concat "go test " (when go-test-verbose "-v ") args " " go-use-test-args)
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)
(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) "'"))
(go-use-gocheck-for-testing (concat "-check.f='" (match-string-no-properties 3) "$'"))
(t (concat "-run='" (match-string-no-properties 3) "$'")))))
(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)
(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"))
(message "Must be in a _test.go file to run go-test-current-suite")))
(defun spacemacs/go-run-main ()
(interactive)
(shell-command
(format "go run %s %s"
(shell-quote-argument (or (file-remote-p (buffer-file-name (buffer-base-buffer)) 'localname)
(buffer-file-name (buffer-base-buffer))))
go-run-args)))
;; misc
(defun spacemacs/go-packages-gopkgs ()
"Return a list of all Go packages, using `gopkgs'."
(sort (process-lines "gopkgs") #'string<))
(defun spacemacs//go-set-tab-width ()
"Set the tab width."
(when go-tab-width
(setq-local tab-width go-tab-width)))