|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { StopWatch } from '../common/stopWatch'; |
| 8 | +import { IConfigurationService, Product } from '../common/types'; |
| 9 | +import { IServiceContainer } from '../ioc/types'; |
| 10 | +import { sendTelemetryWhenDone } from '../telemetry'; |
| 11 | +import { FORMAT } from '../telemetry/constants'; |
| 12 | +import { BaseFormatter } from './baseFormatter'; |
| 13 | + |
| 14 | +export class BlackFormatter extends BaseFormatter { |
| 15 | + constructor(serviceContainer: IServiceContainer) { |
| 16 | + super('black', Product.black, serviceContainer); |
| 17 | + } |
| 18 | + |
| 19 | + public formatDocument(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken, range?: vscode.Range): Thenable<vscode.TextEdit[]> { |
| 20 | + const stopWatch = new StopWatch(); |
| 21 | + const settings = this.serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings(document.uri); |
| 22 | + const hasCustomArgs = Array.isArray(settings.formatting.blackArgs) && settings.formatting.blackArgs.length > 0; |
| 23 | + const formatSelection = false; // range ? !range.isEmpty : false; |
| 24 | + |
| 25 | + const args = []; |
| 26 | + // if (formatSelection) { |
| 27 | + // // tslint:disable-next-line:no-non-null-assertion |
| 28 | + // args.push(...['--lines', `${range!.start.line + 1}-${range!.end.line + 1}`]); |
| 29 | + // } |
| 30 | + // Yapf starts looking for config file starting from the file path. |
| 31 | + const fallbarFolder = this.getWorkspaceUri(document).fsPath; |
| 32 | + const cwd = this.getDocumentPath(document, fallbarFolder); |
| 33 | + const promise = super.provideDocumentFormattingEdits(document, options, token, args, cwd); |
| 34 | + sendTelemetryWhenDone(FORMAT, promise, stopWatch, { tool: 'black', hasCustomArgs, formatSelection }); |
| 35 | + return promise; |
| 36 | + } |
| 37 | +} |
0 commit comments