mirror of
https://github.com/golang/go
synced 2024-11-18 21:14:44 -07:00
0fd2d649e6
Add a new Forwarder type to the lsprpc package, which implements the jsonrpc2.StreamServer interface. This will be used to establish some parity in the implementation of shared and singleton gopls servers. Much more testing is needed, as is handling for the many edge cases around forwarding the LSP, but since this is functionally equivalent to TCP forwarding (and the -remote flag was already broken), I went ahead and used the Forwarder to replace the forward method in the serve command. This means that we can now use the combination of -listen and -remote to chain together gopls servers... not that there's any reason to do this. Also, wrap the new regression tests with a focus on expressiveness when testing the happy path, as well as parameterizing them so that they can be run against different client/server execution environments. This started to be sizable enough to warrant moving them to a separate regtest package. The lsprpc package tests will instead focus on unit testing the client-server binding logic. Updates golang/go#36879 Updates golang/go#34111 Change-Id: Ib98131a58aabc69299845d2ecefceccfc1199574 Reviewed-on: https://go-review.googlesource.com/c/tools/+/218698 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
// 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"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
"golang.org/x/tools/internal/lsp/cache"
|
|
"golang.org/x/tools/internal/lsp/lsprpc"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/tool"
|
|
)
|
|
|
|
// Serve is a struct that exposes the configurable parts of the LSP server as
|
|
// flags, in the right form for tool.Main to consume.
|
|
type Serve struct {
|
|
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"`
|
|
Port int `flag:"port" help:"port on which to run gopls for debugging purposes"`
|
|
Address string `flag:"listen" help:"address on which to listen for remote connections"`
|
|
Trace bool `flag:"rpc.trace" help:"print the full rpc trace in lsp inspector format"`
|
|
Debug string `flag:"debug" help:"serve debug information on the supplied address"`
|
|
|
|
app *Application
|
|
}
|
|
|
|
func (s *Serve) Name() string { return "serve" }
|
|
func (s *Serve) Usage() string { return "" }
|
|
func (s *Serve) ShortHelp() string {
|
|
return "run a server for Go code using the Language Server Protocol"
|
|
}
|
|
func (s *Serve) DetailedHelp(f *flag.FlagSet) {
|
|
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.
|
|
|
|
gopls server flags are:
|
|
`)
|
|
f.PrintDefaults()
|
|
}
|
|
|
|
// Run configures a server based on the flags, and then runs it.
|
|
// It blocks until the server shuts down.
|
|
func (s *Serve) Run(ctx context.Context, args ...string) error {
|
|
if len(args) > 0 {
|
|
return tool.CommandLineErrorf("server does not take arguments, got %v", args)
|
|
}
|
|
|
|
err, closeLog := s.app.debug.SetLogFile(s.Logfile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer closeLog()
|
|
s.app.debug.ServerAddress = s.Address
|
|
s.app.debug.DebugAddress = s.Debug
|
|
s.app.debug.Serve(ctx)
|
|
s.app.debug.MonitorMemory(ctx)
|
|
|
|
var ss jsonrpc2.StreamServer
|
|
if s.app.Remote != "" {
|
|
ss = lsprpc.NewForwarder(s.app.Remote, true)
|
|
} else {
|
|
ss = lsprpc.NewStreamServer(cache.New(s.app.options), true)
|
|
}
|
|
|
|
if s.Address != "" {
|
|
return jsonrpc2.ListenAndServe(ctx, s.Address, ss)
|
|
}
|
|
if s.Port != 0 {
|
|
addr := fmt.Sprintf(":%v", s.Port)
|
|
return jsonrpc2.ListenAndServe(ctx, addr, ss)
|
|
}
|
|
stream := jsonrpc2.NewHeaderStream(os.Stdin, os.Stdout)
|
|
if s.Trace {
|
|
stream = protocol.LoggingStream(stream, s.app.debug.LogWriter)
|
|
}
|
|
return ss.ServeStream(ctx, stream)
|
|
}
|