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
38 lines (33 loc) · 1.89 KB
/
blackFormatter.ts
File metadata and controls
38 lines (33 loc) · 1.89 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as vscode from 'vscode';
import { StopWatch } from '../common/stopWatch';
import { IConfigurationService, Product } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { sendTelemetryWhenDone } from '../telemetry';
import { FORMAT } from '../telemetry/constants';
import { BaseFormatter } from './baseFormatter';
export class BlackFormatter extends BaseFormatter {
constructor(serviceContainer: IServiceContainer) {
super('black', Product.black, serviceContainer);
}
public formatDocument(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken, range?: vscode.Range): Thenable<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 = false; // range ? !range.isEmpty : false;
const args = ['--diff'];
// if (formatSelection) {
// black does not support partial formatting, throw an error?
// // tslint:disable-next-line:no-non-null-assertion
// args.push(...['--lines', `${range!.start.line + 1}-${range!.end.line + 1}`]);
// }
// Yapf starts looking for config file starting from the file path.
const fallbackFolder = this.getWorkspaceUri(document).fsPath;
const cwd = this.getDocumentPath(document, fallbackFolder);
const promise = super.provideDocumentFormattingEdits(document, options, token, args, cwd);
//sendTelemetryWhenDone(FORMAT, promise, stopWatch, { tool: 'black', hasCustomArgs, formatSelection });
return promise;
}
}