-
Notifications
You must be signed in to change notification settings - Fork 11
/
go-playground.el
233 lines (191 loc) · 7.57 KB
/
go-playground.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
;;; go-playground.el --- Local Golang playground for short snippets.
;; Copyright (C) 2015-2024 Alexander I.Grafov and the project
;; contibutors.
;; Author: Alexander I.Grafov <grafov@inet.name>
;; URL: https://github.com/grafov/go-playground
;; Keywords: tools, golang
;; Version: 1.8.2
;; Package-Requires: ((emacs "24") (go-mode "1.4.0") (gotest "0.13.0"))
;; 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/>.
;;; Commentary:
;; Local playground for the Go programs similar to play.golang.org.
;; `M-x go-playground` and type you golang code then make&run it with `C-Return`.
;; Playground works around `go-mode` and requires preconfigured environment
;; for Go language.
;; I recommend you to use `goimports` instead of `gofmt` for automatically make
;; import clauses. It very comfortable especially for experimenting with code
;; in playground.
;; You may push code to play.golang.org with go-mode' function `go-play-buffer`.
;;
;;; Code:
(require 'go-mode)
(require 'gotest)
(require 'compile)
(require 'time-stamp)
(require 'subr-x)
(defgroup go-playground nil
"Options specific to Go Playground."
:group 'go)
(defcustom go-playground-ask-file-name nil
"Non-nil means we ask for a name for the snippet.
By default it will be created as snippet.go"
:type 'boolean
:group 'go-playground)
(defcustom go-playground-confirm-deletion t
"Non-nil means you will be asked for confirmation on the snippet deletion with `go-playground-rm'.
By default confirmation required."
:type 'boolean
:group 'go-playground)
(defcustom go-playground-basedir "~/go/src/playground"
"Base directory for playground snippets. Better to set it under GOPATH."
:type 'file
:group 'go-playground)
(defcustom go-playground-compile-command (concat go-command " mod tidy; " go-command " run ./...")
"The commands used for compilation.
Use \";\" or any other appropriate shell separator if you need several commands in one session."
:type 'string
:group 'go-playground)
(defcustom go-playground-pre-rm-hook nil
"Hook run before a snippet is removed."
:type 'hook
:group 'go-playground)
(defcustom go-playground-init-command "go mod init"
"The shell command executed once when the snippet just created."
:type 'string
:group 'go-playground)
;;;###autoload
(define-minor-mode go-playground-mode
"A place for playing with golang code and export it in short snippets."
:init-value nil
:lighter "Play(Go)"
:keymap '(([C-return] . go-playground-exec)
([M-return] . go-playground-cmd)))
(defun go-playground-snippet-file-name(&optional snippet-name)
(let* ((file-name (cond (snippet-name)
(go-playground-ask-file-name
(read-string "Go Playground filename: "))
("snippet")))
(snippet-dir (go-playground-snippet-unique-dir file-name)))
(let ((default-directory snippet-dir))
(call-process-shell-command go-playground-init-command))
(concat snippet-dir "/" file-name ".go")))
;
(defun go-playground-save-and-run ()
"Obsoleted by go-playground-exec."
(interactive)
(go-playground-exec))
(defun go-playground-exec ()
"Save the buffer then runs Go compiler for executing the code."
(interactive)
(if (go-playground-inside)
(progn
(save-buffer t)
(make-local-variable 'compile-command)
(compile go-playground-compile-command))))
(defun go-playground-cmd (cmd)
"Save the buffer then apply custom compile command from
minibuffer to the files or buffer."
(interactive "scompile command: ")
(if (go-playground-inside)
(progn
(save-buffer t)
(make-local-variable 'compile-command)
(compile cmd))))
;;;###autoload
(defun go-playground ()
"Run playground for Go language in a new buffer."
(interactive)
(let ((snippet-file-name (go-playground-snippet-file-name)))
(switch-to-buffer (create-file-buffer snippet-file-name))
(go-playground-insert-template-head "snippet of code")
(insert "package main
import (
\"fmt\"
)
func main() {
fmt.Println(\"Results:\")
}
")
(backward-char 3)
(go-mode)
(go-playground-mode)
(set-visited-file-name snippet-file-name t)))
(defun go-playground-insert-template-head (description)
(insert "// -*- mode:go;mode:go-playground -*-
// " description " @ " (time-stamp-string "%:y-%02m-%02d %02H:%02M:%02S") "
// === Go Playground ===
// Execute the snippet with: Ctl-Return
// Provide custom arguments to compile with: Alt-Return
// Other useful commands:
// - remove the snippet completely with its dir and all files: (go-playground-rm)
// - upload the current buffer to playground.golang.org: (go-playground-upload)
"))
(defun go-playground-rm ()
"Remove files of the current snippet together with directory of this snippet."
(interactive)
(if (go-playground-inside)
(if (or (not go-playground-confirm-deletion)
(y-or-n-p (format "Do you want delete whole snippet dir %s? "
(file-name-directory (buffer-file-name)))))
(progn
(save-buffer)
(run-hooks 'go-playground-pre-rm-hook)
(delete-directory (file-name-directory (buffer-file-name)) t t)
(kill-buffer)))
(message "Won't delete this! Because %s is not under the path %s. Remove the snippet manually!"
(buffer-file-name) go-playground-basedir)))
;;;###autoload
(defun go-playground-remove-current-snippet ()
"Obsoleted by `go-playground-rm'."
(interactive)
(go-playground-rm))
;;;###autoload
(defun go-playground-download (url)
"Download a paste from the play.golang.org and insert it in a new local playground buffer.
Tries to look for a URL at point."
(interactive (list (read-from-minibuffer "Playground URL: " (ffap-url-p (ffap-string-at-point 'url)))))
(with-current-buffer
(let ((url-request-method "GET") url-request-data url-request-extra-headers)
(url-retrieve-synchronously (concat url ".go")))
(let* ((snippet-file-name (go-playground-snippet-file-name)) (buffer (create-file-buffer snippet-file-name)))
(goto-char (point-min))
(re-search-forward "\n\n")
(copy-to-buffer buffer (point) (point-max))
(kill-buffer)
(with-current-buffer buffer
(goto-char (point-min))
(go-playground-insert-template-head (concat url " imported"))
(go-mode)
(go-playground-mode)
(set-visited-file-name snippet-file-name t)
(switch-to-buffer buffer)))))
(defun go-playground-upload ()
"Upload the current buffer to play.golang.org and return the short URL of the playground."
(interactive)
(if (not (go-playground-inside))
(message "Not in a Go Playground buffer!")
(go-play-buffer)))
(defun go-playground-snippet-unique-dir (prefix)
"Get unique directory under GOPATH/`go-playground-basedir`."
(let ((dir-name (concat go-playground-basedir "/"
(if (and prefix go-playground-ask-file-name) (concat prefix "-"))
(time-stamp-string "at-%:y-%02m-%02d-%02H%02M%02S"))))
(make-directory dir-name t)
dir-name))
(defun go-playground-inside ()
"Is the current buffer is valid go-playground buffer."
(and (bound-and-true-p go-playground-mode)
buffer-file-name
(string-prefix-p (file-truename go-playground-basedir)
(file-truename buffer-file-name))))
(provide 'go-playground)
;;; go-playground.el ends here