提交 95f31949 编写于 作者: M Matt Bierner

Extract version dependent provider

上级 df7425c8
......@@ -6,10 +6,8 @@
import * as vscode from 'vscode';
import * as Proto from '../protocol';
import { ITypeScriptServiceClient } from '../typescriptService';
import API from '../utils/api';
import { disposeAll } from '../utils/dispose';
import * as typeConverters from '../utils/typeConverters';
import { VersionDependentRegistration } from '../utils/versionDependentRegistration';
class TypeScriptFoldingProvider implements vscode.FoldingRangeProvider {
public constructor(
......@@ -76,46 +74,16 @@ class TypeScriptFoldingProvider implements vscode.FoldingRangeProvider {
}
}
class FoldingProviderManager {
private registration: vscode.Disposable | undefined = undefined;
private readonly _disposables: vscode.Disposable[] = [];
constructor(
private readonly selector: vscode.DocumentSelector,
private readonly client: ITypeScriptServiceClient
) {
this.update(client.apiVersion);
this.client.onTsServerStarted(() => {
this.update(this.client.apiVersion);
}, null, this._disposables);
}
public dispose() {
disposeAll(this._disposables);
if (this.registration) {
this.registration.dispose();
this.registration = undefined;
}
}
private update(api: API) {
if (api.has280Features()) {
if (!this.registration) {
this.registration = vscode.languages.registerFoldingRangeProvider(this.selector, new TypeScriptFoldingProvider(this.client));
}
} else {
if (this.registration) {
this.registration.dispose();
this.registration = undefined;
}
}
}
}
export function register(
selector: vscode.DocumentSelector,
client: ITypeScriptServiceClient,
): vscode.Disposable {
return new FoldingProviderManager(selector, client);
return new VersionDependentRegistration(client, {
isSupportedVersion(api) {
return api.has280Features();
},
register() {
return vscode.languages.registerFoldingRangeProvider(selector, new TypeScriptFoldingProvider(client));
}
});
}
\ No newline at end of file
/*---------------------------------------------------------------------------------------------
* 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 { ITypeScriptServiceClient } from '../typescriptService';
import API from '../utils/api';
import { disposeAll } from '../utils/dispose';
export interface Delegate {
isSupportedVersion(api: API): boolean;
register(): vscode.Disposable;
}
export class VersionDependentRegistration {
private registration: vscode.Disposable | undefined = undefined;
private readonly _disposables: vscode.Disposable[] = [];
constructor(
private readonly client: ITypeScriptServiceClient,
private readonly delegate: Delegate,
) {
this.update(client.apiVersion);
this.client.onTsServerStarted(() => {
this.update(this.client.apiVersion);
}, null, this._disposables);
}
public dispose() {
disposeAll(this._disposables);
if (this.registration) {
this.registration.dispose();
this.registration = undefined;
}
}
private update(api: API) {
if (this.delegate.isSupportedVersion(api)) {
if (!this.registration) {
this.registration = this.delegate.register();
}
} else {
if (this.registration) {
this.registration.dispose();
this.registration = undefined;
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册