forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyapfFormatter.ts
More file actions
39 lines (36 loc) · 1.78 KB
/
yapfFormatter.ts
File metadata and controls
39 lines (36 loc) · 1.78 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
import * as vscode from 'vscode';
import { IConfigurationService, Product } from '../common/types';
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 YapfFormatter extends BaseFormatter {
constructor(serviceContainer: IServiceContainer) {
super('yapf', Product.yapf, 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.yapfArgs) && settings.formatting.yapfArgs.length > 0;
const formatSelection = range ? !range.isEmpty : false;
const yapfArgs = ['--diff'];
if (formatSelection) {
// tslint:disable-next-line:no-non-null-assertion
yapfArgs.push(...['--lines', `${range!.start.line + 1}-${range!.end.line + 1}`]);
}
// Yapf starts looking for config file starting from the file path.
const fallbarFolder = this.getWorkspaceUri(document).fsPath;
const cwd = this.getDocumentPath(document, fallbarFolder);
const promise = super.provideDocumentFormattingEdits(document, options, token, yapfArgs, cwd);
sendTelemetryWhenDone(EventName.FORMAT, promise, stopWatch, { tool: 'yapf', hasCustomArgs, formatSelection });
return promise;
}
}