-
Notifications
You must be signed in to change notification settings - Fork 1
/
go.el
253 lines (213 loc) · 7.4 KB
/
go.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(install 'go-mode)
(when (executable-find "gofumpt") (setq lsp-go-use-gofumpt t))
(defun fg/convert-go-stack-trace-file-names (orig-fun &rest args)
(let* ((marker (car args))
(fn (cadr args))
(fn-path)
(dir (caddr args))
(fmts (cadddr args))
(git-fs)
(dd))
;; pkg.fn_sth.go:111
;; fn_sth.go:222
(if (not (string-match "[ \t]*\\([^/.]+\\.\\)?\\(.+\\.go\\)" fn))
(progn
(apply orig-fun args))
(let* ((pkg (match-string 1 fn))
(go-fn (match-string 2 fn)))
(setq fn-path (if (and pkg (> (length pkg) 0))
(format "/%s/%s" (substring pkg 0 (1- (length pkg))) go-fn)
(format "/%s" go-fn))))
(setq dd (with-current-buffer (marker-buffer marker) (locate-dominating-file "." ".git")))
(setq git-fs
(let ((default-directory dd))
(cl-remove-if-not (lambda (git-fn) (string-suffix-p fn-path git-fn))
(process-lines "git" "ls-files" "--full-name"))))
(if (not git-fs)
(progn
(apply orig-fun args))
(setq fn (format "%s%s" dd (car git-fs)))
(apply orig-fun (list marker fn dir fmts))))))
;; monkey patch compilation helper to jump to error file.
;; stack trace doesn't always contain the absolute path, but pkg/fn.go
;; don't see another way to customize the file matching 🥷
(advice-add 'compilation-find-file-1 :around #'fg/convert-go-stack-trace-file-names)
(defun fg/golang-customizations ()
(defalias 'go-play-buffer nil)
(defalias 'go-play-region nil)
(eldoc-mode 1)
(require 'lsp-mode)
(lsp-register-custom-settings
'(("gopls.staticcheck" nil t)
("gopls.allowImplicitNetworkAccess" t t)
))
(setq lsp-go-build-flags ["-tags=test_dbt,local_development"])
(let ((env (make-hash-table)))
(puthash "GOPROXY" "proxy.golang.org,direct" env)
(setq lsp-go-env env))
(lsp-deferred)
(lsp-diagnostics-modeline-mode)
(subword-mode 1)
(yas-minor-mode)
(setq tab-width 4)
(setq display-fill-column-indicator-character ?\u2502)
(display-fill-column-indicator-mode +1)
(add-hook 'before-save-hook #'lsp-format-buffer t t)
(add-hook 'before-save-hook #'lsp-organize-imports t t))
(add-hook 'go-mode-hook 'fg/golang-customizations)
(defun fg/go-compilation-buffer-name ()
(let* ((proj-path (expand-file-name (fg/guess-project-directory)))
(proj-name (file-name-nondirectory (directory-file-name proj-path)))
(loc (substring (directory-file-name (expand-file-name default-directory))
(- (length proj-path) (length proj-name) 1))))
(format "*go-compilation[%s]*" proj-name)))
(defun fg/go-compilation-buffer-p (buf)
(string-prefix-p "*go-compilation[" (buffer-name buf)))
(defun fg/go-find-compilation-buffer ()
(if (get-buffer (fg/go-compilation-buffer-name))
(get-buffer (fg/go-compilation-buffer-name))
(-first 'fg/go-compilation-buffer-p (buffer-list))))
(defun fg/go-recompile ()
(interactive)
(let* ((buf (fg/go-find-compilation-buffer)))
(when buf
(with-current-buffer buf (recompile)))))
(defun fg/kill-go-buffers ()
(interactive)
(mapc (lambda (b) (let* ((name (buffer-name b))
(mm (with-current-buffer b major-mode)))
(when (or (eq mm 'go-mode)
(string-prefix-p "*gopls" name)
(string-prefix-p "*go-compilation" name))
(kill-buffer b))))
(buffer-list)))
(defun fg/go-run-this-test ()
(interactive)
(fg/go-run (format "test -v -vet=all -tags test_dbt,local_development -run %s"
(save-excursion
(re-search-backward "func \\(Test.+\\)(" (point-min))
(match-string 1)))))
(defun fg/go-build-this ()
(interactive)
(fg/go-run "build ."))
(defun fg/go-run-this ()
(interactive)
(fg/go-run "run *.go"))
(defun fg/go-run-all-tests ()
(interactive)
(fg/go-run "test -v -vet=all"))
(defun fg/go-run (cmd)
(let* ((cmd (format "%s %s"
(locate-file "go" exec-path exec-suffixes)
cmd))
(current-dir default-directory)
(dir (file-name-nondirectory (directory-file-name default-directory)))
(buf (fg/go-compilation-buffer-name)))
(when (and (get-buffer buf)
(with-current-buffer buf (not (string= current-dir default-directory))))
(kill-buffer buf))
(if (get-buffer buf)
(with-current-buffer buf
(compile cmd))
(compile cmd)
(with-current-buffer "*compilation*"
(rename-buffer buf)
(toggle-truncate-lines -1)))))
(defun fg/go-make-tests ()
(interactive)
(let* ((dir (file-name-nondirectory (directory-file-name default-directory)))
(buf (fg/go-compilation-buffer-name)))
(if (get-buffer buf) (with-current-buffer buf (compile "make test"))
(compile "make test")
(with-current-buffer "*compilation*" (rename-buffer buf)))))
(defmacro fg/go-goto-error (&rest body)
`(let ((buf (fg/go-find-compilation-buffer)))
(when buf
(with-current-buffer buf
,@body
(compile-goto-error)))))
(defun fg/go-goto-first-error ()
(interactive)
(fg/go-goto-error (goto-char (point-min)) (compilation-next-error 1)))
(defun fg/go-goto-next-error ()
(interactive)
(fg/go-goto-error (compilation-next-error 1)))
(defun fg/go-goto-previous-error ()
(interactive)
(fg/go-goto-error (compilation-previous-error 1)))
(defun fg/go-ignore-all-tests ()
(interactive)
(save-excursion
(goto-char (point-min))
(replace-regexp "func Test\\([^(]+\\)(" "func IgnoreTest\\1(" nil (point-min) (point-max))))
(defun fg/go-enable-all-tests ()
(interactive)
(save-excursion
(goto-char (point-min))
(replace-regexp "func IgnoreTest\\([^(]+\\)(" "func Test\\1(" nil (point-min) (point-max))))
(defun fg/go-play (name)
(interactive
(list (read-string "Name: " (format-time-string "%Y-%m-%dT%H-%M") t)))
(let* ((temporary-file-directory (expand-file-name (format "~/src/github.com/fgeller/go-playground/%s" name)))
(temp-file (progn
(make-directory temporary-file-directory t)
(make-temp-file "go-play" nil ".go"))))
(find-file temp-file)
(insert "package main
import \"fmt\"
func main() {
fmt.Printf(\"\")
}")
(goto-char 55)
(go-mode)
(define-key (current-local-map) (kbd "C-c C-k") 'fg/go-play-kill)
(define-key (current-local-map) (kbd "C-c C-c") 'fg/go-play-run)))
(defun fg/go-play-run ()
(interactive)
(save-buffer)
(exec-path-from-shell-initialize)
(compile (format "/opt/homebrew/bin/go run %s" buffer-file-name)))
(defun fg/go-play-kill ()
(interactive)
(save-buffer)
(delete-file buffer-file-name)
(kill-buffer))
(defun fg/explode-func-args ()
(interactive)
(let* ((start-pos (save-excursion
(unless (looking-at-p "(") (search-backward "("))
(point)))
(end-pos (save-excursion
(goto-char start-pos)
(fg/jump-to-matching-paren)
(point))))
(narrow-to-region start-pos end-pos)
(beginning-of-buffer)
(forward-char 1)
(insert "\n")
(while (search-forward "," nil t)
(insert "\n"))
(end-of-buffer)
(forward-char -1)
(unless (looking-at-p ",") (insert ","))
(insert "\n")
(widen)))
(defun fg/collapse-func-args ()
(interactive)
(let* ((start-pos (save-excursion
(search-backward "(")
(point)))
(end-pos (save-excursion
(goto-char start-pos)
(fg/jump-to-matching-paren)
(point))))
(narrow-to-region start-pos end-pos)
(beginning-of-buffer)
(replace-regexp "\n" "" nil start-pos end-pos)
(end-of-buffer)
(search-backward ")")
(skip-chars-backward " \t(,")
(when (looking-at-p ",") (delete-char 1))
(goto-char start-pos)
(widen)
))