mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-04 15:25:47 +01:00
Extract version dependent provider
This commit is contained in:
@@ -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));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user