-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (117 loc) · 3.15 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
// This is a launcher for a generic zcl language server, which can work with
// basic zcl syntax but doesn't have any knowledge of which blocks and
// attributes are valid for a particular application.
//
// This can be used directly if desired, particularly for simple applications,
// but non-trivial applications ought to provide their own version of this
// which configures the underlying library with application-specific
// information.
//
// This is based on sourcegraph's language server for Go.
import (
"context"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"runtime/debug"
"time"
"github.com/sourcegraph/jsonrpc2"
"github.com/zclconf/zcl-language-server/zclsrv"
_ "net/http/pprof"
)
var (
mode = flag.String("mode", "stdio", "communication mode (stdio|tcp)")
addr = flag.String("addr", ":4389", "server listen address (tcp)")
trace = flag.Bool("trace", false, "print all requests and responses")
logfile = flag.String("logfile", "", "also log to this file (in addition to stderr)")
printVersion = flag.Bool("version", false, "print version and exit")
pprof = flag.String("pprof", ":6060", "start a pprof http server (https://golang.org/pkg/net/http/pprof/)")
freeosmemory = flag.Bool("freeosmemory", true, "aggressively free memory back to the OS")
)
const version = "v0-dev"
func main() {
flag.Parse()
log.SetFlags(0)
// Start pprof server, if desired.
if *pprof != "" {
go func() {
log.Println(http.ListenAndServe(*pprof, nil))
}()
}
if *freeosmemory {
go freeOSMemory()
}
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
if *printVersion {
fmt.Println(version)
return nil
}
var logW io.Writer
if *logfile == "" {
logW = os.Stderr
} else {
f, err := os.Create(*logfile)
if err != nil {
return err
}
defer f.Close()
logW = io.MultiWriter(os.Stderr, f)
}
log.SetOutput(logW)
var connOpt []jsonrpc2.ConnOpt
if *trace {
connOpt = append(connOpt, jsonrpc2.LogMessages(log.New(logW, "", 0)))
}
switch *mode {
case "tcp":
lis, err := net.Listen("tcp", *addr)
if err != nil {
return err
}
defer lis.Close()
log.Println("zcl-language-server: listening on", *addr)
for {
conn, err := lis.Accept()
if err != nil {
return err
}
jsonrpc2.NewConn(context.Background(), jsonrpc2.NewBufferedStream(conn, jsonrpc2.VSCodeObjectCodec{}), zclsrv.NewHandler(), connOpt...)
}
case "stdio":
log.Println("zcl-language-server: reading on stdin, writing on stdout")
<-jsonrpc2.NewConn(context.Background(), jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}), zclsrv.NewHandler(), connOpt...).DisconnectNotify()
log.Println("connection closed")
return nil
default:
return fmt.Errorf("invalid mode %q", *mode)
}
}
type stdrwc struct{}
func (stdrwc) Read(p []byte) (int, error) {
return os.Stdin.Read(p)
}
func (stdrwc) Write(p []byte) (int, error) {
return os.Stdout.Write(p)
}
func (stdrwc) Close() error {
if err := os.Stdin.Close(); err != nil {
return err
}
return os.Stdout.Close()
}
func freeOSMemory() {
for {
time.Sleep(1 * time.Second)
debug.FreeOSMemory()
}
}