Skip to content

Commit b46ddb5

Browse files
committed
Began adding black formatter
1 parent 8396814 commit b46ddb5

File tree

7 files changed

+66
-7
lines changed

7 files changed

+66
-7
lines changed

package.json

+17-1
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,8 @@
14151415
"enum": [
14161416
"autopep8",
14171417
"yapf",
1418-
"none"
1418+
"none",
1419+
"black"
14191420
],
14201421
"scope": "resource"
14211422
},
@@ -1431,6 +1432,12 @@
14311432
"description": "Path to yapf, you can use a custom version of yapf by modifying this setting to include the full path.",
14321433
"scope": "resource"
14331434
},
1435+
"python.formatting.blackPath": {
1436+
"type": "string",
1437+
"default": "black",
1438+
"description": "Path to black, you can use a custom version of black by modifying this setting to include the full path.",
1439+
"scope": "resource"
1440+
},
14341441
"python.formatting.autopep8Args": {
14351442
"type": "array",
14361443
"description": "Arguments passed in. Each argument is a separate item in the array.",
@@ -1449,6 +1456,15 @@
14491456
},
14501457
"scope": "resource"
14511458
},
1459+
"python.formatting.blackArgs": {
1460+
"type": "array",
1461+
"description": "Arguments passed in. Each argument is a separate item in the array.",
1462+
"default": [],
1463+
"items": {
1464+
"type": "string"
1465+
},
1466+
"scope": "resource"
1467+
},
14521468
"python.autoComplete.preloadModules": {
14531469
"type": "array",
14541470
"items": {

src/client/common/types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ export enum Product {
6262
unittest = 12,
6363
ctags = 13,
6464
rope = 14,
65-
isort = 15
65+
isort = 15,
66+
black = 16
6667
}
6768

6869
export enum ModuleNamePurpose {
@@ -192,6 +193,8 @@ export interface IFormattingSettings {
192193
readonly autopep8Args: string[];
193194
yapfPath: string;
194195
readonly yapfArgs: string[];
196+
blackPath: string;
197+
readonly blackArgs: string[];
195198
}
196199
export interface IAutoCompeteSettings {
197200
readonly addBrackets: boolean;
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

src/client/formatters/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ExecutionInfo, IFormattingSettings, Product } from '../common/types';
66

77
export const IFormatterHelper = Symbol('IFormatterHelper');
88

9-
export type FormatterId = 'autopep8' | 'yapf';
9+
export type FormatterId = 'autopep8' | 'yapf' | 'black';
1010

1111
export type FormatterSettingsPropertyNames = {
1212
argsName: keyof IFormattingSettings;

src/client/providers/formatProvider.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { IConfigurationService } from '../common/types';
88
import { IServiceContainer } from '../ioc/types';
99
import { AutoPep8Formatter } from './../formatters/autoPep8Formatter';
1010
import { BaseFormatter } from './../formatters/baseFormatter';
11+
import { BlackFormatter } from './../formatters/blackFormatter';
1112
import { DummyFormatter } from './../formatters/dummyFormatter';
1213
import { YapfFormatter } from './../formatters/yapfFormatter';
1314

@@ -27,16 +28,18 @@ export class PythonFormattingEditProvider implements vscode.DocumentFormattingEd
2728
public constructor(context: vscode.ExtensionContext, serviceContainer: IServiceContainer) {
2829
const yapfFormatter = new YapfFormatter(serviceContainer);
2930
const autoPep8 = new AutoPep8Formatter(serviceContainer);
31+
const blackFormatter = new BlackFormatter(serviceContainer);
3032
const dummy = new DummyFormatter(serviceContainer);
3133
this.formatters.set(yapfFormatter.Id, yapfFormatter);
3234
this.formatters.set(autoPep8.Id, autoPep8);
35+
this.formatters.set(blackFormatter.Id, blackFormatter);
3336
this.formatters.set(dummy.Id, dummy);
3437

3538
this.commands = serviceContainer.get<ICommandManager>(ICommandManager);
3639
this.workspace = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
3740
this.documentManager = serviceContainer.get<IDocumentManager>(IDocumentManager);
3841
this.config = serviceContainer.get<IConfigurationService>(IConfigurationService);
39-
this.disposables.push(this.documentManager.onDidSaveTextDocument(async document => await this.onSaveDocument(document)));
42+
this.disposables.push(this.documentManager.onDidSaveTextDocument(async document => this.onSaveDocument(document)));
4043
}
4144

4245
public dispose() {

src/client/telemetry/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type EditorLoadTelemetry = {
77
condaVersion: string;
88
};
99
export type FormatTelemetry = {
10-
tool: 'autopep8' | 'yapf';
10+
tool: 'autopep8' | 'yapf' | 'black';
1111
hasCustomArgs: boolean;
1212
formatSelection: boolean;
1313
};

yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,7 @@ gulp-untar@^0.0.6:
18261826
tar "^2.2.1"
18271827
through2 "~2.0.3"
18281828

1829-
gulp-util@^3.0.0, gulp-util@^3.0.7, gulp-util@~3.0.0, gulp-util@~3.0.7, gulp-util@~3.0.8:
1829+
gulp-util@^3.0.0, gulp-util@^3.0.7, gulp-util@~3.0.8:
18301830
version "3.0.8"
18311831
resolved "https://registry.yarnpkg.com/gulp-util/-/gulp-util-3.0.8.tgz#0054e1e744502e27c04c187c3ecc505dd54bbb4f"
18321832
dependencies:
@@ -3904,7 +3904,7 @@ ret@~0.1.10:
39043904
version "0.1.15"
39053905
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
39063906

3907-
retyped-diff-match-patch-tsd-ambient@^1.0.0-1:
3907+
retyped-diff-match-patch-tsd-ambient@^1.0.0-0:
39083908
version "1.0.0-1"
39093909
resolved "https://registry.yarnpkg.com/retyped-diff-match-patch-tsd-ambient/-/retyped-diff-match-patch-tsd-ambient-1.0.0-1.tgz#26482bf4915c7ed9f8300bb5cbec48fd4ff5bc62"
39103910

0 commit comments

Comments
 (0)