forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackFormatter.ts
More file actions
47 lines (41 loc) · 2 KB
/
blackFormatter.ts
File metadata and controls
47 lines (41 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as vscode from 'vscode';
import { IApplicationShell } from '../common/application/types';
import { Product } from '../common/installer/productInstaller';
import { IConfigurationService } from '../common/types';
import { noop } from '../common/utils/misc';
import { StopWatch } from '../common/utils/stopWatch';
import { IServiceContainer } from '../ioc/types';
import { sendTelemetryWhenDone } from '../telemetry';
import { EventName } from '../telemetry/constants';
import { BaseFormatter } from './baseFormatter';
export class BlackFormatter extends BaseFormatter {
constructor(serviceContainer: IServiceContainer) {
super('black', Product.black, serviceContainer);
}
public async formatDocument(
document: vscode.TextDocument,
options: vscode.FormattingOptions,
token: vscode.CancellationToken,
range?: vscode.Range
): Promise<vscode.TextEdit[]> {
const stopWatch = new StopWatch();
const settings = this.serviceContainer
.get<IConfigurationService>(IConfigurationService)
.getSettings(document.uri);
const hasCustomArgs = Array.isArray(settings.formatting.blackArgs) && settings.formatting.blackArgs.length > 0;
const formatSelection = range ? !range.isEmpty : false;
if (formatSelection) {
const shell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
// Black does not support partial formatting on purpose.
shell.showErrorMessage('Black does not support the "Format Selection" command').then(noop, noop);
return [];
}
const blackArgs = ['--diff', '--quiet'];
const promise = super.provideDocumentFormattingEdits(document, options, token, blackArgs);
sendTelemetryWhenDone(EventName.FORMAT, promise, stopWatch, { tool: 'black', hasCustomArgs, formatSelection });
return promise;
}
}