This repository was archived by the owner on Mar 27, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
172 lines (150 loc) · 4.16 KB
/
Copy pathmain.go
File metadata and controls
172 lines (150 loc) · 4.16 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"os"
"github.com/sourcegraph/go-langserver/pkg/lsp"
"github.com/sourcegraph/jsonrpc2"
)
var (
mode = flag.String("mode", "stdio", "communication mode (stdio|tcp)")
addr = flag.String("addr", ":2088", "server listen address (tcp)")
logfile = flag.String("log", "/tmp/sample_server.log", "write log output to this file (and stderr)")
)
func main() {
flag.Parse()
log.SetFlags(0)
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
l := log.New(os.Stderr, "", 0)
if *logfile != "" {
f, err := os.Create(*logfile)
if err != nil {
return err
}
defer f.Close()
l.SetOutput(f)
log.SetOutput(f)
}
h := jsonrpc2.HandlerWithError(handle)
switch *mode {
case "tcp":
lis, err := net.Listen("tcp", *addr)
if err != nil {
return err
}
defer lis.Close()
l.Println("listening on", *addr)
return jsonrpc2.Serve(context.Background(), lis, h, jsonrpc2.LogMessages(l))
case "stdio":
l.Println("reading on stdin, writing on stdout")
<-jsonrpc2.NewConn(context.Background(), stdrwc{}, h, jsonrpc2.LogMessages(l)).DisconnectNotify()
l.Println("connection closed")
return nil
default:
return fmt.Errorf("invalid mode %q", *mode)
}
}
type stdrwc struct{}
func (v stdrwc) Read(p []byte) (int, error) {
return os.Stdin.Read(p)
}
func (v 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 handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) {
switch req.Method {
case "initialize":
return lsp.InitializeResult{
Capabilities: lsp.ServerCapabilities{
HoverProvider: true,
DefinitionProvider: true,
ReferencesProvider: true,
},
}, nil
case "shutdown":
// Result is undefined, per
// https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#shutdown-request.
return nil, nil
case "textDocument/hover":
var params lsp.TextDocumentPositionParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
pos := params.Position
return lsp.Hover{
Contents: []lsp.MarkedString{{Language: "markdown", Value: "Hello over LSP!"}},
Range: lsp.Range{
Start: lsp.Position{Line: pos.Line, Character: pos.Character - 3},
End: lsp.Position{Line: pos.Line, Character: pos.Character + 3},
},
}, nil
case "textDocument/definition":
var params lsp.TextDocumentPositionParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
return lsp.Location{
URI: params.TextDocument.URI,
Range: lsp.Range{
Start: lsp.Position{Line: 0, Character: 0},
End: lsp.Position{Line: 0, Character: 0},
},
}, nil
case "textDocument/references":
var params lsp.ReferenceParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
return []lsp.Location{lsp.Location{
URI: params.TextDocument.URI,
Range: lsp.Range{
Start: lsp.Position{Line: 0, Character: 0},
End: lsp.Position{Line: 0, Character: 3},
},
}, lsp.Location{
URI: params.TextDocument.URI,
Range: lsp.Range{
Start: lsp.Position{Line: 0, Character: 4},
End: lsp.Position{Line: 0, Character: 6},
},
}}, nil
case "workspace/symbol":
var params lsp.WorkspaceSymbolParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
return []lsp.SymbolInformation{{
Name: "fooFunc",
Kind: lsp.SKFunction,
Location: lsp.Location{
URI: "file:///some-file",
Range: lsp.Range{Start: lsp.Position{Line: 0, Character: 0}, End: lsp.Position{Line: 0, Character: 3}},
},
ContainerName: "pkg",
}, {
Name: "BarClass",
Kind: lsp.SKClass,
Location: lsp.Location{
URI: "file:///another-file",
Range: lsp.Range{Start: lsp.Position{Line: 1, Character: 2}, End: lsp.Position{Line: 1, Character: 4}},
},
ContainerName: "pkg",
}}, nil
}
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeMethodNotFound, Message: fmt.Sprintf("method not supported: %s", req.Method)}
}