Skip to content

Commit 8b11e06

Browse files
committed
gopls/internal/cmd: cleanup: unify connection, cmdClient
This cleanup change merges connection and cmdClient, since both represent the client-side state of a single connection. Details: - client holds the server (and its initialize result) - client.initialize is the operation that ties the client and server together. We can use it to simplify capabilities_test.go. - protocol.Server is no longer embedded - filesMu locking pushed down into client.getFile - more doc comments - the server helpers diagnosefiles, executeCommand are no longer methods. (The improvement is positive but rather marginal; this change grew out of a question about the role and cardinality of conn, cmdClient and server.) Change-Id: I9a16a4b8281e61d2cdf116f2b9570fa3a8f5b1f5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/681155 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent d1c2fd2 commit 8b11e06

26 files changed

+166
-180
lines changed

gopls/internal/cmd/call_hierarchy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
6060
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
6161
}
6262

63-
callItems, err := conn.PrepareCallHierarchy(ctx, &p)
63+
callItems, err := conn.server.PrepareCallHierarchy(ctx, &p)
6464
if err != nil {
6565
return err
6666
}
@@ -69,7 +69,7 @@ func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
6969
}
7070

7171
for _, item := range callItems {
72-
incomingCalls, err := conn.IncomingCalls(ctx, &protocol.CallHierarchyIncomingCallsParams{Item: item})
72+
incomingCalls, err := conn.server.IncomingCalls(ctx, &protocol.CallHierarchyIncomingCallsParams{Item: item})
7373
if err != nil {
7474
return err
7575
}
@@ -89,7 +89,7 @@ func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
8989
}
9090
fmt.Printf("identifier: %s\n", printString)
9191

92-
outgoingCalls, err := conn.OutgoingCalls(ctx, &protocol.CallHierarchyOutgoingCallsParams{Item: item})
92+
outgoingCalls, err := conn.server.OutgoingCalls(ctx, &protocol.CallHierarchyOutgoingCallsParams{Item: item})
9393
if err != nil {
9494
return err
9595
}
@@ -109,7 +109,7 @@ func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
109109

110110
// callItemPrintString returns a protocol.CallHierarchyItem object represented as a string.
111111
// item and call ranges (protocol.Range) are converted to user friendly spans (1-indexed).
112-
func callItemPrintString(ctx context.Context, conn *connection, item protocol.CallHierarchyItem, callsURI protocol.DocumentURI, calls []protocol.Range) (string, error) {
112+
func callItemPrintString(ctx context.Context, conn *client, item protocol.CallHierarchyItem, callsURI protocol.DocumentURI, calls []protocol.Range) (string, error) {
113113
itemFile, err := conn.openFile(ctx, item.URI)
114114
if err != nil {
115115
return "", err

gopls/internal/cmd/capabilities_test.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ import (
2121
// TestCapabilities does some minimal validation of the server's adherence to the LSP.
2222
// The checks in the test are added as changes are made and errors noticed.
2323
func TestCapabilities(t *testing.T) {
24-
// TODO(bcmills): This test fails on js/wasm, which is not unexpected, but the
25-
// failure mode is that the DidOpen call below reports "no views in session",
26-
// which seems a little too cryptic.
27-
// Is there some missing error reporting somewhere?
24+
// server.DidOpen fails to obtain metadata without go command (e.g. on wasm).
2825
testenv.NeedsTool(t, "go")
2926

3027
tmpDir, err := os.MkdirTemp("", "fake")
@@ -41,35 +38,28 @@ func TestCapabilities(t *testing.T) {
4138
defer os.RemoveAll(tmpDir)
4239

4340
app := New()
44-
45-
params := &protocol.ParamInitialize{}
46-
params.RootURI = protocol.URIFromPath(tmpDir)
47-
params.Capabilities.Workspace.Configuration = true
48-
49-
// Send an initialize request to the server.
5041
ctx := context.Background()
42+
43+
// Initialize the client.
44+
// (Unlike app.connect, we use minimal Initialize params.)
5145
client := newClient(app)
5246
options := settings.DefaultOptions(app.options)
5347
server := server.New(cache.NewSession(ctx, cache.New(nil)), client, options)
54-
result, err := server.Initialize(ctx, params)
55-
if err != nil {
48+
params := &protocol.ParamInitialize{}
49+
params.RootURI = protocol.URIFromPath(tmpDir)
50+
params.Capabilities.Workspace.Configuration = true
51+
if err := client.initialize(ctx, server, params); err != nil {
5652
t.Fatal(err)
5753
}
58-
// Validate initialization result.
59-
if err := validateCapabilities(result); err != nil {
54+
defer client.terminate(ctx)
55+
56+
if err := validateCapabilities(client.initializeResult); err != nil {
6057
t.Error(err)
6158
}
62-
// Complete initialization of server.
63-
if err := server.Initialized(ctx, &protocol.InitializedParams{}); err != nil {
64-
t.Fatal(err)
65-
}
66-
67-
c := newConnection(server, client)
68-
defer c.terminate(ctx)
6959

7060
// Open the file on the server side.
7161
uri := protocol.URIFromPath(tmpFile)
72-
if err := c.Server.DidOpen(ctx, &protocol.DidOpenTextDocumentParams{
62+
if err := server.DidOpen(ctx, &protocol.DidOpenTextDocumentParams{
7363
TextDocument: protocol.TextDocumentItem{
7464
URI: uri,
7565
LanguageID: "go",
@@ -82,7 +72,7 @@ func TestCapabilities(t *testing.T) {
8272

8373
// If we are sending a full text change, the change.Range must be nil.
8474
// It is not enough for the Change to be empty, as that is ambiguous.
85-
if err := c.Server.DidChange(ctx, &protocol.DidChangeTextDocumentParams{
75+
if err := server.DidChange(ctx, &protocol.DidChangeTextDocumentParams{
8676
TextDocument: protocol.VersionedTextDocumentIdentifier{
8777
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
8878
URI: uri,
@@ -100,7 +90,7 @@ func TestCapabilities(t *testing.T) {
10090
}
10191

10292
// Send a code action request to validate expected types.
103-
actions, err := c.Server.CodeAction(ctx, &protocol.CodeActionParams{
93+
actions, err := server.CodeAction(ctx, &protocol.CodeActionParams{
10494
TextDocument: protocol.TextDocumentIdentifier{
10595
URI: uri,
10696
},
@@ -118,7 +108,7 @@ func TestCapabilities(t *testing.T) {
118108
}
119109
}
120110

121-
if err := c.Server.DidSave(ctx, &protocol.DidSaveTextDocumentParams{
111+
if err := server.DidSave(ctx, &protocol.DidSaveTextDocumentParams{
122112
TextDocument: protocol.TextDocumentIdentifier{
123113
URI: uri,
124114
},
@@ -129,7 +119,7 @@ func TestCapabilities(t *testing.T) {
129119
}
130120

131121
// Send a completion request to validate expected types.
132-
list, err := c.Server.Completion(ctx, &protocol.CompletionParams{
122+
list, err := server.Completion(ctx, &protocol.CompletionParams{
133123
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
134124
TextDocument: protocol.TextDocumentIdentifier{
135125
URI: uri,
@@ -159,10 +149,10 @@ func TestCapabilities(t *testing.T) {
159149
t.Errorf("textEdit is not TextEdit nor InsertReplaceEdit, instead it is %T", textEdit)
160150
}
161151
}
162-
if err := c.Server.Shutdown(ctx); err != nil {
152+
if err := server.Shutdown(ctx); err != nil {
163153
t.Fatal(err)
164154
}
165-
if err := c.Server.Exit(ctx); err != nil {
155+
if err := server.Exit(ctx); err != nil {
166156
t.Fatal(err)
167157
}
168158
}

gopls/internal/cmd/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (c *check) Run(ctx context.Context, args ...string) error {
8686
}
8787
checking[uri] = file
8888
}
89-
if err := conn.diagnoseFiles(ctx, uris); err != nil {
89+
if err := diagnoseFiles(ctx, conn.server, uris); err != nil {
9090
return err
9191
}
9292

0 commit comments

Comments
 (0)