2018-12-17 11:52:58 -07:00
|
|
|
// 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.
|
|
|
|
|
2019-01-16 15:01:06 -07:00
|
|
|
// Package cmd handles the gopls command line.
|
2018-12-17 11:52:58 -07:00
|
|
|
// 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"
|
2019-02-08 15:55:06 -07:00
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
2018-12-14 13:46:12 -07:00
|
|
|
"go/token"
|
2019-04-01 18:08:14 -06:00
|
|
|
"io/ioutil"
|
2019-03-29 17:04:29 -06:00
|
|
|
"log"
|
2019-02-08 14:16:57 -07:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2019-02-05 15:27:35 -07:00
|
|
|
|
2018-12-14 13:46:12 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-02-08 14:16:57 -07:00
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/span"
|
2018-12-17 11:52:58 -07:00
|
|
|
"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 {
|
2018-12-14 13:46:12 -07:00
|
|
|
// Core application flags
|
|
|
|
|
2018-12-17 11:52:58 -07:00
|
|
|
// Embed the basic profiling flags supported by the tool package
|
|
|
|
tool.Profile
|
|
|
|
|
2019-02-07 15:05:20 -07:00
|
|
|
// 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
|
2018-12-14 13:46:12 -07:00
|
|
|
|
|
|
|
// An initial, common go/packages configuration
|
|
|
|
Config packages.Config
|
2019-02-07 15:05:20 -07:00
|
|
|
|
|
|
|
// Support for remote lsp server
|
|
|
|
Remote string `flag:"remote" help:"*EXPERIMENTAL* - forward all commands to a remote lsp"`
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements tool.Application returning the binary name.
|
2019-01-16 15:01:06 -07:00
|
|
|
func (app *Application) Name() string { return "gopls" }
|
2018-12-17 11:52:58 -07:00
|
|
|
|
|
|
|
// Usage implements tool.Application returning empty extra argument usage.
|
2018-12-14 13:46:12 -07:00
|
|
|
func (app *Application) Usage() string { return "<command> [command-flags] [command-args]" }
|
2018-12-17 11:52:58 -07:00
|
|
|
|
|
|
|
// ShortHelp implements tool.Application returning the main binary help.
|
|
|
|
func (app *Application) ShortHelp() string {
|
2018-12-14 13:46:12 -07:00
|
|
|
return "The Go Language source tools."
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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(), `
|
2018-12-14 13:46:12 -07:00
|
|
|
Available commands are:
|
2018-12-17 11:52:58 -07:00
|
|
|
`)
|
2018-12-14 13:46:12 -07:00
|
|
|
for _, c := range app.commands() {
|
2018-12-17 11:52:58 -07:00
|
|
|
fmt.Fprintf(f.Output(), " %s : %v\n", c.Name(), c.ShortHelp())
|
|
|
|
}
|
|
|
|
fmt.Fprint(f.Output(), `
|
2019-01-16 15:01:06 -07:00
|
|
|
gopls flags are:
|
2018-12-17 11:52:58 -07:00
|
|
|
`)
|
|
|
|
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
|
2019-01-05 12:20:03 -07:00
|
|
|
// temporary measure for compatibility.
|
2018-12-17 11:52:58 -07:00
|
|
|
func (app *Application) Run(ctx context.Context, args ...string) error {
|
2019-02-12 12:23:47 -07:00
|
|
|
app.Serve.app = app
|
2018-12-17 11:52:58 -07:00
|
|
|
if len(args) == 0 {
|
2019-02-07 15:05:20 -07:00
|
|
|
tool.Main(ctx, &app.Serve, args)
|
2018-12-17 11:52:58 -07:00
|
|
|
return nil
|
|
|
|
}
|
2019-02-08 14:16:57 -07:00
|
|
|
if app.Config.Dir == "" {
|
|
|
|
if wd, err := os.Getwd(); err == nil {
|
|
|
|
app.Config.Dir = wd
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
app.Config.Mode = packages.LoadSyntax
|
|
|
|
app.Config.Tests = true
|
|
|
|
if app.Config.Fset == nil {
|
|
|
|
app.Config.Fset = token.NewFileSet()
|
|
|
|
}
|
2019-02-08 15:55:06 -07:00
|
|
|
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)
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
command, args := args[0], args[1:]
|
|
|
|
for _, c := range app.commands() {
|
|
|
|
if c.Name() == command {
|
|
|
|
tool.Main(ctx, c, args)
|
2018-12-17 11:52:58 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
return tool.CommandLineErrorf("Unknown command %v", command)
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
|
2018-12-14 13:46:12 -07:00
|
|
|
// commands returns the set of commands supported by the gopls tool on the
|
2018-12-17 11:52:58 -07:00
|
|
|
// command line.
|
2018-12-14 13:46:12 -07:00
|
|
|
// The command is specified by the first non flag argument.
|
|
|
|
func (app *Application) commands() []tool.Application {
|
2018-12-17 11:52:58 -07:00
|
|
|
return []tool.Application{
|
2019-02-07 15:05:20 -07:00
|
|
|
&app.Serve,
|
2019-02-08 14:16:57 -07:00
|
|
|
&check{app: app},
|
2019-04-07 21:08:47 -06:00
|
|
|
&format{app: app},
|
|
|
|
&query{app: app},
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 18:08:14 -06:00
|
|
|
type cmdClient interface {
|
|
|
|
protocol.Client
|
|
|
|
|
|
|
|
prepare(app *Application, server protocol.Server)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Application) connect(ctx context.Context, client cmdClient) (protocol.Server, error) {
|
2019-02-08 14:16:57 -07:00
|
|
|
var server protocol.Server
|
2019-03-28 15:17:40 -06:00
|
|
|
switch app.Remote {
|
|
|
|
case "":
|
2019-03-28 19:06:01 -06:00
|
|
|
server = lsp.NewClientServer(client)
|
2019-03-28 15:17:40 -06:00
|
|
|
case "internal":
|
|
|
|
cr, sw, _ := os.Pipe()
|
|
|
|
sr, cw, _ := os.Pipe()
|
2019-03-28 19:06:01 -06:00
|
|
|
var jc *jsonrpc2.Conn
|
2019-03-29 17:04:29 -06:00
|
|
|
jc, server, _ = protocol.NewClient(jsonrpc2.NewHeaderStream(cr, cw), client)
|
2019-03-28 19:06:01 -06:00
|
|
|
go jc.Run(ctx)
|
|
|
|
go lsp.NewServer(jsonrpc2.NewHeaderStream(sr, sw)).Run(ctx)
|
2019-03-28 15:17:40 -06:00
|
|
|
default:
|
2019-02-08 14:16:57 -07:00
|
|
|
conn, err := net.Dial("tcp", app.Remote)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stream := jsonrpc2.NewHeaderStream(conn, conn)
|
2019-03-28 19:06:01 -06:00
|
|
|
var jc *jsonrpc2.Conn
|
2019-03-29 17:04:29 -06:00
|
|
|
jc, server, _ = protocol.NewClient(stream, client)
|
2019-03-28 19:06:01 -06:00
|
|
|
go jc.Run(ctx)
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
2019-04-17 12:47:10 -06:00
|
|
|
|
2019-02-08 14:16:57 -07:00
|
|
|
params := &protocol.InitializeParams{}
|
|
|
|
params.RootURI = string(span.FileURI(app.Config.Dir))
|
2019-04-08 07:16:48 -06:00
|
|
|
params.Capabilities.Workspace.Configuration = true
|
2019-04-17 12:47:10 -06:00
|
|
|
params.Capabilities.TextDocument.Hover.ContentFormat = []protocol.MarkupKind{protocol.PlainText}
|
|
|
|
|
2019-04-01 18:08:14 -06:00
|
|
|
client.prepare(app, server)
|
2019-02-08 14:16:57 -07:00
|
|
|
if _, err := server.Initialize(ctx, params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := server.Initialized(ctx, &protocol.InitializedParams{}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseClient struct {
|
|
|
|
protocol.Server
|
2019-04-01 18:08:14 -06:00
|
|
|
app *Application
|
|
|
|
server protocol.Server
|
|
|
|
fset *token.FileSet
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) ShowMessage(ctx context.Context, p *protocol.ShowMessageParams) error { return nil }
|
|
|
|
func (c *baseClient) ShowMessageRequest(ctx context.Context, p *protocol.ShowMessageRequestParams) (*protocol.MessageActionItem, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-03-29 17:04:29 -06:00
|
|
|
func (c *baseClient) 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:
|
|
|
|
log.Print("Info:", p.Message)
|
|
|
|
case protocol.Log:
|
|
|
|
log.Print("Log:", p.Message)
|
|
|
|
default:
|
|
|
|
log.Print(p.Message)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-23 08:05:23 -06:00
|
|
|
func (c *baseClient) Event(ctx context.Context, t *interface{}) error { return nil }
|
2019-02-08 14:16:57 -07:00
|
|
|
func (c *baseClient) RegisterCapability(ctx context.Context, p *protocol.RegistrationParams) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (c *baseClient) UnregisterCapability(ctx context.Context, p *protocol.UnregistrationParams) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (c *baseClient) WorkspaceFolders(ctx context.Context) ([]protocol.WorkspaceFolder, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (c *baseClient) 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}
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
2019-02-08 14:16:57 -07:00
|
|
|
return results, nil
|
|
|
|
}
|
2019-04-23 08:05:23 -06:00
|
|
|
func (c *baseClient) ApplyEdit(ctx context.Context, p *protocol.ApplyWorkspaceEditParams) (*protocol.ApplyWorkspaceEditResponse, error) {
|
|
|
|
return &protocol.ApplyWorkspaceEditResponse{Applied: false, FailureReason: "not implemented"}, nil
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
|
|
|
func (c *baseClient) PublishDiagnostics(ctx context.Context, p *protocol.PublishDiagnosticsParams) error {
|
|
|
|
return nil
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
2019-04-01 18:08:14 -06:00
|
|
|
|
|
|
|
func (c *baseClient) prepare(app *Application, server protocol.Server) {
|
|
|
|
c.app = app
|
|
|
|
c.server = server
|
|
|
|
c.fset = token.NewFileSet()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) AddFile(ctx context.Context, uri span.URI) (*protocol.ColumnMapper, error) {
|
|
|
|
fname, err := uri.Filename()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", uri, err)
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadFile(fname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", uri, err)
|
|
|
|
}
|
|
|
|
f := c.fset.AddFile(fname, -1, len(content))
|
|
|
|
f.SetLinesForContent(content)
|
|
|
|
m := protocol.NewColumnMapper(uri, c.fset, f, content)
|
|
|
|
p := &protocol.DidOpenTextDocumentParams{}
|
|
|
|
p.TextDocument.URI = string(uri)
|
|
|
|
p.TextDocument.Text = string(content)
|
|
|
|
if err := c.server.DidOpen(ctx, p); err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", uri, err)
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|