Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize LSP extensions #1227

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 14 additions & 31 deletions src/TestExplorer/LSPTestDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,12 @@ import * as vscode from "vscode";
import * as TestDiscovery from "./TestDiscovery";
import {
LSPTestItem,
textDocumentTestsRequest,
workspaceTestsRequest,
} from "../sourcekit-lsp/lspExtensions";
import { InitializeResult, RequestType } from "vscode-languageclient/node";
TextDocumentTestsRequest,
WorkspaceTestsRequest,
} from "../sourcekit-lsp/extensions";
import { SwiftPackage, TargetType } from "../SwiftPackage";
import { Converter } from "vscode-languageclient/lib/common/protocolConverter";

interface ILanguageClient {
get initializeResult(): InitializeResult | undefined;
get protocol2CodeConverter(): Converter;

sendRequest<P, R, E>(
type: RequestType<P, R, E>,
params: P,
token?: vscode.CancellationToken
): Promise<R>;
}

interface ILanguageClientManager {
useLanguageClient<Return>(process: {
(client: ILanguageClient, cancellationToken: vscode.CancellationToken): Promise<Return>;
}): Promise<Return>;
}
import { LanguageClientManager } from "../sourcekit-lsp/LanguageClientManager";
import { LanguageClient } from "vscode-languageclient/node";

/**
* Used to augment test discovery via `swift test --list-tests`.
Expand All @@ -49,7 +32,7 @@ interface ILanguageClientManager {
* these results.
*/
export class LSPTestDiscovery {
constructor(private languageClient: ILanguageClientManager) {}
constructor(private languageClient: LanguageClientManager) {}

/**
* Return a list of tests in the supplied document.
Expand All @@ -62,15 +45,15 @@ export class LSPTestDiscovery {
return await this.languageClient.useLanguageClient(async (client, token) => {
// Only use the lsp for this request if it supports the
// textDocument/tests method, and is at least version 2.
if (this.checkExperimentalCapability(client, textDocumentTestsRequest.method, 2)) {
if (this.checkExperimentalCapability(client, TextDocumentTestsRequest.method, 2)) {
const testsInDocument = await client.sendRequest(
textDocumentTestsRequest,
TextDocumentTestsRequest.type,
{ textDocument: { uri: document.toString() } },
token
);
return this.transformToTestClass(client, swiftPackage, testsInDocument);
} else {
throw new Error(`${textDocumentTestsRequest.method} requests not supported`);
throw new Error(`${TextDocumentTestsRequest.method} requests not supported`);
}
});
}
Expand All @@ -83,11 +66,11 @@ export class LSPTestDiscovery {
return await this.languageClient.useLanguageClient(async (client, token) => {
// Only use the lsp for this request if it supports the
// workspace/tests method, and is at least version 2.
if (this.checkExperimentalCapability(client, workspaceTestsRequest.method, 2)) {
const tests = await client.sendRequest(workspaceTestsRequest, {}, token);
if (this.checkExperimentalCapability(client, WorkspaceTestsRequest.method, 2)) {
const tests = await client.sendRequest(WorkspaceTestsRequest.type, token);
return this.transformToTestClass(client, swiftPackage, tests);
} else {
throw new Error(`${workspaceTestsRequest.method} requests not supported`);
throw new Error(`${WorkspaceTestsRequest.method} requests not supported`);
}
});
}
Expand All @@ -97,7 +80,7 @@ export class LSPTestDiscovery {
* above the supplied `minVersion`.
*/
private checkExperimentalCapability(
client: ILanguageClient,
client: LanguageClient,
method: string,
minVersion: number
) {
Expand All @@ -114,7 +97,7 @@ export class LSPTestDiscovery {
* updating the format of the location.
*/
private transformToTestClass(
client: ILanguageClient,
client: LanguageClient,
swiftPackage: SwiftPackage,
input: LSPTestItem[]
): TestDiscovery.TestClass[] {
Expand Down
2 changes: 1 addition & 1 deletion src/TestExplorer/SPMTestDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//

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

/*
Expand Down
2 changes: 1 addition & 1 deletion src/TestExplorer/TestDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as vscode from "vscode";
import { SwiftPackage, TargetType } from "../SwiftPackage";
import { LSPTestItem } from "../sourcekit-lsp/lspExtensions";
import { LSPTestItem } from "../sourcekit-lsp/extensions";
import { reduceTestItemChildren } from "./TestUtils";

/** Test class definition */
Expand Down
4 changes: 2 additions & 2 deletions src/commands/reindexProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

import * as vscode from "vscode";
import { WorkspaceContext } from "../WorkspaceContext";
import { reindexProjectRequest } from "../sourcekit-lsp/lspExtensions";
import { ReIndexProjectRequest } from "../sourcekit-lsp/extensions";

/**
* Request that the SourceKit-LSP server reindexes the workspace.
*/
export function reindexProject(workspaceContext: WorkspaceContext): Promise<unknown> {
return workspaceContext.languageClientManager.useLanguageClient(async (client, token) => {
try {
await client.sendRequest(reindexProjectRequest, {}, token);
await client.sendRequest(ReIndexProjectRequest.type, token);
const result = await vscode.window.showWarningMessage(
"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.",
"Report Issue",
Expand Down
41 changes: 41 additions & 0 deletions src/sourcekit-lsp/LanguageClientFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import { LanguageClient, LanguageClientOptions, ServerOptions } from "vscode-languageclient/node";

/**
* Used to create a {@link LanguageClient} for use in VS Code.
*
* This is primarily used to make unit testing easier so that we don't have to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome :)

* mock out a constructor in the `vscode-languageclient` module.
*/
export class LanguageClientFactory {
/**
* Create a new {@link LanguageClient} for use in VS Code.
*
* @param name the human-readable name for the client
* @param id the identifier for the client (used in settings)
* @param serverOptions the {@link ServerOptions}
* @param clientOptions the {@link LanguageClientOptions}
* @returns the newly created {@link LanguageClient}
*/
createLanguageClient(
name: string,
id: string,
serverOptions: ServerOptions,
clientOptions: LanguageClientOptions
): LanguageClient {
return new LanguageClient(name, id, serverOptions, clientOptions);
}
}
Loading