Skip to content

Commit 482f0a7

Browse files
authored
Merge pull request #20 from x1unix/dev
Release candidate - 1.5.0
2 parents 04a2c40 + a9d36cb commit 482f0a7

33 files changed

+898
-80
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ Improved Go Playground powered by Monaco Editor and React
1111

1212
* 💡 Code autocomplete
1313
* 💾 Load and save files
14-
* 🛠 [WebAssembly](https://github.com/golang/go/wiki/WebAssembly) support (see [latest release notes](https://github.com/x1unix/go-playground/releases/tag/v1.3.0))
14+
* 📔 Snippets and tutorials
15+
* 🛠 [WebAssembly](https://github.com/golang/go/wiki/WebAssembly) support
1516
* 🌚 Dark theme
1617

1718

1819
And more
1920

2021
## Demo
2122

22-
[http://goplay.x1unix.com/](http://goplay.x1unix.com/)
23+
[http://goplay.tools/](goplay.tools)
2324

2425
## Installation
2526

@@ -40,6 +41,13 @@ $ ./playground -f ./data/packages.json -debug
4041

4142
Use `-help` to get information about command params
4243

44+
### Third-party credits
45+
46+
* Default playground run server provided by [play.golang.org](https://play.golang.org)
47+
* Code for templates and tutorials provided by [gobyexample.com](https://gobyexample.com/)
48+
* Code completion snippets were inspired by [tj/vscode-snippets](https://github.com/tj/vscode-snippets/blob/master/go.json)
49+
50+
4351
## Contributors
4452

4553
### Code Contributors

build/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ ARG GITHUB_URL=https://github.com/x1unix/go-playground
77
RUN yarn install --silent && \
88
REACT_APP_VERSION=$APP_VERSION REACT_APP_GITHUB_URL=$GITHUB_URL REACT_APP_GTAG=$APP_GTAG yarn build
99

10-
FROM golang:1.13-alpine as build
10+
FROM golang:1.14-alpine as build
1111
WORKDIR /tmp/playground
1212
COPY cmd ./cmd
1313
COPY pkg ./pkg
1414
COPY go.mod .
1515
COPY go.sum .
16-
RUN go build -o server ./cmd/playground && \
16+
RUN go build -o server -ldflags="-X 'main.Version=$APP_VERSION'" ./cmd/playground && \
1717
GOOS=js GOARCH=wasm go build -o ./worker.wasm ./cmd/webworker && \
1818
cp $(go env GOROOT)/misc/wasm/wasm_exec.js .
1919

20-
FROM golang:1.13-alpine as production
20+
FROM golang:1.14-alpine as production
2121
WORKDIR /opt/playground
2222
ENV GOROOT /usr/local/go
2323
ENV APP_CLEAN_INTERVAL=10m

cmd/playground/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"go.uber.org/zap"
1919
)
2020

21+
const Version = "testing"
22+
2123
type appArgs struct {
2224
packagesFile string
2325
addr string
@@ -75,6 +77,7 @@ func start(goRoot string, args appArgs) error {
7577
return fmt.Errorf("invalid cleanup interval parameter: %s", err)
7678
}
7779

80+
zap.S().Info("Server version: ", Version)
7881
zap.S().Infof("GOROOT is %q", goRoot)
7982
zap.S().Infof("Packages file is %q", args.packagesFile)
8083
zap.S().Infof("Cleanup interval is %s", cleanInterval.String())
@@ -94,7 +97,7 @@ func start(goRoot string, args appArgs) error {
9497
go store.StartCleaner(ctx, cleanInterval, nil)
9598

9699
r := mux.NewRouter()
97-
langserver.New(packages, compiler.NewBuildService(zap.S(), store)).
100+
langserver.New(Version, packages, compiler.NewBuildService(zap.S(), store)).
98101
Mount(r.PathPrefix("/api").Subrouter())
99102
r.PathPrefix("/").Handler(langserver.SpaFileServer("./public"))
100103

pkg/analyzer/package.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ func (p *Package) SymbolByChar(chr string) []*CompletionItem {
5050
return append(p.Functions.Match(chr), result...)
5151
}
5252

53+
func (p *Package) AllSymbols() []*CompletionItem {
54+
out := make([]*CompletionItem, 0, p.Values.Len()+p.Functions.Len())
55+
out = append(out, p.Functions.Symbols...)
56+
out = append(out, p.Values.Symbols...)
57+
return out
58+
}
59+
5360
func (p *Package) GetCompletionItem() *CompletionItem {
5461
return &CompletionItem{
5562
Label: p.Name,

pkg/analyzer/sym_index.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ type SymbolIndex struct {
66
charMap map[string][]*CompletionItem
77
}
88

9+
func (si *SymbolIndex) Len() int {
10+
return len(si.Symbols)
11+
}
12+
913
func emptySymbolIndex() SymbolIndex {
1014
return SymbolIndex{
1115
Symbols: []*CompletionItem{},

pkg/langserver/request.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func (r *ErrorResponse) Write(w http.ResponseWriter) http.ResponseWriter {
5252
return w
5353
}
5454

55+
type VersionResponse struct {
56+
Version string `json:"version"`
57+
}
58+
59+
func (r VersionResponse) Write(w http.ResponseWriter) {
60+
WriteJSON(w, r)
61+
}
62+
5563
type SuggestionsResponse struct {
5664
Suggestions []*analyzer.CompletionItem `json:"suggestions"`
5765
}

pkg/langserver/server.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ const (
2929
)
3030

3131
type Service struct {
32+
version string
3233
log *zap.SugaredLogger
3334
index analyzer.PackageIndex
3435
compiler compiler.BuildService
3536
limiter *rate.Limiter
3637
}
3738

38-
func New(packages []*analyzer.Package, builder compiler.BuildService) *Service {
39+
func New(version string, packages []*analyzer.Package, builder compiler.BuildService) *Service {
3940
return &Service{
4041
compiler: builder,
42+
version: version,
4143
log: zap.S().Named("langserver"),
4244
index: analyzer.BuildPackageIndex(packages),
4345
limiter: rate.NewLimiter(rate.Every(frameTime), compileRequestsPerFrame),
@@ -46,6 +48,8 @@ func New(packages []*analyzer.Package, builder compiler.BuildService) *Service {
4648

4749
// Mount mounts service on route
4850
func (s *Service) Mount(r *mux.Router) {
51+
r.Path("/version").
52+
HandlerFunc(WrapHandler(s.HandleGetVersion))
4953
r.Path("/suggest").
5054
HandlerFunc(WrapHandler(s.HandleGetSuggestion))
5155
r.Path("/run").Methods(http.MethodPost).
@@ -83,10 +87,6 @@ func (s *Service) lookupBuiltin(val string) (*SuggestionsResponse, error) {
8387
}
8488

8589
func (s *Service) provideSuggestion(req SuggestionRequest) (*SuggestionsResponse, error) {
86-
if req.Value == "" {
87-
return nil, fmt.Errorf("empty suggestion request value, nothing to provide")
88-
}
89-
9090
// Provide package suggestions (if requested)
9191
if req.PackageName != "" {
9292
pkg, ok := s.index.PackageByName(req.PackageName)
@@ -98,14 +98,30 @@ func (s *Service) provideSuggestion(req SuggestionRequest) (*SuggestionsResponse
9898
return nil, fmt.Errorf("failed to analyze package %q: %s", req.PackageName, err)
9999
}
100100

101+
var symbols []*analyzer.CompletionItem
102+
if req.Value != "" {
103+
symbols = pkg.SymbolByChar(req.Value)
104+
} else {
105+
symbols = pkg.AllSymbols()
106+
}
107+
101108
return &SuggestionsResponse{
102-
Suggestions: pkg.SymbolByChar(req.Value),
109+
Suggestions: symbols,
103110
}, nil
104111
}
105112

113+
if req.Value == "" {
114+
return nil, fmt.Errorf("empty suggestion request value, nothing to provide")
115+
}
116+
106117
return s.lookupBuiltin(req.Value)
107118
}
108119

120+
func (s *Service) HandleGetVersion(w http.ResponseWriter, r *http.Request) error {
121+
WriteJSON(w, VersionResponse{Version: s.version})
122+
return nil
123+
}
124+
109125
func (s *Service) HandleGetSuggestion(w http.ResponseWriter, r *http.Request) error {
110126
q := r.URL.Query()
111127
value := q.Get("value")

web/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"circular-dependency-plugin": "^5.2.0",
1111
"connected-react-router": "^6.6.1",
1212
"file-saver": "^2.0.2",
13-
"monaco-editor": "^0.19.3",
14-
"monaco-editor-webpack-plugin": "^1.8.2",
13+
"monaco-editor": "^0.20.0",
14+
"monaco-editor-webpack-plugin": "^1.9.0",
1515
"office-ui-fabric-react": "^7.82.1",
1616
"react": "^16.12.0",
1717
"react-app-rewired": "^2.1.5",
1818
"react-dom": "^16.12.0",
19-
"react-monaco-editor": "^0.33.0",
19+
"react-monaco-editor": "^0.39.1",
2020
"react-redux": "^7.1.3",
2121
"react-router-dom": "^5.1.2",
2222
"react-scripts": "3.3.0",
@@ -26,8 +26,8 @@
2626
"uuid": "^3.4.0"
2727
},
2828
"scripts": {
29-
"start": "react-app-rewired start",
30-
"build": "react-app-rewired build",
29+
"start": "GENERATE_SOURCEMAP=true react-app-rewired start",
30+
"build": "GENERATE_SOURCEMAP=false react-app-rewired build",
3131
"test": "react-app-rewired test",
3232
"eject": "react-app-rewired eject",
3333
"gen:suggestions": "node ./src/editor/internal/genpkgcache.js"

web/public/fonts/CascadiaCode.ttf

350 KB
Binary file not shown.

web/public/fonts/FiraCode-Bold.ttf

317 KB
Binary file not shown.

0 commit comments

Comments
 (0)