mirror of
https://github.com/golang/go
synced 2024-11-18 16:44:43 -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:
parent
1f1f5f5d57
commit
0ec5c269d4
@ -18,9 +18,6 @@ func (s *Server) cacheAndDiagnose(ctx context.Context, uri span.URI, content str
|
||||
return err
|
||||
}
|
||||
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()
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
|
@ -11,12 +11,16 @@ import (
|
||||
"golang.org/x/tools/internal/jsonrpc2"
|
||||
)
|
||||
|
||||
const defaultMessageBufferSize = 20
|
||||
|
||||
func canceller(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
|
||||
conn.Notify(context.Background(), "$/cancelRequest", &CancelParams{ID: *req.ID})
|
||||
}
|
||||
|
||||
func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server) {
|
||||
conn := jsonrpc2.NewConn(stream)
|
||||
conn.Capacity = defaultMessageBufferSize
|
||||
conn.RejectIfOverloaded = true
|
||||
conn.Handler = clientHandler(client)
|
||||
conn.Canceler = jsonrpc2.Canceler(canceller)
|
||||
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) {
|
||||
conn := jsonrpc2.NewConn(stream)
|
||||
conn.Capacity = defaultMessageBufferSize
|
||||
conn.RejectIfOverloaded = true
|
||||
conn.Handler = serverHandler(server)
|
||||
conn.Canceler = jsonrpc2.Canceler(canceller)
|
||||
return conn, &clientDispatcher{Conn: conn}
|
||||
|
@ -28,17 +28,14 @@ import (
|
||||
// NewClientServer
|
||||
func NewClientServer(client protocol.Client) *Server {
|
||||
return &Server{
|
||||
client: client,
|
||||
configured: make(chan struct{}),
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
// NewServer starts an LSP server on the supplied stream, and waits until the
|
||||
// stream is closed.
|
||||
func NewServer(stream jsonrpc2.Stream) *Server {
|
||||
s := &Server{
|
||||
configured: make(chan struct{}),
|
||||
}
|
||||
s := &Server{}
|
||||
s.Conn, s.client = protocol.NewServer(stream, s)
|
||||
return s
|
||||
}
|
||||
@ -81,8 +78,6 @@ type Server struct {
|
||||
textDocumentSyncKind protocol.TextDocumentSyncKind
|
||||
|
||||
view *cache.View
|
||||
|
||||
configured chan struct{}
|
||||
}
|
||||
|
||||
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 {
|
||||
go func() {
|
||||
// we hae to do this in a go routine to unblock the jsonrpc processor
|
||||
// but we also have to block all calls to packages.Load until this is done
|
||||
// TODO: we need to rewrite all the concurrency handling hin the server
|
||||
defer func() { close(s.configured) }()
|
||||
s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
|
||||
Registrations: []protocol.Registration{{
|
||||
ID: "workspace/didChangeConfiguration",
|
||||
Method: "workspace/didChangeConfiguration",
|
||||
}},
|
||||
})
|
||||
config, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{
|
||||
Items: []protocol.ConfigurationItem{{
|
||||
ScopeURI: protocol.NewURI(s.view.Folder),
|
||||
Section: "gopls",
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
s.Error(err)
|
||||
}
|
||||
if err := s.processConfig(config[0]); err != nil {
|
||||
s.Error(err)
|
||||
}
|
||||
}()
|
||||
s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
|
||||
Registrations: []protocol.Registration{{
|
||||
ID: "workspace/didChangeConfiguration",
|
||||
Method: "workspace/didChangeConfiguration",
|
||||
}},
|
||||
})
|
||||
config, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{
|
||||
Items: []protocol.ConfigurationItem{{
|
||||
ScopeURI: protocol.NewURI(s.view.Folder),
|
||||
Section: "gopls",
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.processConfig(config[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user