mirror of
https://github.com/golang/go
synced 2024-11-18 19:24:39 -07:00
internal/lsp: process configuration per workspace folder
Change-Id: Ibd72a13166b65e418a40bf850401573a6b9caf0f Reviewed-on: https://go-review.googlesource.com/c/tools/+/187819 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
db2fa46ec3
commit
fc6e2057e7
@ -23,10 +23,10 @@ import (
|
|||||||
func (s *Server) initialize(ctx context.Context, params *protocol.InitializeParams) (*protocol.InitializeResult, error) {
|
func (s *Server) initialize(ctx context.Context, params *protocol.InitializeParams) (*protocol.InitializeResult, error) {
|
||||||
s.initializedMu.Lock()
|
s.initializedMu.Lock()
|
||||||
defer s.initializedMu.Unlock()
|
defer s.initializedMu.Unlock()
|
||||||
if s.isInitialized {
|
if s.state >= serverInitializing {
|
||||||
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server already initialized")
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server already initialized")
|
||||||
}
|
}
|
||||||
s.isInitialized = true // mark server as initialized now
|
s.state = serverInitializing
|
||||||
|
|
||||||
// TODO: Remove the option once we are certain there are no issues here.
|
// TODO: Remove the option once we are certain there are no issues here.
|
||||||
s.textDocumentSyncKind = protocol.Incremental
|
s.textDocumentSyncKind = protocol.Incremental
|
||||||
@ -140,16 +140,7 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
for _, view := range s.session.Views() {
|
for _, view := range s.session.Views() {
|
||||||
config, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{
|
if err := s.fetchConfig(ctx, view); err != nil {
|
||||||
Items: []protocol.ConfigurationItem{{
|
|
||||||
ScopeURI: protocol.NewURI(view.Folder()),
|
|
||||||
Section: "gopls",
|
|
||||||
}},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := s.processConfig(ctx, view, config[0]); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,6 +151,28 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) fetchConfig(ctx context.Context, view source.View) error {
|
||||||
|
configs, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{
|
||||||
|
Items: []protocol.ConfigurationItem{{
|
||||||
|
ScopeURI: protocol.NewURI(view.Folder()),
|
||||||
|
Section: "gopls",
|
||||||
|
}, {
|
||||||
|
ScopeURI: protocol.NewURI(view.Folder()),
|
||||||
|
Section: view.Name(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, config := range configs {
|
||||||
|
if err := s.processConfig(ctx, view, config); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) processConfig(ctx context.Context, view source.View, config interface{}) error {
|
func (s *Server) processConfig(ctx context.Context, view source.View, config interface{}) error {
|
||||||
// TODO: We should probably store and process more of the config.
|
// TODO: We should probably store and process more of the config.
|
||||||
if config == nil {
|
if config == nil {
|
||||||
@ -238,17 +251,17 @@ func (s *Server) processConfig(ctx context.Context, view source.View, config int
|
|||||||
func (s *Server) shutdown(ctx context.Context) error {
|
func (s *Server) shutdown(ctx context.Context) error {
|
||||||
s.initializedMu.Lock()
|
s.initializedMu.Lock()
|
||||||
defer s.initializedMu.Unlock()
|
defer s.initializedMu.Unlock()
|
||||||
if !s.isInitialized {
|
if s.state < serverInitialized {
|
||||||
return jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server not initialized")
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server not initialized")
|
||||||
}
|
}
|
||||||
// drop all the active views
|
// drop all the active views
|
||||||
s.session.Shutdown(ctx)
|
s.session.Shutdown(ctx)
|
||||||
s.isInitialized = false
|
s.state = serverShutDown
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) exit(ctx context.Context) error {
|
func (s *Server) exit(ctx context.Context) error {
|
||||||
if s.isInitialized {
|
if s.state != serverShutDown {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -60,12 +60,21 @@ func (s *Server) Run(ctx context.Context) error {
|
|||||||
return s.Conn.Run(ctx)
|
return s.Conn.Run(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type serverState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
serverCreated = serverState(iota)
|
||||||
|
serverInitializing // set once the server has received "initialize" request
|
||||||
|
serverInitialized // set once the server has received "initialized" request
|
||||||
|
serverShutDown
|
||||||
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Conn *jsonrpc2.Conn
|
Conn *jsonrpc2.Conn
|
||||||
client protocol.Client
|
client protocol.Client
|
||||||
|
|
||||||
initializedMu sync.Mutex
|
initializedMu sync.Mutex
|
||||||
isInitialized bool // set once the server has received "initialize" request
|
state serverState
|
||||||
|
|
||||||
// Configurations.
|
// Configurations.
|
||||||
// TODO(rstambler): Separate these into their own struct?
|
// TODO(rstambler): Separate these into their own struct?
|
||||||
|
@ -31,6 +31,9 @@ func (s *Server) changeFolders(ctx context.Context, event protocol.WorkspaceFold
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) addView(ctx context.Context, name string, uri span.URI) error {
|
func (s *Server) addView(ctx context.Context, name string, uri span.URI) error {
|
||||||
s.session.NewView(ctx, name, uri)
|
view := s.session.NewView(ctx, name, uri)
|
||||||
|
if s.state >= serverInitialized {
|
||||||
|
s.fetchConfig(ctx, view)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user