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 LSP inlay hints on the client side #198

Merged
merged 3 commits into from
Jul 8, 2022
Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@
"sourcekit-lsp.inlayHints.enabled": {
"type": "boolean",
"default": true,
"description": "Render inlay type annotations in the editor. Inlay hints require Swift 5.6 or later.",
"description": "Render inlay type annotations in the editor. Inlay hints require Swift 5.6.",
"markdownDeprecationMessage": "**Deprecated**: Please use `#editor.inlayHints.enabled#` instead.",
"order": 3
},
"sourcekit-lsp.trace.server": {
Expand Down
14 changes: 8 additions & 6 deletions src/sourcekit-lsp/LanguageClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from "../utilities/utilities";
import { Version } from "../utilities/version";
import { FolderEvent, WorkspaceContext } from "../WorkspaceContext";
import { activateInlayHints } from "./inlayHints";
import { activateLegacyInlayHints } from "./inlayHints";
import { FolderContext } from "../FolderContext";

/** Manages the creation and destruction of Language clients as we move between
Expand Down Expand Up @@ -69,7 +69,7 @@ export class LanguageClientManager {
private onDidCreateFileDisposable: vscode.Disposable;
private onDidDeleteFileDisposable: vscode.Disposable;
private onChangeConfig: vscode.Disposable;
private inlayHints?: vscode.Disposable;
private legacyInlayHints?: vscode.Disposable;
private supportsDidChangedWatchedFiles: boolean;
private restartedPromise?: Promise<void>;
private currentWorkspaceFolder?: vscode.Uri;
Expand Down Expand Up @@ -144,7 +144,7 @@ export class LanguageClientManager {
this.onDidCreateFileDisposable?.dispose();
this.onDidDeleteFileDisposable?.dispose();
this.onChangeConfig.dispose();
this.inlayHints?.dispose();
this.legacyInlayHints?.dispose();
this.languageClient?.stop();
}

Expand Down Expand Up @@ -183,8 +183,8 @@ export class LanguageClientManager {
// language client is set to null while it is in the process of restarting
this.languageClient = null;
this.currentWorkspaceFolder = uri;
this.inlayHints?.dispose();
this.inlayHints = undefined;
this.legacyInlayHints?.dispose();
this.legacyInlayHints = undefined;
if (client) {
this.cancellationToken?.cancel();
this.cancellationToken?.dispose();
Expand Down Expand Up @@ -336,7 +336,9 @@ export class LanguageClientManager {
this.clientReadyPromise = client
.start()
.then(() => {
this.inlayHints = activateInlayHints(client);
if (this.workspaceContext.swiftVersion.isLessThan(new Version(5, 7, 0))) {
this.legacyInlayHints = activateLegacyInlayHints(client);
}
})
.catch(reason => {
this.workspaceContext.outputChannel.log(`${reason}`);
Expand Down
10 changes: 5 additions & 5 deletions src/sourcekit-lsp/inlayHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import * as vscode from "vscode";
import * as langclient from "vscode-languageclient/node";
import configuration from "../configuration";
import { LanguageClientManager } from "./LanguageClientManager";
import { inlayHintsRequest } from "./lspExtensions";
import { legacyInlayHintsRequest } from "./lspExtensions";

/** Provide Inlay Hints using sourcekit-lsp */
class SwiftInlayHintsProvider implements vscode.InlayHintsProvider {
class SwiftLegacyInlayHintsProvider implements vscode.InlayHintsProvider {
onDidChangeInlayHints?: vscode.Event<void> | undefined;

constructor(private client: langclient.LanguageClient) {}
Expand All @@ -37,7 +37,7 @@ class SwiftInlayHintsProvider implements vscode.InlayHintsProvider {
textDocument: this.client.code2ProtocolConverter.asTextDocumentIdentifier(document),
range: { start: range.start, end: range.end },
};
const result = this.client.sendRequest(inlayHintsRequest, params, token);
const result = this.client.sendRequest(legacyInlayHintsRequest, params, token);
return result.then(
hints => {
return hints.map(hint => {
Expand Down Expand Up @@ -66,10 +66,10 @@ class SwiftInlayHintsProvider implements vscode.InlayHintsProvider {
}

/** activate the inlay hints */
export function activateInlayHints(client: langclient.LanguageClient): vscode.Disposable {
export function activateLegacyInlayHints(client: langclient.LanguageClient): vscode.Disposable {
const inlayHint = vscode.languages.registerInlayHintsProvider(
LanguageClientManager.documentSelector,
new SwiftInlayHintsProvider(client)
new SwiftLegacyInlayHintsProvider(client)
);

return inlayHint;
Expand Down
12 changes: 7 additions & 5 deletions src/sourcekit-lsp/lspExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as langclient from "vscode-languageclient/node";

// Definitions for non-standard requests used by sourcekit-lsp

export interface InlayHintsParams {
export interface LegacyInlayHintsParams {
/**
* The text document.
*/
Expand All @@ -36,7 +36,7 @@ export interface InlayHintsParams {
only?: string[];
}

export interface InlayHint {
export interface LegacyInlayHint {
/**
* The position within the code that this hint is
* attached to.
Expand All @@ -55,6 +55,8 @@ export interface InlayHint {
label: string;
}

export const inlayHintsRequest = new langclient.RequestType<InlayHintsParams, InlayHint[], unknown>(
"sourcekit-lsp/inlayHints"
);
export const legacyInlayHintsRequest = new langclient.RequestType<
LegacyInlayHintsParams,
LegacyInlayHint[],
unknown
>("sourcekit-lsp/inlayHints");