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

Migrate to upstream workspace/textDocumentContent #1027

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
113 changes: 62 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@
"dependencies": {
"lcov-parse": "^1.0.0",
"plist": "^3.1.0",
"vscode-languageclient": "^9.0.1",
"vscode-languageclient": "^10.0.0-next.13",
"xml2js": "^0.6.2"
}
}
}
2 changes: 1 addition & 1 deletion src/TestExplorer/LSPTestDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from "../sourcekit-lsp/lspExtensions";
import { InitializeResult, RequestType } from "vscode-languageclient/node";
import { SwiftPackage, TargetType } from "../SwiftPackage";
import { Converter } from "vscode-languageclient/lib/common/protocolConverter";
import { Converter } from "vscode-languageclient/$test/common/protocolConverter";

interface ILanguageClient {
get initializeResult(): InitializeResult | undefined;
Expand Down
38 changes: 25 additions & 13 deletions src/sourcekit-lsp/LanguageClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { DiagnosticsManager } from "../DiagnosticsManager";
import { LSPLogger, LSPOutputChannel } from "./LSPOutputChannel";
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel";
import { promptForDiagnostics } from "../commands/captureDiagnostics";
import { activateGetReferenceDocument } from "./getReferenceDocument";
import { activateLegacyGetReferenceDocument } from "./getReferenceDocument";

interface SourceKitLogMessageParams extends langclient.LogMessageParams {
logName?: string;
Expand Down Expand Up @@ -113,7 +113,7 @@ export class LanguageClientManager {
private cancellationToken?: vscode.CancellationTokenSource;
private legacyInlayHints?: vscode.Disposable;
private peekDocuments?: vscode.Disposable;
private getReferenceDocument?: vscode.Disposable;
private legacyGetReferenceDocument?: vscode.Disposable;
private restartedPromise?: Promise<void>;
private currentWorkspaceFolder?: vscode.Uri;
private waitingOnRestartCount: number;
Expand Down Expand Up @@ -251,7 +251,7 @@ export class LanguageClientManager {
this.cancellationToken?.dispose();
this.legacyInlayHints?.dispose();
this.peekDocuments?.dispose();
this.getReferenceDocument?.dispose();
this.legacyGetReferenceDocument?.dispose();
this.subscriptions.forEach(item => item.dispose());
this.languageClient?.stop();
this.namedOutputChannels.forEach(channel => channel.dispose());
Expand Down Expand Up @@ -402,8 +402,8 @@ export class LanguageClientManager {
this.legacyInlayHints = undefined;
this.peekDocuments?.dispose();
this.peekDocuments = undefined;
this.getReferenceDocument?.dispose();
this.getReferenceDocument = undefined;
this.legacyGetReferenceDocument?.dispose();
this.legacyGetReferenceDocument = undefined;
if (client) {
this.cancellationToken?.cancel();
this.cancellationToken?.dispose();
Expand Down Expand Up @@ -581,13 +581,19 @@ export class LanguageClientManager {
initializationOptions: this.initializationOptions(),
};

const client = new langclient.LanguageClient(
"swift.sourcekit-lsp",
"SourceKit Language Server",
serverOptions,
clientOptions
);

// TODO: Remove once LSP 3.18 is official (we currently need this for
// the proposed `workspace/textDocumentContent` API)
client.registerProposedFeatures();

return {
client: new langclient.LanguageClient(
"swift.sourcekit-lsp",
"SourceKit Language Server",
serverOptions,
clientOptions
),
client,
errorHandler,
};
}
Expand Down Expand Up @@ -658,9 +664,15 @@ export class LanguageClientManager {
this.legacyInlayHints = activateLegacyInlayHints(client);
}

// TODO: This may have to be adjusted to the version
// https://github.com/swiftlang/sourcekit-lsp/pull/1639
// is merged to
if (this.workspaceContext.swiftVersion.isLessThan(new Version(6, 1, 0))) {
this.legacyGetReferenceDocument = activateLegacyGetReferenceDocument(client);
this.workspaceContext.subscriptions.push(this.legacyGetReferenceDocument);
}

this.peekDocuments = activatePeekDocuments(client);
this.getReferenceDocument = activateGetReferenceDocument(client);
this.workspaceContext.subscriptions.push(this.getReferenceDocument);
})
.catch(reason => {
this.workspaceContext.outputChannel.log(`${reason}`);
Expand Down
17 changes: 13 additions & 4 deletions src/sourcekit-lsp/getReferenceDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@

import * as vscode from "vscode";
import * as langclient from "vscode-languageclient/node";
import { GetReferenceDocumentParams, GetReferenceDocumentRequest } from "./lspExtensions";
import {
LegacyGetReferenceDocumentParams,
LegacyGetReferenceDocumentRequest,
} from "./lspExtensions";

export function activateGetReferenceDocument(client: langclient.LanguageClient): vscode.Disposable {
export function activateLegacyGetReferenceDocument(
client: langclient.LanguageClient
): vscode.Disposable {
const getReferenceDocument = vscode.workspace.registerTextDocumentContentProvider(
"sourcekit-lsp",
{
provideTextDocumentContent: async (uri, token) => {
const params: GetReferenceDocumentParams = {
const params: LegacyGetReferenceDocumentParams = {
uri: client.code2ProtocolConverter.asUri(uri),
};

const result = await client.sendRequest(GetReferenceDocumentRequest, params, token);
const result = await client.sendRequest(
LegacyGetReferenceDocumentRequest,
params,
token
);

if (result) {
return result.content;
Expand Down
10 changes: 5 additions & 5 deletions src/sourcekit-lsp/lspExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const PeekDocumentsRequest = new langclient.RequestType<
>("workspace/peekDocuments");

// Get Reference Document
export interface GetReferenceDocumentParams {
export interface LegacyGetReferenceDocumentParams {
/**
* The `DocumentUri` of the custom scheme url for which content is required
*/
Expand All @@ -67,17 +67,17 @@ export interface GetReferenceDocumentParams {
/**
* Response containing `content` of `GetReferenceDocumentRequest`
*/
export interface GetReferenceDocumentResult {
export interface LegacyGetReferenceDocumentResult {
content: string;
}

/**
* Request from the client to the server asking for contents of a URI having a custom scheme
* For example: "sourcekit-lsp:"
*/
export const GetReferenceDocumentRequest = new langclient.RequestType<
GetReferenceDocumentParams,
GetReferenceDocumentResult,
export const LegacyGetReferenceDocumentRequest = new langclient.RequestType<
LegacyGetReferenceDocumentParams,
LegacyGetReferenceDocumentResult,
unknown
>("workspace/getReferenceDocument");

Expand Down
2 changes: 1 addition & 1 deletion test/suite/BuildFlags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//

import * as assert from "assert";
import { strict as assert } from "assert";
import * as vscode from "vscode";
import { SwiftToolchain } from "../../src/toolchain/toolchain";
import { ArgumentFilter, BuildFlags } from "../../src/toolchain/BuildFlags";
Expand Down
2 changes: 1 addition & 1 deletion test/suite/DiagnosticsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//

import * as assert from "assert";
import { strict as assert } from "assert";
import * as vscode from "vscode";
import { SwiftToolchain } from "../../src/toolchain/toolchain";
import { executeTaskAndWaitForResult, waitForNoRunningTasks } from "../utilities";
Expand Down
Loading