-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
72 lines (57 loc) · 1.54 KB
/
main.go
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
package main
import (
"context"
"io/ioutil"
"log"
"net"
"os"
"github.com/aca/neuron-language-server/neuron"
"github.com/sourcegraph/go-lsp"
"github.com/sourcegraph/jsonrpc2"
)
type DebugLogger struct {
conn net.Conn
}
func main() {
var connOpt []jsonrpc2.ConnOpt
// Setup a debug logger,
// You can send a log to client, but each clients implement different way to see / debug log.
// So we just use simple trick to debug server.
//
// $ nc -lp 3000
// Use netcat on the terminal like this, and this server would write log to it.
logConn, err := net.Dial("tcp", "localhost:3000")
var logger *log.Logger
if err == nil {
logger = log.New(logConn, "", log.LstdFlags|log.Lshortfile)
} else {
logger = log.New(ioutil.Discard, "", log.LstdFlags)
}
connOpt = append(connOpt, jsonrpc2.LogMessages(logger))
s := &server{
logger: logger,
documents: make(map[lsp.DocumentURI]string),
neuronMeta: make(map[string]neuron.Result),
diagnosticChan: make(chan lsp.DocumentURI),
}
s.logger.Print("start")
handler := jsonrpc2.HandlerWithError(s.handle)
<-jsonrpc2.NewConn(
context.Background(),
jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}),
handler, connOpt...).DisconnectNotify()
s.logger.Print("shutdown")
}
type stdrwc struct{}
func (stdrwc) Read(p []byte) (int, error) {
return os.Stdin.Read(p)
}
func (c stdrwc) Write(p []byte) (int, error) {
return os.Stdout.Write(p)
}
func (c stdrwc) Close() error {
if err := os.Stdin.Close(); err != nil {
return err
}
return os.Stdout.Close()
}