Prototype experimental folding API for TS/JS

#44158
This commit is contained in:
Matt Bierner
2018-02-22 16:06:57 -08:00
parent afa0f1814d
commit dcf6559cc1
2 changed files with 67 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
/*---------------------------------------------------------------------------------------------
* 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';
// TODO: forward declarations for private TS API.
interface TextSpan {
start: number;
length: number;
}
interface OutliningSpan {
textSpan: TextSpan;
hintSpan: TextSpan;
bannerText: string;
autoCollapse: boolean;
}
interface OutliningSpansRequestArgs extends Proto.FileRequestArgs { }
interface OutliningSpansResponse extends Proto.Response {
body?: OutliningSpan[];
}
export default class TypeScriptFoldingProvider implements vscode.FoldingProvider {
public constructor(
private readonly client: ITypeScriptServiceClient
) { }
async provideFoldingRanges(
document: vscode.TextDocument,
token: vscode.CancellationToken
): Promise<vscode.FoldingRangeList | undefined> {
if (!this.client.apiVersion.has270Features()) {
return;
}
const file = this.client.normalizePath(document.uri);
if (!file) {
return;
}
const args: OutliningSpansRequestArgs = { file };
const response: OutliningSpansResponse = await this.client.execute('outliningSpans', args, token);
if (!response || !response.body) {
return;
}
return new vscode.FoldingRangeList(response.body.map(span => {
const start = document.positionAt(span.textSpan.start);
const end = document.positionAt(span.textSpan.start + span.textSpan.length);
return new vscode.FoldingRange(start.line, end.line);
}));
}
}

View File

@@ -125,6 +125,11 @@ export default class LanguageProvider {
this.disposables.push(languages.registerRenameProvider(selector, new (await import('./features/renameProvider')).default(client)));
this.disposables.push(languages.registerCodeActionsProvider(selector, new (await import('./features/quickFixProvider')).default(client, this.formattingOptionsManager, commandManager, this.diagnosticsManager)));
this.disposables.push(languages.registerCodeActionsProvider(selector, new (await import('./features/refactorProvider')).default(client, this.formattingOptionsManager, commandManager)));
if (workspace.getConfiguration('typescript').get('enableExperimentalFolding', false)) {
this.disposables.push(languages.registerFoldingProvider(selector, new (await import('./features/folderingProvider')).default(client)));
}
this.registerVersionDependentProviders();
const cachedResponse = new CachedNavTreeResponse();