Skip to content

Commit 540b43e

Browse files
committed
Add ability to turn off goimports.
As suggested by @dominikh. Have it on by default.
1 parent 22e1ff8 commit 540b43e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

playground/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<span id="controls">
1313
<input type="button" value="Run" ng-click="run(false)" />
1414
<input type="button" value="Format" ng-click="format()" />
15+
<label title="Rewrite imports on Format">
16+
<input id="imports" type="checkbox" checked />Imports
17+
</label>
1518
<input type="button" value="Share" ng-click="share()" />
1619
<input type="text" class="show-share-url-{{showShareUrl}}" id="share-url" value="{{shareUrl}}" onfocus="select()" />
1720
</span>

playground/playground.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"go/ast"
6+
"go/format"
67
"go/parser"
78
"go/scanner"
89
"go/token"
@@ -52,7 +53,7 @@ func main() {
5253
})
5354
}()
5455
} else {
55-
scope.Set("code", "package main\n\nimport (\n\t\"fmt\"\n\t\"github.com/gopherjs/gopherjs/js\"\n)\n\nfunc main() {\n\tfmt.Println(\"Hello, playground\")\n\tjs.Global.Call(\"alert\", \"Hello, JavaScript\")\n\tprintln(\"Hello, JS console\")\n}\n")
56+
scope.Set("code", "package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/gopherjs/gopherjs/js\"\n)\n\nfunc main() {\n\tfmt.Println(\"Hello, playground\")\n\tjs.Global.Call(\"alert\", \"Hello, JavaScript\")\n\tprintln(\"Hello, JS console\")\n}\n")
5657
close(codeReady)
5758
}
5859
scope.Set("shareUrl", "")
@@ -201,7 +202,14 @@ func main() {
201202

202203
scope.Set("format", func() {
203204
go func() {
204-
out, err := imports.Process("foo.go", []byte(scope.Get("code").String()), nil)
205+
var out []byte
206+
var err error
207+
switch dom.GetWindow().Document().GetElementByID("imports").(*dom.HTMLInputElement).Checked {
208+
case true:
209+
out, err = imports.Process("prog.go", []byte(scope.Get("code").String()), nil)
210+
case false:
211+
out, err = format.Source([]byte(scope.Get("code").String()))
212+
}
205213
if err != nil {
206214
scope.Apply(func() {
207215
scope.Set("output", []Line{Line{"type": "err", "content": err.Error()}})

0 commit comments

Comments
 (0)