Skip to content

Add supports for inline values as ts plugin #146608

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "MIT",
"description": "Dependencies shared by all extensions",
"dependencies": {
"typescript": "4.6.3"
"typescript": "4.6.3",
"ts-plugin-inline-values": "0.0.1"
},
"scripts": {
"postinstall": "node ./postinstall.mjs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { DocumentSelector } from '../utils/documentSelector';
import { ClientCapability, ITypeScriptServiceClient, ServerResponse, ExecConfig } from '../typescriptService';
import { conditionalRegistration, requireMinVersion, requireSomeCapability } from '../utils/dependentRegistration';
import API from '../utils/api';
import { Disposable } from '../utils/dispose';
import type * as ExtraProto from 'ts-plugin-inline-values/dist/proto';

namespace ExperimentalProto {
type InlineValueResponse = ExtraProto.InlineValuesResponse;

export interface IExtendedTypeScriptServiceClient {
execute<K extends keyof ExtendedTsServerRequests>(
command: K,
args: ExtendedTsServerRequests[K][0],
token: vscode.CancellationToken,
config?: ExecConfig
): Promise<ServerResponse.Response<ExtendedTsServerRequests[K][1]>>;
}

export interface ExtendedTsServerRequests {
'typescript/builtin/provideInlineValues': [ExtraProto.InlineValuesArgs, InlineValueResponse];
}
}

class TypeScriptInlineValuesProvider extends Disposable implements vscode.InlineValuesProvider {
public static readonly minVersion = API.v460;

constructor(
private readonly client: ITypeScriptServiceClient
) {
super();
}

async provideInlineValues(model: vscode.TextDocument, range: vscode.Range, context: vscode.InlineValueContext, token: vscode.CancellationToken): Promise<vscode.InlineValue[]> {
const filepath = this.client.toOpenedFilePath(model);
if (!filepath) {
return [];
}

const position = model.offsetAt(context.stoppedLocation.start);
const start = model.offsetAt(range.start);
const end = model.offsetAt(range.end);

const response = await (this.client as ExperimentalProto.IExtendedTypeScriptServiceClient).execute(
'typescript/builtin/provideInlineValues',
{
file: filepath,
position,
start: start,
length: end - start,
},
token
);
if (response.type !== 'response' || !response.success || !response.body) {
return [];
}

const results = response.body.map(value => {
if (value.type === 'EvaluatableExpression') {
return new vscode.InlineValueEvaluatableExpression(
new vscode.Range(
model.positionAt(value.start),
model.positionAt(value.start + value.length)
),
value.expression
);
} else {
return new vscode.InlineValueVariableLookup(
new vscode.Range(
model.positionAt(value.start),
model.positionAt(value.start + value.length)
),
value.variableName
);
}
});

return results;
}

}
export function register(
selector: DocumentSelector,
client: ITypeScriptServiceClient
) {
return conditionalRegistration([
requireMinVersion(client, TypeScriptInlineValuesProvider.minVersion),
requireSomeCapability(client, ClientCapability.Semantic),
], () => {
const provider = new TypeScriptInlineValuesProvider(client);
return vscode.languages.registerInlineValuesProvider(selector.semantic, provider);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default class LanguageProvider extends Disposable {
import('./languageFeatures/tagClosing').then(provider => this._register(provider.register(selector, this.description, this.client))),
import('./languageFeatures/typeDefinitions').then(provider => this._register(provider.register(selector, this.client))),
import('./languageFeatures/inlayHints').then(provider => this._register(provider.register(selector, this.description, this.client, this.fileConfigurationManager))),
import('./languageFeatures/inlineValues').then(provider => this._register(provider.register(selector, this.client)))
]);
}

Expand Down
9 changes: 9 additions & 0 deletions extensions/typescript-language-features/src/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ export class PluginManager extends Disposable {

private readPlugins() {
const pluginMap = new Map<string, ReadonlyArray<TypeScriptServerPlugin>>();
pluginMap.set('builtin:inline-values', [
{
name: 'ts-plugin-inline-values',
enableForWorkspaceTypeScriptVersions: true,
uri: vscode.Uri.parse('builtin://ts-plugin-inline-values'),
languages: []
}
]);

for (const extension of vscode.extensions.all) {
const pack = extension.packageJSON;
if (pack.contributes && Array.isArray(pack.contributes.typescriptServerPlugins)) {
Expand Down
5 changes: 5 additions & 0 deletions extensions/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ node-gyp-build@^4.3.0:
resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.3.0.tgz#9f256b03e5826150be39c764bf51e993946d71a3"
integrity sha512-iWjXZvmboq0ja1pUGULQBexmxq8CV4xBhX7VDOTbL7ZR4FOowwY/VOtRxBN/yKxmdGoIp4j5ysNT4u3S2pDQ3Q==

ts-plugin-inline-values@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/ts-plugin-inline-values/-/ts-plugin-inline-values-0.0.1.tgz#e4271f2fcab73793511ad4519f8e17e056ecf3d5"
integrity sha512-pSqFjzRNmkdSvcNfz3Jt3N7KeP7bzeWbTluYKAC7BXCnwWcstrYoVq6h6hf59TgNB4Q7zDMXyzKcuswRWPcpMA==

typescript@4.6.3:
version "4.6.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.3.tgz#eefeafa6afdd31d725584c67a0eaba80f6fc6c6c"
Expand Down