mirror of
https://github.com/golang/go
synced 2024-11-18 18:14:43 -07:00
internal/lsp: remove the Context argument from NewSession
The passed-in Context is not used, and creates the illusion of a startup dependency problem: existing code is careful to pass in the context containing the correct Client instance. This allows passing in a source.Session, rather than a source.Cache, into lsp server constructors. Updates golang/go#34111 Change-Id: I081ad6fa800b846b63e04d7164577e3a32966704 Reviewed-on: https://go-review.googlesource.com/c/tools/+/215740 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
parent
29f64efd55
commit
78d067421b
2
internal/lsp/cache/cache.go
vendored
2
internal/lsp/cache/cache.go
vendored
@ -73,7 +73,7 @@ func (c *cache) GetFile(uri span.URI) source.FileHandle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cache) NewSession(ctx context.Context) source.Session {
|
func (c *cache) NewSession() source.Session {
|
||||||
index := atomic.AddInt64(&sessionIndex, 1)
|
index := atomic.AddInt64(&sessionIndex, 1)
|
||||||
s := &session{
|
s := &session{
|
||||||
cache: c,
|
cache: c,
|
||||||
|
@ -40,7 +40,7 @@ func TestCapabilities(t *testing.T) {
|
|||||||
params.Capabilities.Workspace.Configuration = true
|
params.Capabilities.Workspace.Configuration = true
|
||||||
|
|
||||||
// Send an initialize request to the server.
|
// Send an initialize request to the server.
|
||||||
ctx, c.Server = lsp.NewClientServer(ctx, cache.New(app.options), c.Client)
|
ctx, c.Server = lsp.NewClientServer(ctx, cache.New(app.options).NewSession(), c.Client)
|
||||||
result, err := c.Server.Initialize(ctx, params)
|
result, err := c.Server.Initialize(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -192,7 +192,7 @@ func (app *Application) connect(ctx context.Context) (*connection, error) {
|
|||||||
switch app.Remote {
|
switch app.Remote {
|
||||||
case "":
|
case "":
|
||||||
connection := newConnection(app)
|
connection := newConnection(app)
|
||||||
ctx, connection.Server = lsp.NewClientServer(ctx, cache.New(app.options), connection.Client)
|
ctx, connection.Server = lsp.NewClientServer(ctx, cache.New(app.options).NewSession(), connection.Client)
|
||||||
return connection, connection.initialize(ctx, app.options)
|
return connection, connection.initialize(ctx, app.options)
|
||||||
case "internal":
|
case "internal":
|
||||||
internalMu.Lock()
|
internalMu.Lock()
|
||||||
@ -208,7 +208,7 @@ func (app *Application) connect(ctx context.Context) (*connection, error) {
|
|||||||
ctx, jc, connection.Server = protocol.NewClient(ctx, jsonrpc2.NewHeaderStream(cr, cw), connection.Client)
|
ctx, jc, connection.Server = protocol.NewClient(ctx, jsonrpc2.NewHeaderStream(cr, cw), connection.Client)
|
||||||
go jc.Run(ctx)
|
go jc.Run(ctx)
|
||||||
go func() {
|
go func() {
|
||||||
ctx, srv := lsp.NewServer(ctx, cache.New(app.options), jsonrpc2.NewHeaderStream(sr, sw))
|
ctx, srv := lsp.NewServer(ctx, cache.New(app.options).NewSession(), jsonrpc2.NewHeaderStream(sr, sw))
|
||||||
srv.Run(ctx)
|
srv.Run(ctx)
|
||||||
}()
|
}()
|
||||||
if err := connection.initialize(ctx, app.options); err != nil {
|
if err := connection.initialize(ctx, app.options); err != nil {
|
||||||
|
@ -97,7 +97,7 @@ func (s *Serve) Run(ctx context.Context, args ...string) error {
|
|||||||
if s.Trace {
|
if s.Trace {
|
||||||
stream = protocol.LoggingStream(stream, out)
|
stream = protocol.LoggingStream(stream, out)
|
||||||
}
|
}
|
||||||
ctx, srv := lsp.NewServer(ctx, cache.New(s.app.options), stream)
|
ctx, srv := lsp.NewServer(ctx, cache.New(s.app.options).NewSession(), stream)
|
||||||
return prepare(ctx, srv).Run(ctx)
|
return prepare(ctx, srv).Run(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,6 +140,7 @@ func (r *runner) RunGoplsCmd(t testing.TB, args ...string) (string, string) {
|
|||||||
return string(stdout), string(stderr)
|
return string(stdout), string(stderr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NormalizeGoplsCmd runs the gopls command and normalizes its output.
|
||||||
func (r *runner) NormalizeGoplsCmd(t testing.TB, args ...string) (string, string) {
|
func (r *runner) NormalizeGoplsCmd(t testing.TB, args ...string) (string, string) {
|
||||||
stdout, stderr := r.RunGoplsCmd(t, args...)
|
stdout, stderr := r.RunGoplsCmd(t, args...)
|
||||||
return r.Normalize(stdout), r.Normalize(stderr)
|
return r.Normalize(stdout), r.Normalize(stderr)
|
||||||
|
@ -52,7 +52,7 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) {
|
|||||||
defer data.Exported.Cleanup()
|
defer data.Exported.Cleanup()
|
||||||
|
|
||||||
cache := cache.New(nil)
|
cache := cache.New(nil)
|
||||||
session := cache.NewSession(ctx)
|
session := cache.NewSession()
|
||||||
options := tests.DefaultOptions()
|
options := tests.DefaultOptions()
|
||||||
session.SetOptions(options)
|
session.SetOptions(options)
|
||||||
options.Env = data.Config.Env
|
options.Env = data.Config.Env
|
||||||
@ -928,7 +928,7 @@ func TestModfileSuggestedFixes(t *testing.T) {
|
|||||||
|
|
||||||
ctx := tests.Context(t)
|
ctx := tests.Context(t)
|
||||||
cache := cache.New(nil)
|
cache := cache.New(nil)
|
||||||
session := cache.NewSession(ctx)
|
session := cache.NewSession()
|
||||||
options := tests.DefaultOptions()
|
options := tests.DefaultOptions()
|
||||||
options.TempModfile = true
|
options.TempModfile = true
|
||||||
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
|
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
|
||||||
|
@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
|
|||||||
func TestModfileRemainsUnchanged(t *testing.T) {
|
func TestModfileRemainsUnchanged(t *testing.T) {
|
||||||
ctx := tests.Context(t)
|
ctx := tests.Context(t)
|
||||||
cache := cache.New(nil)
|
cache := cache.New(nil)
|
||||||
session := cache.NewSession(ctx)
|
session := cache.NewSession()
|
||||||
options := tests.DefaultOptions()
|
options := tests.DefaultOptions()
|
||||||
options.TempModfile = true
|
options.TempModfile = true
|
||||||
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
|
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
|
||||||
@ -67,7 +67,7 @@ func TestModfileRemainsUnchanged(t *testing.T) {
|
|||||||
func TestDiagnostics(t *testing.T) {
|
func TestDiagnostics(t *testing.T) {
|
||||||
ctx := tests.Context(t)
|
ctx := tests.Context(t)
|
||||||
cache := cache.New(nil)
|
cache := cache.New(nil)
|
||||||
session := cache.NewSession(ctx)
|
session := cache.NewSession()
|
||||||
options := tests.DefaultOptions()
|
options := tests.DefaultOptions()
|
||||||
options.TempModfile = true
|
options.TempModfile = true
|
||||||
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
|
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
|
||||||
|
@ -18,23 +18,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NewClientServer
|
// NewClientServer
|
||||||
func NewClientServer(ctx context.Context, cache source.Cache, client protocol.Client) (context.Context, *Server) {
|
func NewClientServer(ctx context.Context, session source.Session, client protocol.Client) (context.Context, *Server) {
|
||||||
ctx = protocol.WithClient(ctx, client)
|
ctx = protocol.WithClient(ctx, client)
|
||||||
return ctx, &Server{
|
return ctx, &Server{
|
||||||
client: client,
|
client: client,
|
||||||
session: cache.NewSession(ctx),
|
session: session,
|
||||||
delivered: make(map[span.URI]sentDiagnostics),
|
delivered: make(map[span.URI]sentDiagnostics),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer starts an LSP server on the supplied stream, and waits until the
|
// NewServer creates an LSP server and binds it to handle incoming client
|
||||||
// stream is closed.
|
// messages on on the supplied stream.
|
||||||
func NewServer(ctx context.Context, cache source.Cache, stream jsonrpc2.Stream) (context.Context, *Server) {
|
func NewServer(ctx context.Context, session source.Session, stream jsonrpc2.Stream) (context.Context, *Server) {
|
||||||
s := &Server{
|
s := &Server{
|
||||||
delivered: make(map[span.URI]sentDiagnostics),
|
delivered: make(map[span.URI]sentDiagnostics),
|
||||||
|
session: session,
|
||||||
}
|
}
|
||||||
ctx, s.Conn, s.client = protocol.NewServer(ctx, stream, s)
|
ctx, s.Conn, s.client = protocol.NewServer(ctx, stream, s)
|
||||||
s.session = cache.NewSession(ctx)
|
|
||||||
return ctx, s
|
return ctx, s
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ func RunServerOnAddress(ctx context.Context, cache source.Cache, addr string, h
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
h(NewServer(ctx, cache, jsonrpc2.NewHeaderStream(conn, conn)))
|
h(NewServer(ctx, cache.NewSession(), jsonrpc2.NewHeaderStream(conn, conn)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func testSource(t *testing.T, exporter packagestest.Exporter) {
|
|||||||
defer data.Exported.Cleanup()
|
defer data.Exported.Cleanup()
|
||||||
|
|
||||||
cache := cache.New(nil)
|
cache := cache.New(nil)
|
||||||
session := cache.NewSession(ctx)
|
session := cache.NewSession()
|
||||||
options := tests.DefaultOptions()
|
options := tests.DefaultOptions()
|
||||||
options.Env = data.Config.Env
|
options.Env = data.Config.Env
|
||||||
view, _, err := session.NewView(ctx, "source_test", span.FileURI(data.Config.Dir), options)
|
view, _, err := session.NewView(ctx, "source_test", span.FileURI(data.Config.Dir), options)
|
||||||
|
@ -215,7 +215,7 @@ type Cache interface {
|
|||||||
FileSystem
|
FileSystem
|
||||||
|
|
||||||
// NewSession creates a new Session manager and returns it.
|
// NewSession creates a new Session manager and returns it.
|
||||||
NewSession(ctx context.Context) Session
|
NewSession() Session
|
||||||
|
|
||||||
// FileSet returns the shared fileset used by all files in the system.
|
// FileSet returns the shared fileset used by all files in the system.
|
||||||
FileSet() *token.FileSet
|
FileSet() *token.FileSet
|
||||||
|
Loading…
Reference in New Issue
Block a user