Skip to content

Commit 02d0726

Browse files
Reorganize LSP extensions (#1227)
1 parent 93cce88 commit 02d0726

20 files changed

+791
-366
lines changed

src/TestExplorer/LSPTestDiscovery.ts

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,12 @@ import * as vscode from "vscode";
1616
import * as TestDiscovery from "./TestDiscovery";
1717
import {
1818
LSPTestItem,
19-
textDocumentTestsRequest,
20-
workspaceTestsRequest,
21-
} from "../sourcekit-lsp/lspExtensions";
22-
import { InitializeResult, RequestType } from "vscode-languageclient/node";
19+
TextDocumentTestsRequest,
20+
WorkspaceTestsRequest,
21+
} from "../sourcekit-lsp/extensions";
2322
import { SwiftPackage, TargetType } from "../SwiftPackage";
24-
import { Converter } from "vscode-languageclient/lib/common/protocolConverter";
25-
26-
interface ILanguageClient {
27-
get initializeResult(): InitializeResult | undefined;
28-
get protocol2CodeConverter(): Converter;
29-
30-
sendRequest<P, R, E>(
31-
type: RequestType<P, R, E>,
32-
params: P,
33-
token?: vscode.CancellationToken
34-
): Promise<R>;
35-
}
36-
37-
interface ILanguageClientManager {
38-
useLanguageClient<Return>(process: {
39-
(client: ILanguageClient, cancellationToken: vscode.CancellationToken): Promise<Return>;
40-
}): Promise<Return>;
41-
}
23+
import { LanguageClientManager } from "../sourcekit-lsp/LanguageClientManager";
24+
import { LanguageClient } from "vscode-languageclient/node";
4225

4326
/**
4427
* Used to augment test discovery via `swift test --list-tests`.
@@ -49,7 +32,7 @@ interface ILanguageClientManager {
4932
* these results.
5033
*/
5134
export class LSPTestDiscovery {
52-
constructor(private languageClient: ILanguageClientManager) {}
35+
constructor(private languageClient: LanguageClientManager) {}
5336

5437
/**
5538
* Return a list of tests in the supplied document.
@@ -62,15 +45,15 @@ export class LSPTestDiscovery {
6245
return await this.languageClient.useLanguageClient(async (client, token) => {
6346
// Only use the lsp for this request if it supports the
6447
// textDocument/tests method, and is at least version 2.
65-
if (this.checkExperimentalCapability(client, textDocumentTestsRequest.method, 2)) {
48+
if (this.checkExperimentalCapability(client, TextDocumentTestsRequest.method, 2)) {
6649
const testsInDocument = await client.sendRequest(
67-
textDocumentTestsRequest,
50+
TextDocumentTestsRequest.type,
6851
{ textDocument: { uri: document.toString() } },
6952
token
7053
);
7154
return this.transformToTestClass(client, swiftPackage, testsInDocument);
7255
} else {
73-
throw new Error(`${textDocumentTestsRequest.method} requests not supported`);
56+
throw new Error(`${TextDocumentTestsRequest.method} requests not supported`);
7457
}
7558
});
7659
}
@@ -83,11 +66,11 @@ export class LSPTestDiscovery {
8366
return await this.languageClient.useLanguageClient(async (client, token) => {
8467
// Only use the lsp for this request if it supports the
8568
// workspace/tests method, and is at least version 2.
86-
if (this.checkExperimentalCapability(client, workspaceTestsRequest.method, 2)) {
87-
const tests = await client.sendRequest(workspaceTestsRequest, {}, token);
69+
if (this.checkExperimentalCapability(client, WorkspaceTestsRequest.method, 2)) {
70+
const tests = await client.sendRequest(WorkspaceTestsRequest.type, token);
8871
return this.transformToTestClass(client, swiftPackage, tests);
8972
} else {
90-
throw new Error(`${workspaceTestsRequest.method} requests not supported`);
73+
throw new Error(`${WorkspaceTestsRequest.method} requests not supported`);
9174
}
9275
});
9376
}
@@ -97,7 +80,7 @@ export class LSPTestDiscovery {
9780
* above the supplied `minVersion`.
9881
*/
9982
private checkExperimentalCapability(
100-
client: ILanguageClient,
83+
client: LanguageClient,
10184
method: string,
10285
minVersion: number
10386
) {
@@ -114,7 +97,7 @@ export class LSPTestDiscovery {
11497
* updating the format of the location.
11598
*/
11699
private transformToTestClass(
117-
client: ILanguageClient,
100+
client: LanguageClient,
118101
swiftPackage: SwiftPackage,
119102
input: LSPTestItem[]
120103
): TestDiscovery.TestClass[] {

src/TestExplorer/SPMTestDiscovery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import { TestStyle } from "../sourcekit-lsp/lspExtensions";
15+
import { TestStyle } from "../sourcekit-lsp/extensions";
1616
import { TestClass } from "./TestDiscovery";
1717

1818
/*

src/TestExplorer/TestDiscovery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import * as vscode from "vscode";
1616
import { SwiftPackage, TargetType } from "../SwiftPackage";
17-
import { LSPTestItem } from "../sourcekit-lsp/lspExtensions";
17+
import { LSPTestItem } from "../sourcekit-lsp/extensions";
1818
import { reduceTestItemChildren } from "./TestUtils";
1919

2020
/** Test class definition */

src/commands/reindexProject.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
import * as vscode from "vscode";
1616
import { WorkspaceContext } from "../WorkspaceContext";
17-
import { reindexProjectRequest } from "../sourcekit-lsp/lspExtensions";
17+
import { ReIndexProjectRequest } from "../sourcekit-lsp/extensions";
1818

1919
/**
2020
* Request that the SourceKit-LSP server reindexes the workspace.
2121
*/
2222
export function reindexProject(workspaceContext: WorkspaceContext): Promise<unknown> {
2323
return workspaceContext.languageClientManager.useLanguageClient(async (client, token) => {
2424
try {
25-
await client.sendRequest(reindexProjectRequest, {}, token);
25+
await client.sendRequest(ReIndexProjectRequest.type, token);
2626
const result = await vscode.window.showWarningMessage(
2727
"Re-indexing a project should never be necessary and indicates a bug in SourceKit-LSP. Please file an issue describing which symbol was out-of-date and how you got into the state.",
2828
"Report Issue",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2024 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import { LanguageClient, LanguageClientOptions, ServerOptions } from "vscode-languageclient/node";
16+
17+
/**
18+
* Used to create a {@link LanguageClient} for use in VS Code.
19+
*
20+
* This is primarily used to make unit testing easier so that we don't have to
21+
* mock out a constructor in the `vscode-languageclient` module.
22+
*/
23+
export class LanguageClientFactory {
24+
/**
25+
* Create a new {@link LanguageClient} for use in VS Code.
26+
*
27+
* @param name the human-readable name for the client
28+
* @param id the identifier for the client (used in settings)
29+
* @param serverOptions the {@link ServerOptions}
30+
* @param clientOptions the {@link LanguageClientOptions}
31+
* @returns the newly created {@link LanguageClient}
32+
*/
33+
createLanguageClient(
34+
name: string,
35+
id: string,
36+
serverOptions: ServerOptions,
37+
clientOptions: LanguageClientOptions
38+
): LanguageClient {
39+
return new LanguageClient(name, id, serverOptions, clientOptions);
40+
}
41+
}

0 commit comments

Comments
 (0)