Skip to content

Commit 339b536

Browse files
committed
enabled formatting by copying contents to temp file #157
1 parent 691872d commit 339b536

3 files changed

Lines changed: 26 additions & 41 deletions

File tree

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
import * as vscode from "vscode";
4-
import * as path from "path";
54
import {BaseFormatter} from "./baseFormatter";
65

76
export class AutoPep8Formatter extends BaseFormatter {
@@ -11,7 +10,6 @@ export class AutoPep8Formatter extends BaseFormatter {
1110

1211
public formatDocument(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<vscode.TextEdit[]> {
1312
let autopep8Path = this.pythonSettings.formatting.autopep8Path;
14-
let fileDir = path.dirname(document.uri.fsPath);
15-
return super.provideDocumentFormattingEdits(document, options, token, `${autopep8Path} "${document.uri.fsPath}"`);
13+
return super.provideDocumentFormattingEdits(document, options, token, `${autopep8Path} --diff`);
1614
}
1715
}

src/client/formatters/baseFormatter.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from "path";
55
import * as fs from "fs";
66
import {sendCommand} from "./../common/childProc";
77
import * as settings from "./../common/configSettings";
8-
import {getTextEdits} from "./../common/editor";
8+
import {getTextEditsFromPatch, getTempFileWithDocumentContents} from "./../common/editor";
99

1010
export abstract class BaseFormatter {
1111
public Id: string;
@@ -23,22 +23,31 @@ export abstract class BaseFormatter {
2323
protected provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken, cmdLine: string): Thenable<vscode.TextEdit[]> {
2424
// Todo: Save the contents of the file to a temporary file and format that instead saving the actual file
2525
// This could unnecessarily trigger other behaviours
26-
return document.save().then(saved => {
27-
let filePath = document.uri.fsPath;
28-
if (!fs.existsSync(filePath)) {
29-
vscode.window.showErrorMessage(`File ${filePath} does not exist`);
26+
this.outputChannel.clear();
27+
28+
// autopep8 and yapf have the ability to read from the process input stream and return the formatted code out of the output stream
29+
// However they don't support returning the diff of the formatted text when reading data from the input stream
30+
// Yes getting text formatted that way avoids having to create a temporary file, however the diffing will have
31+
// to be done here in node (extension), i.e. extension cpu, i.e. les responsive solution
32+
let tmpFileCreated = document.isDirty;
33+
let filePromise = tmpFileCreated ? getTempFileWithDocumentContents(document) : Promise.resolve(document.fileName);
34+
return filePromise.then(filePath => {
35+
if (token.isCancellationRequested) {
36+
return [filePath, ""];
37+
}
38+
return Promise.all<string>([Promise.resolve(filePath), sendCommand(cmdLine + ` "${filePath}"`, vscode.workspace.rootPath)]);
39+
}).then(data => {
40+
// Delete the temporary file created
41+
if (tmpFileCreated) {
42+
fs.unlink(data[0]);
43+
}
44+
if (token.isCancellationRequested) {
3045
return [];
3146
}
32-
33-
this.outputChannel.clear();
34-
let fileDir = path.dirname(document.uri.fsPath);
35-
36-
return sendCommand(cmdLine, fileDir).then(data => {
37-
return getTextEdits(document.getText(), data);
38-
}).catch(errorMsg => {
39-
this.outputChannel.appendLine(errorMsg);
40-
throw new Error(`There was an error in formatting the document. View the Python output window for details.`);
41-
});
47+
return getTextEditsFromPatch(document.getText(), data[1]);
48+
}).catch(error => {
49+
this.outputChannel.appendLine(error);
50+
throw new Error(`There was an error in formatting the document. View the Python output window for details.`);
4251
});
4352
}
4453
}
Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
"use strict";
22

33
import * as vscode from "vscode";
4-
import * as path from "path";
54
import {BaseFormatter} from "./baseFormatter";
65
import * as settings from "./../common/configSettings";
7-
import {sendCommand} from "./../common/childProc";
8-
import * as fs from "fs";
9-
import {getTextEditsFromPatch} from "./../common/editor";
106

117
export class YapfFormatter extends BaseFormatter {
128
constructor(outputChannel: vscode.OutputChannel) {
@@ -15,24 +11,6 @@ export class YapfFormatter extends BaseFormatter {
1511

1612
public formatDocument(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<vscode.TextEdit[]> {
1713
let yapfPath = this.pythonSettings.formatting.yapfPath;
18-
let fileDir = path.dirname(document.uri.fsPath);
19-
let commandLine = `${yapfPath} "${document.uri.fsPath}" --diff`;
20-
21-
return document.save().then(saved => {
22-
let filePath = document.uri.fsPath;
23-
if (!fs.existsSync(filePath)) {
24-
vscode.window.showErrorMessage(`File ${filePath} does not exist`)
25-
return [];
26-
}
27-
28-
this.outputChannel.clear();
29-
30-
return sendCommand(commandLine, fileDir).then(data => {
31-
return getTextEditsFromPatch(document.getText(), data);
32-
}).catch(errorMsg => {
33-
this.outputChannel.appendLine(errorMsg);
34-
throw new Error(`There was an error in formatting the document. View the Python output window for details.`);
35-
});
36-
});
14+
return super.provideDocumentFormattingEdits(document, options, token, `${yapfPath} --diff`);
3715
}
3816
}

0 commit comments

Comments
 (0)