Skip to content

Commit 7837af4

Browse files
committed
Created Go module, moved handler code to subpackage
1 parent 38cf645 commit 7837af4

11 files changed

+48
-27
lines changed

Diff for: go.mod

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/bcmi-labs/arduino-language-server
2+
3+
go 1.12
4+
5+
require (
6+
github.com/gorilla/websocket v1.4.0 // indirect
7+
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1
8+
github.com/sourcegraph/jsonrpc2 v0.0.0-20190106185902-35a74f039c6a
9+
)

Diff for: go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
2+
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
3+
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1 h1:O1d7nVzpGmP5pGAZBSlp9TSpjNwwI0xThxhPd9oVJuU=
4+
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1/go.mod h1:tpps84QRlOVVLYk5QpKYX8Tr289D1v/UTWDLqeguiqM=
5+
github.com/sourcegraph/jsonrpc2 v0.0.0-20190106185902-35a74f039c6a h1:jTZwOlrDhmk4Ez2vhWh7kA0eKUahp1lCO2uyM4fi/Qk=
6+
github.com/sourcegraph/jsonrpc2 v0.0.0-20190106185902-35a74f039c6a/go.mod h1:eESpbCslcLDs8j2D7IEdGVgul7xuk9odqDTaor30IUU=

Diff for: builder.go renamed to handler/builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handler
22

33
import (
44
"bufio"

Diff for: handler.go renamed to handler/handler.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package main
1+
package handler
22

33
import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"io"
78
"log"
89
"net/url"
910
"path/filepath"
@@ -13,16 +14,29 @@ import (
1314
"github.com/sourcegraph/jsonrpc2"
1415
)
1516

16-
func newInoHandler() *InoHandler {
17-
return &InoHandler{
17+
var enableLogging bool
18+
19+
// NewInoHandler creates and configures an InoHandler.
20+
func NewInoHandler(stdin io.ReadCloser, stdout io.WriteCloser, stdinLog, stdoutLog io.Writer,
21+
clangdIn io.ReadCloser, clangdOut io.WriteCloser, clangdinLog, clangdoutLog io.Writer,
22+
logging bool) *InoHandler {
23+
enableLogging = logging
24+
handler := &InoHandler{
1825
data: make(map[lsp.DocumentURI]*FileData),
1926
}
27+
clangdStream := jsonrpc2.NewBufferedStream(StreamReadWrite{clangdIn, clangdOut, clangdinLog, clangdoutLog}, jsonrpc2.VSCodeObjectCodec{})
28+
clangdHandler := jsonrpc2.HandlerWithError(handler.FromClangd)
29+
handler.ClangdConn = jsonrpc2.NewConn(context.Background(), clangdStream, clangdHandler)
30+
stdStream := jsonrpc2.NewBufferedStream(StreamReadWrite{stdin, stdout, stdinLog, stdoutLog}, jsonrpc2.VSCodeObjectCodec{})
31+
stdHandler := jsonrpc2.HandlerWithError(handler.FromStdio)
32+
handler.StdioConn = jsonrpc2.NewConn(context.Background(), stdStream, stdHandler)
33+
return handler
2034
}
2135

2236
// InoHandler is a JSON-RPC handler that delegates messages to clangd.
2337
type InoHandler struct {
24-
stdioConn *jsonrpc2.Conn
25-
clangdConn *jsonrpc2.Conn
38+
StdioConn *jsonrpc2.Conn
39+
ClangdConn *jsonrpc2.Conn
2640
data map[lsp.DocumentURI]*FileData
2741
}
2842

@@ -44,9 +58,9 @@ func (handler *InoHandler) FromStdio(ctx context.Context, conn *jsonrpc2.Conn, r
4458
}
4559
var result interface{}
4660
if req.Notif {
47-
err = handler.clangdConn.Notify(ctx, req.Method, params)
61+
err = handler.ClangdConn.Notify(ctx, req.Method, params)
4862
} else {
49-
result, err = sendRequest(ctx, handler.clangdConn, req.Method, params)
63+
result, err = sendRequest(ctx, handler.ClangdConn, req.Method, params)
5064
}
5165
if err != nil {
5266
log.Println("From stdio: Method:", req.Method, "Params:", params, "Error:", err)
@@ -304,9 +318,9 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
304318
}
305319
var result interface{}
306320
if req.Notif {
307-
err = handler.stdioConn.Notify(ctx, req.Method, params)
321+
err = handler.StdioConn.Notify(ctx, req.Method, params)
308322
} else {
309-
result, err = sendRequest(ctx, handler.stdioConn, req.Method, params)
323+
result, err = sendRequest(ctx, handler.StdioConn, req.Method, params)
310324
}
311325
if err != nil {
312326
log.Println("From clangd: Method:", req.Method, "Params:", params, "Error:", err)

Diff for: properties.go renamed to handler/properties.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handler
22

33
import (
44
"bufio"

Diff for: properties_test.go renamed to handler/properties_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handler
22

33
import (
44
"reflect"

Diff for: protocol.go renamed to handler/protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handler
22

33
import (
44
"context"

Diff for: sourcemap.go renamed to handler/sourcemap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handler
22

33
import (
44
"bufio"

Diff for: sourcemap_test.go renamed to handler/sourcemap_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handler
22

33
import (
44
"reflect"

Diff for: stream.go renamed to handler/stream.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handler
22

33
import (
44
"io"

Diff for: main.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"flag"
65
"io"
76
"log"
@@ -10,7 +9,7 @@ import (
109
"os/user"
1110
"strings"
1211

13-
"github.com/sourcegraph/jsonrpc2"
12+
"github.com/bcmi-labs/arduino-language-server/handler"
1413
)
1514

1615
var enableLogging bool
@@ -41,15 +40,8 @@ func main() {
4140
go io.Copy(clangderrLog, clangdErr)
4241
}
4342

44-
inoHandler := newInoHandler()
45-
clangdStream := jsonrpc2.NewBufferedStream(StreamReadWrite{clangdIn, clangdOut, clangdinLog, clangdoutLog}, jsonrpc2.VSCodeObjectCodec{})
46-
clangdHandler := jsonrpc2.HandlerWithError(inoHandler.FromClangd)
47-
inoHandler.clangdConn = jsonrpc2.NewConn(context.Background(), clangdStream, clangdHandler)
48-
stdStream := jsonrpc2.NewBufferedStream(StreamReadWrite{os.Stdin, os.Stdout, stdinLog, stdoutLog}, jsonrpc2.VSCodeObjectCodec{})
49-
stdHandler := jsonrpc2.HandlerWithError(inoHandler.FromStdio)
50-
inoHandler.stdioConn = jsonrpc2.NewConn(context.Background(), stdStream, stdHandler)
51-
52-
<-inoHandler.stdioConn.DisconnectNotify()
43+
inoHandler := handler.NewInoHandler(os.Stdin, os.Stdout, stdinLog, stdoutLog, clangdIn, clangdOut, clangdinLog, clangdoutLog, enableLogging)
44+
<-inoHandler.StdioConn.DisconnectNotify()
5345
}
5446

5547
func createLogFiles() (logFile, stdinLog, stdoutLog, clangdinLog, clangdoutLog, clangderrLog *os.File) {

0 commit comments

Comments
 (0)