forked from golang/tools
/
cmd.go
372 lines (334 loc) · 10.1 KB
/
cmd.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package cmd handles the gopls command line.
// It contains a handler for each of the modes, along with all the flag handling
// and the command line output format.
package cmd
import (
"context"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"net"
"os"
"strings"
"sync"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/lsp"
"golang.org/x/tools/internal/lsp/cache"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/tool"
)
// Application is the main application as passed to tool.Main
// It handles the main command line parsing and dispatch to the sub commands.
type Application struct {
// Core application flags
// Embed the basic profiling flags supported by the tool package
tool.Profile
// We include the server configuration directly for now, so the flags work
// even without the verb.
// TODO: Remove this when we stop allowing the serve verb by default.
Serve Serve
// An initial, common go/packages configuration
Config packages.Config
// The base cache to use for sessions from this application.
Cache source.Cache
// Support for remote lsp server
Remote string `flag:"remote" help:"*EXPERIMENTAL* - forward all commands to a remote lsp"`
// Enable verbose logging
Verbose bool `flag:"v" help:"Verbose output"`
}
// Returns a new Application ready to run.
func New(config *packages.Config) *Application {
app := &Application{
Cache: cache.New(),
}
if config != nil {
app.Config = *config
}
return app
}
// Name implements tool.Application returning the binary name.
func (app *Application) Name() string { return "gopls" }
// Usage implements tool.Application returning empty extra argument usage.
func (app *Application) Usage() string { return "<command> [command-flags] [command-args]" }
// ShortHelp implements tool.Application returning the main binary help.
func (app *Application) ShortHelp() string {
return "The Go Language source tools."
}
// DetailedHelp implements tool.Application returning the main binary help.
// This includes the short help for all the sub commands.
func (app *Application) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), `
Available commands are:
`)
for _, c := range app.commands() {
fmt.Fprintf(f.Output(), " %s : %v\n", c.Name(), c.ShortHelp())
}
fmt.Fprint(f.Output(), `
gopls flags are:
`)
f.PrintDefaults()
}
// Run takes the args after top level flag processing, and invokes the correct
// sub command as specified by the first argument.
// If no arguments are passed it will invoke the server sub command, as a
// temporary measure for compatibility.
func (app *Application) Run(ctx context.Context, args ...string) error {
app.Serve.app = app
if len(args) == 0 {
tool.Main(ctx, &app.Serve, args)
return nil
}
if app.Config.Dir == "" {
if wd, err := os.Getwd(); err == nil {
app.Config.Dir = wd
}
}
app.Config.Mode = packages.LoadSyntax
app.Config.Tests = true
if app.Config.Fset == nil {
app.Config.Fset = token.NewFileSet()
}
app.Config.Context = ctx
app.Config.ParseFile = func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
return parser.ParseFile(fset, filename, src, parser.AllErrors|parser.ParseComments)
}
command, args := args[0], args[1:]
for _, c := range app.commands() {
if c.Name() == command {
tool.Main(ctx, c, args)
return nil
}
}
return tool.CommandLineErrorf("Unknown command %v", command)
}
// commands returns the set of commands supported by the gopls tool on the
// command line.
// The command is specified by the first non flag argument.
func (app *Application) commands() []tool.Application {
return []tool.Application{
&app.Serve,
&bug{},
&check{app: app},
&format{app: app},
&query{app: app},
&version{app: app},
}
}
var (
internalMu sync.Mutex
internalConnections = make(map[string]*connection)
)
func (app *Application) connect(ctx context.Context) (*connection, error) {
switch app.Remote {
case "":
connection := newConnection(app)
connection.Server = lsp.NewClientServer(app.Cache, connection.Client)
return connection, connection.initialize(ctx)
case "internal":
internalMu.Lock()
defer internalMu.Unlock()
if c := internalConnections[app.Config.Dir]; c != nil {
return c, nil
}
connection := newConnection(app)
ctx := context.Background() //TODO:a way of shutting down the internal server
cr, sw, _ := os.Pipe()
sr, cw, _ := os.Pipe()
var jc *jsonrpc2.Conn
jc, connection.Server, _ = protocol.NewClient(jsonrpc2.NewHeaderStream(cr, cw), connection.Client)
go jc.Run(ctx)
go lsp.NewServer(app.Cache, jsonrpc2.NewHeaderStream(sr, sw)).Run(ctx)
if err := connection.initialize(ctx); err != nil {
return nil, err
}
internalConnections[app.Config.Dir] = connection
return connection, nil
default:
connection := newConnection(app)
conn, err := net.Dial("tcp", app.Remote)
if err != nil {
return nil, err
}
stream := jsonrpc2.NewHeaderStream(conn, conn)
var jc *jsonrpc2.Conn
jc, connection.Server, _ = protocol.NewClient(stream, connection.Client)
go jc.Run(ctx)
return connection, connection.initialize(ctx)
}
}
func (c *connection) initialize(ctx context.Context) error {
params := &protocol.InitializeParams{}
params.RootURI = string(span.FileURI(c.Client.app.Config.Dir))
params.Capabilities.Workspace.Configuration = true
params.Capabilities.TextDocument.Hover.ContentFormat = []protocol.MarkupKind{protocol.PlainText}
if _, err := c.Server.Initialize(ctx, params); err != nil {
return err
}
if err := c.Server.Initialized(ctx, &protocol.InitializedParams{}); err != nil {
return err
}
return nil
}
type connection struct {
protocol.Server
Client *cmdClient
}
type cmdClient struct {
protocol.Server
app *Application
fset *token.FileSet
filesMu sync.Mutex
files map[span.URI]*cmdFile
}
type cmdFile struct {
uri span.URI
mapper *protocol.ColumnMapper
err error
added bool
hasDiagnostics chan struct{}
diagnostics []protocol.Diagnostic
}
func newConnection(app *Application) *connection {
return &connection{
Client: &cmdClient{
app: app,
fset: token.NewFileSet(),
files: make(map[span.URI]*cmdFile),
},
}
}
func (c *cmdClient) ShowMessage(ctx context.Context, p *protocol.ShowMessageParams) error { return nil }
func (c *cmdClient) ShowMessageRequest(ctx context.Context, p *protocol.ShowMessageRequestParams) (*protocol.MessageActionItem, error) {
return nil, nil
}
func (c *cmdClient) LogMessage(ctx context.Context, p *protocol.LogMessageParams) error {
switch p.Type {
case protocol.Error:
log.Print("Error:", p.Message)
case protocol.Warning:
log.Print("Warning:", p.Message)
case protocol.Info:
if c.app.Verbose {
log.Print("Info:", p.Message)
}
case protocol.Log:
if c.app.Verbose {
log.Print("Log:", p.Message)
}
default:
if c.app.Verbose {
log.Print(p.Message)
}
}
return nil
}
func (c *cmdClient) Event(ctx context.Context, t *interface{}) error { return nil }
func (c *cmdClient) RegisterCapability(ctx context.Context, p *protocol.RegistrationParams) error {
return nil
}
func (c *cmdClient) UnregisterCapability(ctx context.Context, p *protocol.UnregistrationParams) error {
return nil
}
func (c *cmdClient) WorkspaceFolders(ctx context.Context) ([]protocol.WorkspaceFolder, error) {
return nil, nil
}
func (c *cmdClient) Configuration(ctx context.Context, p *protocol.ConfigurationParams) ([]interface{}, error) {
results := make([]interface{}, len(p.Items))
for i, item := range p.Items {
if item.Section != "gopls" {
continue
}
env := map[string]interface{}{}
for _, value := range c.app.Config.Env {
l := strings.SplitN(value, "=", 2)
if len(l) != 2 {
continue
}
env[l[0]] = l[1]
}
results[i] = map[string]interface{}{
"env": env,
"noDocsOnHover": true,
}
}
return results, nil
}
func (c *cmdClient) ApplyEdit(ctx context.Context, p *protocol.ApplyWorkspaceEditParams) (*protocol.ApplyWorkspaceEditResponse, error) {
return &protocol.ApplyWorkspaceEditResponse{Applied: false, FailureReason: "not implemented"}, nil
}
func (c *cmdClient) PublishDiagnostics(ctx context.Context, p *protocol.PublishDiagnosticsParams) error {
c.filesMu.Lock()
defer c.filesMu.Unlock()
uri := span.URI(p.URI)
file := c.getFile(ctx, uri)
hadDiagnostics := file.diagnostics != nil
file.diagnostics = p.Diagnostics
if file.diagnostics == nil {
file.diagnostics = []protocol.Diagnostic{}
}
if !hadDiagnostics {
close(file.hasDiagnostics)
}
return nil
}
func (c *cmdClient) getFile(ctx context.Context, uri span.URI) *cmdFile {
file, found := c.files[uri]
if !found {
file = &cmdFile{
uri: uri,
hasDiagnostics: make(chan struct{}),
}
c.files[uri] = file
}
if file.mapper == nil {
fname, err := uri.Filename()
if err != nil {
file.err = fmt.Errorf("%v: %v", uri, err)
return file
}
content, err := ioutil.ReadFile(fname)
if err != nil {
file.err = fmt.Errorf("%v: %v", uri, err)
return file
}
f := c.fset.AddFile(fname, -1, len(content))
f.SetLinesForContent(content)
file.mapper = protocol.NewColumnMapper(uri, fname, c.fset, f, content)
}
return file
}
func (c *connection) AddFile(ctx context.Context, uri span.URI) *cmdFile {
c.Client.filesMu.Lock()
defer c.Client.filesMu.Unlock()
file := c.Client.getFile(ctx, uri)
if !file.added {
file.added = true
p := &protocol.DidOpenTextDocumentParams{}
p.TextDocument.URI = string(uri)
p.TextDocument.Text = string(file.mapper.Content)
if err := c.Server.DidOpen(ctx, p); err != nil {
file.err = fmt.Errorf("%v: %v", uri, err)
}
}
return file
}
func (c *connection) terminate(ctx context.Context) {
if c.Client.app.Remote == "internal" {
// internal connections need to be left alive for the next test
return
}
//TODO: do we need to handle errors on these calls?
c.Shutdown(ctx)
//TODO: right now calling exit terminates the process, we should rethink that
//server.Exit(ctx)
}