Skip to content

Commit

Permalink
Check in stubbed out smart select support for js/ts
Browse files Browse the repository at this point in the history
For microsoft/TypeScript#29071

This require upstream TS support. Check in experimental support so that TS team can test the ux of this feature
  • Loading branch information
mjbvz committed Apr 11, 2019
1 parent aaee57e commit f635233
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 12 additions & 0 deletions extensions/typescript-language-features/src/typescriptService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
}
Expand Down
1 change: 1 addition & 0 deletions extensions/typescript-language-features/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f635233

Please sign in to comment.