@@ -293,10 +293,14 @@ var (
293
293
func (app * Application ) connect (ctx context.Context , onProgress func (* protocol.ProgressParams )) (* connection , error ) {
294
294
switch {
295
295
case app .Remote == "" :
296
- connection := newConnection (app , onProgress )
297
- connection .Server = lsp .NewServer (cache .NewSession (ctx , cache .New (nil ), app .options ), connection .Client )
298
- ctx = protocol .WithClient (ctx , connection .Client )
299
- return connection , connection .initialize (ctx , app .options )
296
+ client := newClient (app , onProgress )
297
+ server := lsp .NewServer (cache .NewSession (ctx , cache .New (nil ), app .options ), client )
298
+ conn := newConnection (server , client )
299
+ if err := conn .initialize (protocol .WithClient (ctx , client ), app .options ); err != nil {
300
+ return nil , err
301
+ }
302
+ return conn , nil
303
+
300
304
case strings .HasPrefix (app .Remote , "internal@" ):
301
305
internalMu .Lock ()
302
306
defer internalMu .Unlock ()
@@ -331,19 +335,19 @@ func CloseTestConnections(ctx context.Context) {
331
335
}
332
336
333
337
func (app * Application ) connectRemote (ctx context.Context , remote string ) (* connection , error ) {
334
- connection := newConnection (app , nil )
335
338
conn , err := lsprpc .ConnectToRemote (ctx , remote )
336
339
if err != nil {
337
340
return nil , err
338
341
}
339
342
stream := jsonrpc2 .NewHeaderStream (conn )
340
343
cc := jsonrpc2 .NewConn (stream )
341
- connection .Server = protocol .ServerDispatcher (cc )
342
- ctx = protocol .WithClient (ctx , connection .Client )
344
+ server := protocol .ServerDispatcher (cc )
345
+ client := newClient (app , nil )
346
+ connection := newConnection (server , client )
347
+ ctx = protocol .WithClient (ctx , connection .client )
343
348
cc .Go (ctx ,
344
349
protocol .Handlers (
345
- protocol .ClientHandler (connection .Client ,
346
- jsonrpc2 .MethodNotFound )))
350
+ protocol .ClientHandler (client , jsonrpc2 .MethodNotFound )))
347
351
return connection , connection .initialize (ctx , app .options )
348
352
}
349
353
@@ -355,7 +359,7 @@ var matcherString = map[source.SymbolMatcher]string{
355
359
356
360
func (c * connection ) initialize (ctx context.Context , options func (* source.Options )) error {
357
361
params := & protocol.ParamInitialize {}
358
- params .RootURI = protocol .URIFromPath (c .Client .app .wd )
362
+ params .RootURI = protocol .URIFromPath (c .client .app .wd )
359
363
params .Capabilities .Workspace .Configuration = true
360
364
361
365
// Make sure to respect configured options when sending initialize request.
@@ -377,7 +381,7 @@ func (c *connection) initialize(ctx context.Context, options func(*source.Option
377
381
378
382
// If the subcommand has registered a progress handler, report the progress
379
383
// capability.
380
- if c .Client .onProgress != nil {
384
+ if c .client .onProgress != nil {
381
385
params .Capabilities .Window .WorkDoneProgress = true
382
386
}
383
387
@@ -395,11 +399,10 @@ func (c *connection) initialize(ctx context.Context, options func(*source.Option
395
399
396
400
type connection struct {
397
401
protocol.Server
398
- Client * cmdClient
402
+ client * cmdClient
399
403
}
400
404
401
405
type cmdClient struct {
402
- protocol.Server
403
406
app * Application
404
407
onProgress func (* protocol.ProgressParams )
405
408
@@ -417,13 +420,18 @@ type cmdFile struct {
417
420
diagnostics []protocol.Diagnostic
418
421
}
419
422
420
- func newConnection (app * Application , onProgress func (* protocol.ProgressParams )) * connection {
423
+ func newClient (app * Application , onProgress func (* protocol.ProgressParams )) * cmdClient {
424
+ return & cmdClient {
425
+ app : app ,
426
+ onProgress : onProgress ,
427
+ files : make (map [span.URI ]* cmdFile ),
428
+ }
429
+ }
430
+
431
+ func newConnection (server protocol.Server , client * cmdClient ) * connection {
421
432
return & connection {
422
- Client : & cmdClient {
423
- app : app ,
424
- onProgress : onProgress ,
425
- files : make (map [span.URI ]* cmdFile ),
426
- },
433
+ Server : server ,
434
+ client : client ,
427
435
}
428
436
}
429
437
@@ -611,7 +619,7 @@ func (c *cmdClient) openFile(ctx context.Context, uri span.URI) *cmdFile {
611
619
// - map a (URI, protocol.Range) to a MappedRange;
612
620
// - parse a command-line argument to a MappedRange.
613
621
func (c * connection ) openFile (ctx context.Context , uri span.URI ) (* cmdFile , error ) {
614
- file := c .Client .openFile (ctx , uri )
622
+ file := c .client .openFile (ctx , uri )
615
623
if file .err != nil {
616
624
return nil , file .err
617
625
}
@@ -646,22 +654,22 @@ func (c *connection) diagnoseFiles(ctx context.Context, files []span.URI) error
646
654
for _ , file := range files {
647
655
untypedFiles = append (untypedFiles , string (file ))
648
656
}
649
- c .Client .diagnosticsMu .Lock ()
650
- defer c .Client .diagnosticsMu .Unlock ()
657
+ c .client .diagnosticsMu .Lock ()
658
+ defer c .client .diagnosticsMu .Unlock ()
651
659
652
- c .Client .diagnosticsDone = make (chan struct {})
660
+ c .client .diagnosticsDone = make (chan struct {})
653
661
_ , err := c .Server .NonstandardRequest (ctx , "gopls/diagnoseFiles" , map [string ]interface {}{"files" : untypedFiles })
654
662
if err != nil {
655
- close (c .Client .diagnosticsDone )
663
+ close (c .client .diagnosticsDone )
656
664
return err
657
665
}
658
666
659
- <- c .Client .diagnosticsDone
667
+ <- c .client .diagnosticsDone
660
668
return nil
661
669
}
662
670
663
671
func (c * connection ) terminate (ctx context.Context ) {
664
- if strings .HasPrefix (c .Client .app .Remote , "internal@" ) {
672
+ if strings .HasPrefix (c .client .app .Remote , "internal@" ) {
665
673
// internal connections need to be left alive for the next test
666
674
return
667
675
}
0 commit comments