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.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2019-02-12 11:16:08 -07:00
|
|
|
"net"
|
2018-12-17 11:52:58 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
|
|
|
"golang.org/x/tools/internal/tool"
|
|
|
|
)
|
|
|
|
|
2019-02-07 15:05:20 -07:00
|
|
|
// Serve is a struct that exposes the configurable parts of the LSP server as
|
2018-12-17 11:52:58 -07:00
|
|
|
// flags, in the right form for tool.Main to consume.
|
2019-02-07 15:05:20 -07:00
|
|
|
type Serve struct {
|
2018-12-17 11:52:58 -07:00
|
|
|
Logfile string `flag:"logfile" help:"filename to log to. if value is \"auto\", then logging to a default output file is enabled"`
|
|
|
|
Mode string `flag:"mode" help:"no effect"`
|
2019-01-16 15:01:06 -07:00
|
|
|
Port int `flag:"port" help:"port on which to run gopls for debugging purposes"`
|
2019-02-12 11:16:08 -07:00
|
|
|
Address string `flag:"listen" help:"address on which to listen for remote connections"`
|
2019-03-29 17:04:29 -06:00
|
|
|
Trace bool `flag:"rpc.trace" help:"Print the full rpc trace in lsp inspector format"`
|
2019-02-07 15:05:20 -07:00
|
|
|
|
|
|
|
app *Application
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 15:05:20 -07:00
|
|
|
func (s *Serve) Name() string { return "serve" }
|
|
|
|
func (s *Serve) Usage() string { return "" }
|
|
|
|
func (s *Serve) ShortHelp() string {
|
2018-12-17 11:52:58 -07:00
|
|
|
return "run a server for Go code using the Language Server Protocol"
|
|
|
|
}
|
2019-02-07 15:05:20 -07:00
|
|
|
func (s *Serve) DetailedHelp(f *flag.FlagSet) {
|
2018-12-17 11:52:58 -07:00
|
|
|
fmt.Fprint(f.Output(), `
|
|
|
|
The server communicates using JSONRPC2 on stdin and stdout, and is intended to be run directly as
|
|
|
|
a child of an editor process.
|
2018-12-14 13:46:12 -07:00
|
|
|
|
|
|
|
gopls server flags are:
|
2018-12-17 11:52:58 -07:00
|
|
|
`)
|
2018-12-14 13:46:12 -07:00
|
|
|
f.PrintDefaults()
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run configures a server based on the flags, and then runs it.
|
|
|
|
// It blocks until the server shuts down.
|
2019-02-07 15:05:20 -07:00
|
|
|
func (s *Serve) Run(ctx context.Context, args ...string) error {
|
2018-12-17 11:52:58 -07:00
|
|
|
if len(args) > 0 {
|
|
|
|
return tool.CommandLineErrorf("server does not take arguments, got %v", args)
|
|
|
|
}
|
|
|
|
out := os.Stderr
|
|
|
|
if s.Logfile != "" {
|
|
|
|
filename := s.Logfile
|
|
|
|
if filename == "auto" {
|
2019-01-16 15:01:06 -07:00
|
|
|
filename = filepath.Join(os.TempDir(), fmt.Sprintf("gopls-%d.log", os.Getpid()))
|
2018-12-17 11:52:58 -07:00
|
|
|
}
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to create log file: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(io.MultiWriter(os.Stderr, f))
|
|
|
|
out = f
|
|
|
|
}
|
2019-02-07 15:05:20 -07:00
|
|
|
if s.app.Remote != "" {
|
|
|
|
return s.forward()
|
|
|
|
}
|
2019-03-29 17:04:29 -06:00
|
|
|
|
|
|
|
// For debugging purposes only.
|
|
|
|
run := func(srv *lsp.Server) {
|
|
|
|
srv.Conn.Logger = logger(s.Trace, out)
|
|
|
|
go srv.Conn.Run(ctx)
|
|
|
|
}
|
|
|
|
if s.Address != "" {
|
|
|
|
return lsp.RunServerOnAddress(ctx, s.Address, run)
|
|
|
|
}
|
|
|
|
if s.Port != 0 {
|
|
|
|
return lsp.RunServerOnPort(ctx, s.Port, run)
|
|
|
|
}
|
|
|
|
stream := jsonrpc2.NewHeaderStream(os.Stdin, os.Stdout)
|
|
|
|
srv := lsp.NewServer(stream)
|
|
|
|
srv.Conn.Logger = logger(s.Trace, out)
|
|
|
|
return srv.Conn.Run(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Serve) forward() error {
|
|
|
|
conn, err := net.Dial("tcp", s.app.Remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errc := make(chan error)
|
|
|
|
|
|
|
|
go func(conn net.Conn) {
|
|
|
|
_, err := io.Copy(conn, os.Stdin)
|
|
|
|
errc <- err
|
|
|
|
}(conn)
|
|
|
|
|
|
|
|
go func(conn net.Conn) {
|
|
|
|
_, err := io.Copy(os.Stdout, conn)
|
|
|
|
errc <- err
|
|
|
|
}(conn)
|
|
|
|
|
|
|
|
return <-errc
|
|
|
|
}
|
|
|
|
|
|
|
|
func logger(trace bool, out io.Writer) jsonrpc2.Logger {
|
|
|
|
return func(direction jsonrpc2.Direction, id *jsonrpc2.ID, elapsed time.Duration, method string, payload *json.RawMessage, err *jsonrpc2.Error) {
|
|
|
|
if !trace {
|
|
|
|
return
|
|
|
|
}
|
2019-01-10 18:42:25 -07:00
|
|
|
const eol = "\r\n\r\n\r\n"
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(out, "[Error - %v] %s %s%s %v%s", time.Now().Format("3:04:05 PM"),
|
|
|
|
direction, method, id, err, eol)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
outx := new(strings.Builder)
|
|
|
|
fmt.Fprintf(outx, "[Trace - %v] ", time.Now().Format("3:04:05 PM"))
|
|
|
|
switch direction {
|
|
|
|
case jsonrpc2.Send:
|
|
|
|
fmt.Fprint(outx, "Received ")
|
|
|
|
case jsonrpc2.Receive:
|
|
|
|
fmt.Fprint(outx, "Sending ")
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case id == nil:
|
|
|
|
fmt.Fprint(outx, "notification ")
|
|
|
|
case elapsed >= 0:
|
|
|
|
fmt.Fprint(outx, "response ")
|
|
|
|
default:
|
|
|
|
fmt.Fprint(outx, "request ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(outx, "'%s", method)
|
|
|
|
switch {
|
|
|
|
case id == nil:
|
|
|
|
// do nothing
|
|
|
|
case id.Name != "":
|
|
|
|
fmt.Fprintf(outx, " - (%s)", id.Name)
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(outx, " - (%d)", id.Number)
|
|
|
|
}
|
|
|
|
fmt.Fprint(outx, "'")
|
|
|
|
if elapsed >= 0 {
|
|
|
|
msec := int(elapsed.Round(time.Millisecond) / time.Millisecond)
|
|
|
|
fmt.Fprintf(outx, " in %dms", msec)
|
|
|
|
}
|
2019-02-05 15:27:35 -07:00
|
|
|
params := "null"
|
|
|
|
if payload != nil {
|
|
|
|
params = string(*payload)
|
|
|
|
}
|
2019-01-10 18:42:25 -07:00
|
|
|
if params == "null" {
|
|
|
|
params = "{}"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(outx, ".\r\nParams: %s%s", params, eol)
|
|
|
|
fmt.Fprintf(out, "%s", outx.String())
|
|
|
|
}
|
2019-02-07 15:05:20 -07:00
|
|
|
}
|