mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-23 10:08:49 +01:00
Use ConfigurationDependentRegistration to remove more boilerplate code
This commit is contained in:
@@ -52,7 +52,6 @@ export class CachedNavTreeResponse {
|
||||
}
|
||||
|
||||
export abstract class TypeScriptBaseCodeLensProvider implements CodeLensProvider {
|
||||
private enabled: boolean = true;
|
||||
private onDidChangeCodeLensesEmitter = new EventEmitter<void>();
|
||||
|
||||
public constructor(
|
||||
@@ -64,18 +63,7 @@ export abstract class TypeScriptBaseCodeLensProvider implements CodeLensProvider
|
||||
return this.onDidChangeCodeLensesEmitter.event;
|
||||
}
|
||||
|
||||
protected setEnabled(enabled: false): void {
|
||||
if (this.enabled !== enabled) {
|
||||
this.enabled = enabled;
|
||||
this.onDidChangeCodeLensesEmitter.fire();
|
||||
}
|
||||
}
|
||||
|
||||
async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
|
||||
if (!this.enabled) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const filepath = this.client.normalizePath(document.uri);
|
||||
if (!filepath) {
|
||||
return [];
|
||||
|
||||
@@ -8,33 +8,14 @@ import * as nls from 'vscode-nls';
|
||||
import * as Proto from '../protocol';
|
||||
import * as PConst from '../protocol.const';
|
||||
import { ITypeScriptServiceClient } from '../typescriptService';
|
||||
import { VersionDependentRegistration } from '../utils/dependentRegistration';
|
||||
import API from '../utils/api';
|
||||
import { ConfigurationDependentRegistration, VersionDependentRegistration } from '../utils/dependentRegistration';
|
||||
import * as typeConverters from '../utils/typeConverters';
|
||||
import { CachedNavTreeResponse, ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
|
||||
import { disposeAll } from '../utils/dispose';
|
||||
import API from '../utils/api';
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
export default class TypeScriptImplementationsCodeLensProvider extends TypeScriptBaseCodeLensProvider {
|
||||
|
||||
private readonly _disposables: vscode.Disposable[] = [];
|
||||
|
||||
public constructor(
|
||||
client: ITypeScriptServiceClient,
|
||||
private readonly language: string,
|
||||
cachedResponse: CachedNavTreeResponse
|
||||
) {
|
||||
super(client, cachedResponse);
|
||||
|
||||
this.updateConfiguration();
|
||||
|
||||
vscode.workspace.onDidChangeConfiguration(() => this.updateConfiguration(), null, this._disposables);
|
||||
}
|
||||
|
||||
public dispose() {
|
||||
disposeAll(this._disposables);
|
||||
}
|
||||
|
||||
public resolveCodeLens(inputCodeLens: vscode.CodeLens, token: vscode.CancellationToken): Promise<vscode.CodeLens> {
|
||||
const codeLens = inputCodeLens as ReferencesCodeLens;
|
||||
const args = typeConverters.Position.toFileLocationRequestArgs(codeLens.file, codeLens.range.start);
|
||||
@@ -96,25 +77,17 @@ export default class TypeScriptImplementationsCodeLensProvider extends TypeScrip
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private updateConfiguration(): void {
|
||||
const config = vscode.workspace.getConfiguration(this.language);
|
||||
this.setEnabled(config.get('implementationsCodeLens.enabled', false));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function register(
|
||||
selector: vscode.DocumentSelector,
|
||||
modeId: string,
|
||||
client: ITypeScriptServiceClient,
|
||||
cachedResponse: CachedNavTreeResponse,
|
||||
) {
|
||||
return new VersionDependentRegistration(client, API.v220, () => {
|
||||
const provider = new TypeScriptImplementationsCodeLensProvider(client, modeId, cachedResponse);
|
||||
return vscode.Disposable.from(
|
||||
vscode.languages.registerCodeLensProvider(selector, provider),
|
||||
provider,
|
||||
);
|
||||
});
|
||||
return new VersionDependentRegistration(client, API.v220, () =>
|
||||
new ConfigurationDependentRegistration(modeId, 'implementationsCodeLens.enabled', () => {
|
||||
return vscode.languages.registerCodeLensProvider(selector,
|
||||
new TypeScriptImplementationsCodeLensProvider(client, cachedResponse));
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -3,27 +3,21 @@
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { Position, Range, CompletionItemProvider, CompletionItemKind, TextDocument, CancellationToken, CompletionItem, window, Uri, TextEditor, SnippetString, workspace, DocumentSelector, languages, Disposable } from 'vscode';
|
||||
|
||||
import { ITypeScriptServiceClient } from '../typescriptService';
|
||||
import * as Proto from '../protocol';
|
||||
|
||||
import { CancellationToken, CompletionItem, CompletionItemKind, CompletionItemProvider, Disposable, DocumentSelector, languages, Position, Range, SnippetString, TextDocument, TextEditor, Uri, window } from 'vscode';
|
||||
import * as nls from 'vscode-nls';
|
||||
import * as typeConverters from '../utils/typeConverters';
|
||||
import * as Proto from '../protocol';
|
||||
import { ITypeScriptServiceClient } from '../typescriptService';
|
||||
import { Command, CommandManager } from '../utils/commandManager';
|
||||
import { ConfigurationDependentRegistration } from '../utils/dependentRegistration';
|
||||
import * as typeConverters from '../utils/typeConverters';
|
||||
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
const configurationNamespace = 'jsDocCompletion';
|
||||
|
||||
namespace Configuration {
|
||||
export const enabled = 'enabled';
|
||||
}
|
||||
|
||||
class JsDocCompletionItem extends CompletionItem {
|
||||
constructor(
|
||||
document: TextDocument,
|
||||
position: Position,
|
||||
shouldGetJSDocFromTSServer: boolean,
|
||||
position: Position
|
||||
) {
|
||||
super('/** */', CompletionItemKind.Snippet);
|
||||
this.detail = localize('typescript.jsDocCompletionItem.documentation', 'JSDoc comment');
|
||||
@@ -41,7 +35,7 @@ class JsDocCompletionItem extends CompletionItem {
|
||||
this.command = {
|
||||
title: 'Try Complete JSDoc',
|
||||
command: TryCompleteJsDocCommand.COMMAND_NAME,
|
||||
arguments: [document.uri, start, shouldGetJSDocFromTSServer]
|
||||
arguments: [document.uri, start]
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -65,12 +59,6 @@ class JsDocCompletionProvider implements CompletionItemProvider {
|
||||
return [];
|
||||
}
|
||||
|
||||
// TODO: unregister provider when disabled
|
||||
const enableJsDocCompletions = workspace.getConfiguration(configurationNamespace, document.uri).get<boolean>(Configuration.enabled, true);
|
||||
if (!enableJsDocCompletions) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!this.isValidCursorPosition(document, position)) {
|
||||
return [];
|
||||
}
|
||||
@@ -79,7 +67,7 @@ class JsDocCompletionProvider implements CompletionItemProvider {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [new JsDocCompletionItem(document, position, enableJsDocCompletions)];
|
||||
return [new JsDocCompletionItem(document, position)];
|
||||
}
|
||||
|
||||
private async isCommentableLocation(
|
||||
@@ -141,7 +129,7 @@ class TryCompleteJsDocCommand implements Command {
|
||||
* Try to insert a jsdoc comment, using a template provide by typescript
|
||||
* if possible, otherwise falling back to a default comment format.
|
||||
*/
|
||||
public async execute(resource: Uri, start: Position, shouldGetJSDocFromTSServer: boolean): Promise<boolean> {
|
||||
public async execute(resource: Uri, start: Position): Promise<boolean> {
|
||||
const file = this.client.normalizePath(resource);
|
||||
if (!file) {
|
||||
return false;
|
||||
@@ -152,14 +140,11 @@ class TryCompleteJsDocCommand implements Command {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!shouldGetJSDocFromTSServer) {
|
||||
return this.tryInsertDefaultDoc(editor, start);
|
||||
}
|
||||
|
||||
const didInsertFromTemplate = await this.tryInsertJsDocFromTemplate(editor, file, start);
|
||||
if (didInsertFromTemplate) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.tryInsertDefaultDoc(editor, start);
|
||||
}
|
||||
|
||||
@@ -227,7 +212,9 @@ export function register(
|
||||
client: ITypeScriptServiceClient,
|
||||
commandManager: CommandManager
|
||||
): Disposable {
|
||||
return languages.registerCompletionItemProvider(selector,
|
||||
new JsDocCompletionProvider(client, commandManager),
|
||||
'*');
|
||||
return new ConfigurationDependentRegistration('jsDocCompletion', 'enabled', () => {
|
||||
return languages.registerCompletionItemProvider(selector,
|
||||
new JsDocCompletionProvider(client, commandManager),
|
||||
'*');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -8,31 +8,14 @@ import * as nls from 'vscode-nls';
|
||||
import * as Proto from '../protocol';
|
||||
import * as PConst from '../protocol.const';
|
||||
import { ITypeScriptServiceClient } from '../typescriptService';
|
||||
import API from '../utils/api';
|
||||
import { ConfigurationDependentRegistration, VersionDependentRegistration } from '../utils/dependentRegistration';
|
||||
import * as typeConverters from '../utils/typeConverters';
|
||||
import { CachedNavTreeResponse, ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
|
||||
import { disposeAll } from '../utils/dispose';
|
||||
import { VersionDependentRegistration } from '../utils/dependentRegistration';
|
||||
import API from '../utils/api';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLensProvider {
|
||||
private readonly _disposables: vscode.Disposable[] = [];
|
||||
|
||||
public constructor(
|
||||
client: ITypeScriptServiceClient,
|
||||
private readonly language: string,
|
||||
cachedResponse: CachedNavTreeResponse
|
||||
) {
|
||||
super(client, cachedResponse);
|
||||
|
||||
this.updateConfiguration();
|
||||
vscode.workspace.onDidChangeConfiguration(() => this.updateConfiguration(), null, this._disposables);
|
||||
}
|
||||
|
||||
public dispose() {
|
||||
disposeAll(this._disposables);
|
||||
}
|
||||
|
||||
public resolveCodeLens(inputCodeLens: vscode.CodeLens, token: vscode.CancellationToken): Promise<vscode.CodeLens> {
|
||||
const codeLens = inputCodeLens as ReferencesCodeLens;
|
||||
@@ -106,11 +89,6 @@ class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLensProvide
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private updateConfiguration(): void {
|
||||
const config = vscode.workspace.getConfiguration(this.language);
|
||||
this.setEnabled(config.get('referencesCodeLens.enabled', false));
|
||||
}
|
||||
}
|
||||
|
||||
export function register(
|
||||
@@ -119,11 +97,9 @@ export function register(
|
||||
client: ITypeScriptServiceClient,
|
||||
cachedResponse: CachedNavTreeResponse,
|
||||
) {
|
||||
return new VersionDependentRegistration(client, API.v206, () => {
|
||||
const referenceCodeLensProvider = new TypeScriptReferencesCodeLensProvider(client, modeId, cachedResponse);
|
||||
return vscode.Disposable.from(
|
||||
vscode.languages.registerCodeLensProvider(selector, referenceCodeLensProvider),
|
||||
referenceCodeLensProvider,
|
||||
);
|
||||
});
|
||||
return new VersionDependentRegistration(client, API.v206, () =>
|
||||
new ConfigurationDependentRegistration(modeId, 'referencesCodeLens.enabled', () => {
|
||||
return vscode.languages.registerCodeLensProvider(selector,
|
||||
new TypeScriptReferencesCodeLensProvider(client, cachedResponse));
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user