forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatOnSaveProvider.ts
More file actions
57 lines (48 loc) · 2.64 KB
/
Copy pathformatOnSaveProvider.ts
File metadata and controls
57 lines (48 loc) · 2.64 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
48
49
50
51
52
53
54
55
56
57
"use strict";
// Solution for auto-formatting borrowed from the "go" language VSCode extension.
import * as vscode from "vscode";
import {BaseFormatter} from "./../formatters/baseFormatter";
import {YapfFormatter} from "./../formatters/yapfFormatter";
import {AutoPep8Formatter} from "./../formatters/autoPep8Formatter";
import * as settings from "./../common/configSettings";
import * as telemetryHelper from "../common/telemetry";
import * as telemetryContracts from "../common/telemetryContracts";
export function activateFormatOnSaveProvider(languageFilter: vscode.DocumentFilter, settings: settings.IPythonSettings, outputChannel: vscode.OutputChannel, workspaceRootPath: string): vscode.Disposable {
let formatters = new Map<string, BaseFormatter>();
let pythonSettings = settings;
let yapfFormatter = new YapfFormatter(outputChannel, settings, workspaceRootPath);
let autoPep8 = new AutoPep8Formatter(outputChannel, settings, workspaceRootPath);
formatters.set(yapfFormatter.Id, yapfFormatter);
formatters.set(autoPep8.Id, autoPep8);
// This is really ugly. I'm not sure we can do better until
// Code supports a pre-save event where we can do the formatting before
// the file is written to disk.
let ignoreNextSave = new WeakSet<vscode.TextDocument>();
let subscription = vscode.workspace.onDidSaveTextDocument(document => {
if (document.languageId !== languageFilter.language || ignoreNextSave.has(document)) {
return;
}
let textEditor = vscode.window.activeTextEditor;
if (pythonSettings.formatting.formatOnSave && textEditor.document === document) {
let formatter = formatters.get(pythonSettings.formatting.provider);
let delays = new telemetryHelper.Delays();
formatter.formatDocument(document, null, null).then(edits => {
if (edits.length === 0) return false;
return textEditor.edit(editBuilder => {
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
});
}).then(applied => {
delays.stop();
telemetryHelper.sendTelemetryEvent(telemetryContracts.IDE.Format, { Format_Provider: formatter.Id, Format_OnSave: "true" }, delays.toMeasures());
ignoreNextSave.add(document);
return applied ? document.save() : true;
}).then(() => {
ignoreNextSave.delete(document);
}, () => {
// Catch any errors and ignore so that we still trigger
// the file save.
});
}
}, null, null);
return subscription;
}