Check in stubbed out smart select support for js/ts

For https://github.com/Microsoft/TypeScript/issues/29071

This require upstream TS support. Check in experimental support so that TS team can test the ux of this feature
This commit is contained in:
Matt Bierner
2019-04-11 15:24:03 -07:00
parent aaee57e128
commit f635233740
4 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
/*---------------------------------------------------------------------------------------------
* 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 * as Proto from '../protocol';
import { ITypeScriptServiceClient } from '../typescriptService';
import API from '../utils/api';
import { VersionDependentRegistration } from '../utils/dependentRegistration';
import * as typeConverters from '../utils/typeConverters';
class SmartSelection implements vscode.SelectionRangeProvider {
public static readonly minVersion = API.v350;
public constructor(
private readonly client: ITypeScriptServiceClient
) { }
public async provideSelectionRanges(
document: vscode.TextDocument,
positions: vscode.Position[],
token: vscode.CancellationToken,
): Promise<vscode.SelectionRange[] | undefined> {
const file = this.client.toOpenedFilePath(document);
if (!file) {
return undefined;
}
const args: Proto.FileRequestArgs & { locations: Proto.Location[] } = {
file,
locations: positions.map(typeConverters.Position.toLocation)
};
const response = await this.client.execute('selectionRange', args, token);
if (response.type !== 'response' || !response.body) {
return undefined;
}
return response.body.map(SmartSelection.convertSelectionRange);
}
private static convertSelectionRange(
selectionRange: Proto.SelectionRange
): vscode.SelectionRange {
return new vscode.SelectionRange(
typeConverters.Range.fromTextSpan(selectionRange.textSpan),
selectionRange.parent ? SmartSelection.convertSelectionRange(selectionRange.parent) : undefined,
);
}
}
export function register(
selector: vscode.DocumentSelector,
client: ITypeScriptServiceClient,
) {
return new VersionDependentRegistration(client, SmartSelection.minVersion, () =>
vscode.languages.registerSelectionRangeProvider(selector, new SmartSelection(client)));
}

View File

@@ -73,6 +73,7 @@ export default class LanguageProvider extends Disposable {
this._register((await import('./features/references')).register(selector, this.client));
this._register((await import('./features/referencesCodeLens')).register(selector, this.description.id, this.client, cachedResponse));
this._register((await import('./features/rename')).register(selector, this.client, this.fileConfigurationManager));
this._register((await import('./features/smartSelect')).register(selector, this.client));
this._register((await import('./features/signatureHelp')).register(selector, this.client));
this._register((await import('./features/tagClosing')).register(selector, this.description.id, this.client));
this._register((await import('./features/typeDefinitions')).register(selector, this.client));

View File

@@ -11,6 +11,17 @@ import { TypeScriptServiceConfiguration } from './utils/configuration';
import Logger from './utils/logger';
import { PluginManager } from './utils/plugins';
declare module './protocol' {
interface SelectionRange {
textSpan: Proto.TextSpan;
parent?: SelectionRange;
}
interface SelectionRangeResponse extends Proto.Response {
body?: ReadonlyArray<SelectionRange>;
}
}
export namespace ServerResponse {
export class Cancelled {
@@ -54,6 +65,7 @@ export interface TypeScriptRequestTypes {
'quickinfo': [Proto.FileLocationRequestArgs, Proto.QuickInfoResponse];
'references': [Proto.FileLocationRequestArgs, Proto.ReferencesResponse];
'rename': [Proto.RenameRequestArgs, Proto.RenameResponse];
'selectionRange': [Proto.FileRequestArgs & { locations: Proto.Location[] }, Proto.SelectionRangeResponse];
'signatureHelp': [Proto.SignatureHelpRequestArgs, Proto.SignatureHelpResponse];
'typeDefinition': [Proto.FileLocationRequestArgs, Proto.TypeDefinitionResponse];
}

View File

@@ -36,6 +36,7 @@ export default class API {
public static readonly v330 = API.fromSimpleString('3.3.0');
public static readonly v333 = API.fromSimpleString('3.3.3');
public static readonly v340 = API.fromSimpleString('3.4.0');
public static readonly v350 = API.fromSimpleString('3.5.0');
public static fromVersionString(versionString: string): API {