internal/lsp: hold notifications until gopls is initialized

Errors in options generate showMessage notifications, but these
were lost if they were sent before the initialization handshake completes.

Change-Id: I011da2387467bb6bf188201a7294e1bb30805cbc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/263518
Run-TryBot: Peter Weinberger <pjw@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Peter Weinberger <pjw@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Peter Weinbergr 2020-10-18 18:52:49 -04:00 committed by Peter Weinberger
parent b894a3290f
commit f5c826d190
2 changed files with 25 additions and 5 deletions

View File

@ -161,6 +161,11 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
s.state = serverInitialized
s.stateMu.Unlock()
for _, not := range s.notifications {
s.client.ShowMessage(ctx, not)
}
s.notifications = nil
options := s.session.Options()
defer func() { s.session.SetOptions(options) }()
@ -421,22 +426,34 @@ func (s *Server) fetchConfig(ctx context.Context, name string, folder span.URI,
return nil
}
func (s *Server) eventuallyShowMessage(ctx context.Context, msg *protocol.ShowMessageParams) error {
s.stateMu.Lock()
defer s.stateMu.Unlock()
if s.state == serverInitialized {
return s.client.ShowMessage(ctx, msg)
}
s.notifications = append(s.notifications, msg)
return nil
}
func (s *Server) handleOptionResults(ctx context.Context, results source.OptionResults) error {
for _, result := range results {
if result.Error != nil {
if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
msg := &protocol.ShowMessageParams{
Type: protocol.Error,
Message: result.Error.Error(),
}); err != nil {
}
if err := s.eventuallyShowMessage(ctx, msg); err != nil {
return err
}
}
switch result.State {
case source.OptionUnexpected:
if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
msg := &protocol.ShowMessageParams{
Type: protocol.Error,
Message: fmt.Sprintf("unexpected gopls setting %q", result.Name),
}); err != nil {
}
if err := s.eventuallyShowMessage(ctx, msg); err != nil {
return err
}
case source.OptionDeprecated:
@ -444,7 +461,7 @@ func (s *Server) handleOptionResults(ctx context.Context, results source.OptionR
if result.Replacement != "" {
msg = fmt.Sprintf("%s, use %q instead", msg, result.Replacement)
}
if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
if err := s.eventuallyShowMessage(ctx, &protocol.ShowMessageParams{
Type: protocol.Warning,
Message: msg,
}); err != nil {

View File

@ -67,6 +67,9 @@ type Server struct {
session source.Session
// notifications generated before serverInitialized
notifications []*protocol.ShowMessageParams
// changedFiles tracks files for which there has been a textDocument/didChange.
changedFilesMu sync.Mutex
changedFiles map[span.URI]struct{}