Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 91429cc

Browse files
committed
handle situations where text returned by formaters is empty #171
1 parent 92b87de commit 91429cc

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

src/client/common/editor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export function getTextEditsFromPatch(before: string, patch: string): TextEdit[]
4848
// Strip the first two lines
4949
patch = patch.substring(patch.indexOf("@@"));
5050
}
51+
if (patch.length === 0) {
52+
return [];
53+
}
5154

5255
let d = new dmp.diff_match_patch();
5356
let patches = <Patch[]>(<any>d).patch_fromText(patch);

src/client/formatters/baseFormatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export abstract class BaseFormatter {
3232
let tmpFileCreated = document.isDirty;
3333
let filePromise = tmpFileCreated ? getTempFileWithDocumentContents(document) : Promise.resolve(document.fileName);
3434
return filePromise.then(filePath => {
35-
if (token.isCancellationRequested) {
35+
if (token && token.isCancellationRequested) {
3636
return [filePath, ""];
3737
}
3838
return Promise.all<string>([Promise.resolve(filePath), sendCommand(cmdLine + ` "${filePath}"`, vscode.workspace.rootPath)]);
@@ -41,7 +41,7 @@ export abstract class BaseFormatter {
4141
if (tmpFileCreated) {
4242
fs.unlink(data[0]);
4343
}
44-
if (token.isCancellationRequested) {
44+
if (token && token.isCancellationRequested) {
4545
return [];
4646
}
4747
return getTextEditsFromPatch(document.getText(), data[1]);

src/client/providers/formatOnSaveProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ export function activateFormatOnSaveProvider(languageFilter: vscode.DocumentFilt
3636
let delays = new telemetryHelper.Delays();
3737

3838
formatter.formatDocument(document, null, null).then(edits => {
39+
if (edits.length === 0) return false;
3940
return textEditor.edit(editBuilder => {
4041
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
4142
});
4243
}).then(applied => {
4344
delays.stop();
4445
telemetryHelper.sendTelemetryEvent(telemetryContracts.IDE.Format, { Format_Provider: formatter.Id, Format_OnSave: "true" }, delays.toMeasures());
4546
ignoreNextSave.add(document);
46-
return document.save();
47+
return applied ? document.save() : true;
4748
}).then(() => {
4849
ignoreNextSave.delete(document);
4950
}, () => {

0 commit comments

Comments
 (0)