Skip to content

Commit

Permalink
Migrate to upstream LSP inlay hints on the client side (#198)
Browse files Browse the repository at this point in the history
* Disable non-standard inlay hints for Swift >= 5.7.0

* Deprecated sourcekit-lsp.inlayHints.enabled

* Update deprecation message

Co-authored-by: Adam Fowler <adamfowler71@gmail.com>

Co-authored-by: Adam Fowler <adamfowler71@gmail.com>
  • Loading branch information
fwcd and adam-fowler authored Jul 8, 2022
1 parent e48e95c commit 3c5836a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
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");

0 comments on commit 3c5836a

Please sign in to comment.