Skip to content

Commit 0c546b3

Browse files
committed
Add ability to fix imports via goimports.
Add ability to turn off goimports. As suggested by @dominikh.
1 parent 40f830c commit 0c546b3

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
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: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/gopherjs/gopherjs.github.io/playground/internal/imports"
1314
"github.com/gopherjs/gopherjs/compiler"
1415
"github.com/gopherjs/gopherjs/js"
1516
"github.com/neelance/go-angularjs"
@@ -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", "")
@@ -200,13 +201,26 @@ func main() {
200201
}()
201202

202203
scope.Set("format", func() {
203-
out, err := format.Source([]byte(scope.Get("code").String()))
204-
if err != nil {
205-
scope.Set("output", []Line{Line{"type": "err", "content": err.Error()}})
206-
return
207-
}
208-
scope.Set("code", string(out))
209-
scope.Set("output", []Line{})
204+
go func() {
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+
}
213+
if err != nil {
214+
scope.Apply(func() {
215+
scope.Set("output", []Line{Line{"type": "err", "content": err.Error()}})
216+
})
217+
return
218+
}
219+
scope.Apply(func() {
220+
scope.Set("code", string(out))
221+
scope.Set("output", []Line{})
222+
})
223+
}()
210224
})
211225

212226
scope.Set("share", func() {

0 commit comments

Comments
 (0)