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

feat: support inlay hints #452

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
@@ -1,5 +1,6 @@
{
"private": true,
"enableProposedApi": true,
"name": "volar",
"displayName": "Volar",
"description": "Language support for Vue 3",
Expand All @@ -12,7 +13,7 @@
"url": "https://github.com/johnsoncodehk/volar.git"
},
"engines": {
"vscode": "^1.58.0"
"vscode": "^1.60.0"
},
"keywords": [
"volar",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Expand Up @@ -25,6 +25,6 @@
"vscode-nls": "5.0.0"
},
"devDependencies": {
"@types/vscode": "^1.58.1"
"@types/vscode": "^1.60.0"
}
}
2 changes: 2 additions & 0 deletions packages/client/src/extension.ts
Expand Up @@ -9,6 +9,7 @@ import * as createWorkspaceSnippets from './features/createWorkspaceSnippets';
import * as documentVersion from './features/documentVersion';
import * as documentContent from './features/documentContent';
import * as documentPrintWidth from './features/documentPrintWidth';
import * as inlayHints from './features/inlayHints';
import * as preview from './features/preview';
import * as showReferences from './features/showReferences';
import * as splitEditors from './features/splitEditors';
Expand Down Expand Up @@ -85,6 +86,7 @@ export async function activate(context: vscode.ExtensionContext) {
preview.activate(context);
createWorkspaceSnippets.activate(context);
callGraph.activate(context, apiClient);
inlayHints.activate(context, docClient ?? apiClient);
verifyAll.activate(context, docClient ?? apiClient);
virtualFiles.activate(context, docClient ?? apiClient);
tagClosing.activate(context, htmlClient, apiClient);
Expand Down
48 changes: 48 additions & 0 deletions packages/client/src/features/inlayHints.ts
@@ -0,0 +1,48 @@
import * as vscode from 'vscode';
import * as shared from '@volar/shared';
import { LanguageClient } from 'vscode-languageclient/node';

class TypeScriptInlayHintsProvider implements vscode.InlayHintsProvider {

constructor(
private readonly client: LanguageClient,
) { }

async provideInlayHints(model: vscode.TextDocument, range: vscode.Range, _token: vscode.CancellationToken): Promise<vscode.InlayHint[]> {

const inlayHints = await this.client.sendRequest(shared.GetInlayHintsRequest.type, {
textDocument: this.client.code2ProtocolConverter.asTextDocumentIdentifier(model),
range: this.client.code2ProtocolConverter.asRange(range),
});

if (!inlayHints)
return [];

return inlayHints.map(hint => {
const result = new vscode.InlayHint(
hint.text,
this.client.protocol2CodeConverter.asPosition(hint.position),
hint.kind,
);
result.whitespaceBefore = hint.whitespaceBefore;
result.whitespaceAfter = hint.whitespaceAfter;
return result;
});
}
}

function register(
selector: vscode.DocumentSelector,
client: LanguageClient,
) {
return vscode.languages.registerInlayHintsProvider(selector,
new TypeScriptInlayHintsProvider(client));
}

export async function activate(
context: vscode.ExtensionContext,
client: LanguageClient,
) {
await client.onReady();
context.subscriptions.push(register([{ scheme: 'file', language: 'vue' }], client))
}