1
0
mirror of https://github.com/golang/go synced 2024-11-18 21:24:44 -07:00

internal/lsp: clean up the confguraton handling

Now the jsonrpc2 library allows you to call outgoing methods within a handler
we can clean up some stuff and also have it work correctly in more cases.

Change-Id: I8633069816d92f7cc16842431775efb1a98a506a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/170008
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Ian Cottrell 2019-03-29 14:16:59 -04:00
parent 1f1f5f5d57
commit 0ec5c269d4
3 changed files with 26 additions and 34 deletions

View File

@ -18,9 +18,6 @@ func (s *Server) cacheAndDiagnose(ctx context.Context, uri span.URI, content str
return err return err
} }
go func() { go func() {
//TODO: this is an ugly hack to make the diagnostics call happen after the
// configuration is collected, we need to rewrite all the concurrency
<-s.configured
ctx := s.view.BackgroundContext() ctx := s.view.BackgroundContext()
if ctx.Err() != nil { if ctx.Err() != nil {
return return

View File

@ -11,12 +11,16 @@ import (
"golang.org/x/tools/internal/jsonrpc2" "golang.org/x/tools/internal/jsonrpc2"
) )
const defaultMessageBufferSize = 20
func canceller(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) { func canceller(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
conn.Notify(context.Background(), "$/cancelRequest", &CancelParams{ID: *req.ID}) conn.Notify(context.Background(), "$/cancelRequest", &CancelParams{ID: *req.ID})
} }
func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server) { func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server) {
conn := jsonrpc2.NewConn(stream) conn := jsonrpc2.NewConn(stream)
conn.Capacity = defaultMessageBufferSize
conn.RejectIfOverloaded = true
conn.Handler = clientHandler(client) conn.Handler = clientHandler(client)
conn.Canceler = jsonrpc2.Canceler(canceller) conn.Canceler = jsonrpc2.Canceler(canceller)
return conn, &serverDispatcher{Conn: conn} return conn, &serverDispatcher{Conn: conn}
@ -24,6 +28,8 @@ func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server) {
func NewServer(stream jsonrpc2.Stream, server Server) (*jsonrpc2.Conn, Client) { func NewServer(stream jsonrpc2.Stream, server Server) (*jsonrpc2.Conn, Client) {
conn := jsonrpc2.NewConn(stream) conn := jsonrpc2.NewConn(stream)
conn.Capacity = defaultMessageBufferSize
conn.RejectIfOverloaded = true
conn.Handler = serverHandler(server) conn.Handler = serverHandler(server)
conn.Canceler = jsonrpc2.Canceler(canceller) conn.Canceler = jsonrpc2.Canceler(canceller)
return conn, &clientDispatcher{Conn: conn} return conn, &clientDispatcher{Conn: conn}

View File

@ -28,17 +28,14 @@ import (
// NewClientServer // NewClientServer
func NewClientServer(client protocol.Client) *Server { func NewClientServer(client protocol.Client) *Server {
return &Server{ return &Server{
client: client, client: client,
configured: make(chan struct{}),
} }
} }
// NewServer starts an LSP server on the supplied stream, and waits until the // NewServer starts an LSP server on the supplied stream, and waits until the
// stream is closed. // stream is closed.
func NewServer(stream jsonrpc2.Stream) *Server { func NewServer(stream jsonrpc2.Stream) *Server {
s := &Server{ s := &Server{}
configured: make(chan struct{}),
}
s.Conn, s.client = protocol.NewServer(stream, s) s.Conn, s.client = protocol.NewServer(stream, s)
return s return s
} }
@ -81,8 +78,6 @@ type Server struct {
textDocumentSyncKind protocol.TextDocumentSyncKind textDocumentSyncKind protocol.TextDocumentSyncKind
view *cache.View view *cache.View
configured chan struct{}
} }
func (s *Server) Run(ctx context.Context) error { func (s *Server) Run(ctx context.Context) error {
@ -160,30 +155,24 @@ func (s *Server) Initialize(ctx context.Context, params *protocol.InitializePara
} }
func (s *Server) Initialized(ctx context.Context, params *protocol.InitializedParams) error { func (s *Server) Initialized(ctx context.Context, params *protocol.InitializedParams) error {
go func() { s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
// we hae to do this in a go routine to unblock the jsonrpc processor Registrations: []protocol.Registration{{
// but we also have to block all calls to packages.Load until this is done ID: "workspace/didChangeConfiguration",
// TODO: we need to rewrite all the concurrency handling hin the server Method: "workspace/didChangeConfiguration",
defer func() { close(s.configured) }() }},
s.client.RegisterCapability(ctx, &protocol.RegistrationParams{ })
Registrations: []protocol.Registration{{ config, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{
ID: "workspace/didChangeConfiguration", Items: []protocol.ConfigurationItem{{
Method: "workspace/didChangeConfiguration", ScopeURI: protocol.NewURI(s.view.Folder),
}}, Section: "gopls",
}) }},
config, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{ })
Items: []protocol.ConfigurationItem{{ if err != nil {
ScopeURI: protocol.NewURI(s.view.Folder), return err
Section: "gopls", }
}}, if err := s.processConfig(config[0]); err != nil {
}) return err
if err != nil { }
s.Error(err)
}
if err := s.processConfig(config[0]); err != nil {
s.Error(err)
}
}()
return nil return nil
} }