mirror of
https://github.com/Microsoft/vscode
synced 2024-09-19 02:26:04 +00:00
Move formatting provider to use standard registration
This commit is contained in:
parent
03563adac6
commit
604beac170
|
@ -3,14 +3,15 @@
|
|||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { DocumentRangeFormattingEditProvider, OnTypeFormattingEditProvider, FormattingOptions, TextDocument, Position, Range, CancellationToken, TextEdit, WorkspaceConfiguration, Disposable, languages, workspace, DocumentSelector } from 'vscode';
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
import * as Proto from '../protocol';
|
||||
import { ITypeScriptServiceClient } from '../typescriptService';
|
||||
import * as typeConverters from '../utils/typeConverters';
|
||||
import FileConfigurationManager from './fileConfigurationManager';
|
||||
import { disposeAll } from '../utils/dispose';
|
||||
|
||||
export class TypeScriptFormattingProvider implements DocumentRangeFormattingEditProvider, OnTypeFormattingEditProvider {
|
||||
class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEditProvider, vscode.OnTypeFormattingEditProvider {
|
||||
private enabled: boolean = true;
|
||||
|
||||
public constructor(
|
||||
|
@ -18,7 +19,7 @@ export class TypeScriptFormattingProvider implements DocumentRangeFormattingEdit
|
|||
private readonly formattingOptionsManager: FileConfigurationManager
|
||||
) { }
|
||||
|
||||
public updateConfiguration(config: WorkspaceConfiguration): void {
|
||||
public updateConfiguration(config: vscode.WorkspaceConfiguration): void {
|
||||
this.enabled = config.get('format.enable', true);
|
||||
}
|
||||
|
||||
|
@ -27,11 +28,11 @@ export class TypeScriptFormattingProvider implements DocumentRangeFormattingEdit
|
|||
}
|
||||
|
||||
private async doFormat(
|
||||
document: TextDocument,
|
||||
options: FormattingOptions,
|
||||
document: vscode.TextDocument,
|
||||
options: vscode.FormattingOptions,
|
||||
args: Proto.FormatRequestArgs,
|
||||
token: CancellationToken
|
||||
): Promise<TextEdit[]> {
|
||||
token: vscode.CancellationToken
|
||||
): Promise<vscode.TextEdit[]> {
|
||||
await this.formattingOptionsManager.ensureConfigurationOptions(document, options, token);
|
||||
try {
|
||||
const response = await this.client.execute('format', args, token);
|
||||
|
@ -45,11 +46,11 @@ export class TypeScriptFormattingProvider implements DocumentRangeFormattingEdit
|
|||
}
|
||||
|
||||
public async provideDocumentRangeFormattingEdits(
|
||||
document: TextDocument,
|
||||
range: Range,
|
||||
options: FormattingOptions,
|
||||
token: CancellationToken
|
||||
): Promise<TextEdit[]> {
|
||||
document: vscode.TextDocument,
|
||||
range: vscode.Range,
|
||||
options: vscode.FormattingOptions,
|
||||
token: vscode.CancellationToken
|
||||
): Promise<vscode.TextEdit[]> {
|
||||
const absPath = this.client.normalizePath(document.uri);
|
||||
if (!absPath) {
|
||||
return [];
|
||||
|
@ -65,12 +66,12 @@ export class TypeScriptFormattingProvider implements DocumentRangeFormattingEdit
|
|||
}
|
||||
|
||||
public async provideOnTypeFormattingEdits(
|
||||
document: TextDocument,
|
||||
position: Position,
|
||||
document: vscode.TextDocument,
|
||||
position: vscode.Position,
|
||||
ch: string,
|
||||
options: FormattingOptions,
|
||||
token: CancellationToken
|
||||
): Promise<TextEdit[]> {
|
||||
options: vscode.FormattingOptions,
|
||||
token: vscode.CancellationToken
|
||||
): Promise<vscode.TextEdit[]> {
|
||||
const filepath = this.client.normalizePath(document.uri);
|
||||
if (!filepath) {
|
||||
return [];
|
||||
|
@ -87,7 +88,7 @@ export class TypeScriptFormattingProvider implements DocumentRangeFormattingEdit
|
|||
try {
|
||||
const response = await this.client.execute('formatonkey', args, token);
|
||||
const edits = response.body;
|
||||
const result: TextEdit[] = [];
|
||||
const result: vscode.TextEdit[] = [];
|
||||
if (!edits) {
|
||||
return result;
|
||||
}
|
||||
|
@ -116,16 +117,20 @@ export class TypeScriptFormattingProvider implements DocumentRangeFormattingEdit
|
|||
}
|
||||
}
|
||||
|
||||
export class FormattingProviderManager {
|
||||
private formattingProviderRegistration: Disposable | undefined;
|
||||
|
||||
class FormattingProviderManager {
|
||||
private formattingProviderRegistration: vscode.Disposable | undefined;
|
||||
private disposables: vscode.Disposable[] = [];
|
||||
constructor(
|
||||
private readonly modeId: string,
|
||||
private readonly formattingProvider: TypeScriptFormattingProvider,
|
||||
private readonly selector: DocumentSelector
|
||||
) { }
|
||||
private readonly selector: vscode.DocumentSelector
|
||||
) {
|
||||
vscode.workspace.onDidChangeConfiguration(() => this.updateConfiguration(), null, this.disposables);
|
||||
}
|
||||
|
||||
public dispose() {
|
||||
disposeAll(this.disposables);
|
||||
|
||||
if (this.formattingProviderRegistration) {
|
||||
this.formattingProviderRegistration.dispose();
|
||||
this.formattingProviderRegistration = undefined;
|
||||
|
@ -133,14 +138,33 @@ export class FormattingProviderManager {
|
|||
}
|
||||
|
||||
public updateConfiguration(): void {
|
||||
const config = workspace.getConfiguration(this.modeId);
|
||||
const config = vscode.workspace.getConfiguration(this.modeId);
|
||||
this.formattingProvider.updateConfiguration(config);
|
||||
|
||||
if (!this.formattingProvider.isEnabled() && this.formattingProviderRegistration) {
|
||||
this.formattingProviderRegistration.dispose();
|
||||
this.formattingProviderRegistration = undefined;
|
||||
} else if (this.formattingProvider.isEnabled() && !this.formattingProviderRegistration) {
|
||||
this.formattingProviderRegistration = languages.registerDocumentRangeFormattingEditProvider(this.selector, this.formattingProvider);
|
||||
this.formattingProviderRegistration = vscode.languages.registerDocumentRangeFormattingEditProvider(this.selector, this.formattingProvider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function register(
|
||||
selector: vscode.DocumentSelector,
|
||||
modeId: string,
|
||||
config: vscode.WorkspaceConfiguration,
|
||||
client: ITypeScriptServiceClient,
|
||||
fileConfigurationManager: FileConfigurationManager
|
||||
) {
|
||||
const disposables: vscode.Disposable[] = [];
|
||||
const formattingProvider = new TypeScriptFormattingProvider(client, fileConfigurationManager);
|
||||
formattingProvider.updateConfiguration(config);
|
||||
disposables.push(vscode.languages.registerOnTypeFormattingEditProvider(selector, formattingProvider, ';', '}', '\n'));
|
||||
|
||||
const formattingProviderManager = new FormattingProviderManager(modeId, formattingProvider, selector);
|
||||
formattingProviderManager.updateConfiguration();
|
||||
disposables.push(formattingProviderManager);
|
||||
|
||||
return vscode.Disposable.from(...disposables);
|
||||
}
|
|
@ -104,30 +104,21 @@ export default class LanguageProvider {
|
|||
const selector = this.documentSelector;
|
||||
const config = vscode.workspace.getConfiguration(this.id);
|
||||
|
||||
const { TypeScriptFormattingProvider, FormattingProviderManager } = await import('./features/formattingProvider');
|
||||
const formattingProvider = new TypeScriptFormattingProvider(client, this.fileConfigurationManager);
|
||||
formattingProvider.updateConfiguration(config);
|
||||
this.disposables.push(vscode.languages.registerOnTypeFormattingEditProvider(selector, formattingProvider, ';', '}', '\n'));
|
||||
|
||||
const formattingProviderManager = new FormattingProviderManager(this.description.id, formattingProvider, selector);
|
||||
formattingProviderManager.updateConfiguration();
|
||||
this.disposables.push(formattingProviderManager);
|
||||
this.toUpdateOnConfigurationChanged.push(formattingProviderManager);
|
||||
|
||||
const cachedResponse = new CachedNavTreeResponse();
|
||||
|
||||
this.disposables.push((await import('./features/completionItemProvider')).register(selector, client, typingsStatus, this.fileConfigurationManager, commandManager));
|
||||
this.disposables.push((await import('./features/directiveCommentCompletionProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/jsDocCompletionProvider')).register(selector, client, commandManager));
|
||||
this.disposables.push((await import('./features/hoverProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/definitionProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/directiveCommentCompletionProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/documentHighlightProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/referenceProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/documentSymbolProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/renameProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/signatureHelpProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/formattingProvider')).register(selector, this.description.id, config, client, this.fileConfigurationManager));
|
||||
this.disposables.push((await import('./features/hoverProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/jsDocCompletionProvider')).register(selector, client, commandManager));
|
||||
this.disposables.push((await import('./features/quickFixProvider')).register(selector, client, this.fileConfigurationManager, commandManager, this.diagnosticsManager, this.bufferSyncSupport, this.telemetryReporter));
|
||||
this.disposables.push((await import('./features/refactorProvider')).register(selector, client, this.fileConfigurationManager, commandManager));
|
||||
this.disposables.push((await import('./features/referenceProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/renameProvider')).register(selector, client));
|
||||
this.disposables.push((await import('./features/signatureHelpProvider')).register(selector, client));
|
||||
|
||||
await this.initFoldingProvider();
|
||||
this.disposables.push(vscode.workspace.onDidChangeConfiguration(c => {
|
||||
|
|
Loading…
Reference in a new issue