Skip to content

Commit ef97a7a

Browse files
committed
format on save is available now(just like https://github.com/Microsoft/vscode-go/blob/master/src/goMain.ts)
1 parent 6990eff commit ef97a7a

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
## Release history
44

5-
### CURRENT
5+
### DEVELOPING
66
* add protobuf support(work with https://marketplace.visualstudio.com/items?itemName=peterj.proto)
77
* add javascript/typescript support
88
* allow different style & fallback style option for different languages
9+
* format on save is available now(just like https://github.com/Microsoft/vscode-go/blob/master/src/goMain.ts)
910

1011
### v 0.6.1
1112
* clean up dependencies #9

src/extension.ts

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
5959

6060

6161
parser.onopentag = (tag) => {
62-
if (currentEdit) { reject("Malformed output"); }
62+
if (currentEdit) {
63+
reject("Malformed output");
64+
}
6365

6466
switch (tag.name) {
6567
case "replacements":
@@ -161,7 +163,9 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
161163
vscode.window.showInformationMessage("The '" + formatCommandBinPath + "' command is not available. Please check your clang.formatTool user setting and ensure it is installed.");
162164
return resolve(null);
163165
}
164-
if (err) return reject("Cannot format due to syntax errors.");
166+
if (err) {
167+
return reject("Cannot format due to syntax errors.");
168+
}
165169

166170
var dummyProcessor = (value: string) => {
167171
debugger;
@@ -201,23 +205,69 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
201205
var child = cp.execFile(formatCommandBinPath, formatArgs, { cwd: workingPath }, childCompleted);
202206
child.stdin.end(codeContent);
203207

204-
token.onCancellationRequested(() => {
205-
child.kill();
206-
reject("Cancelation requested");
207-
});
208+
if(token) {
209+
token.onCancellationRequested(() => {
210+
child.kill();
211+
reject("Cancelation requested");
212+
});
213+
}
208214
});
209215
}
210216

217+
public formatDocument(document: vscode.TextDocument): Thenable<vscode.TextEdit[]>{
218+
return this.doFormatDocument(document, null, null, null);
219+
}
220+
211221
}
212222

213223
let diagnosticCollection: vscode.DiagnosticCollection;
214224

215225
export function activate(ctx: vscode.ExtensionContext): void {
216226

217227
var formatter = new ClangDocumentFormattingEditProvider();
228+
var availableLanguages = {};
218229

219230
MODES.forEach(mode => {
220231
ctx.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider(mode, formatter));
221232
ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(mode, formatter));
222-
})
233+
availableLanguages[mode.language] = true;
234+
});
235+
236+
// TODO: This is really ugly. I'm not sure we can do better until
237+
// Code supports a pre-save event where we can do the formatting before
238+
// the file is written to disk.
239+
// @see https://github.com/Microsoft/vscode-go/blob/master/src/goMain.ts
240+
let ignoreNextSave = new WeakSet<vscode.TextDocument>();
241+
242+
vscode.workspace.onDidSaveTextDocument(document => {
243+
try {
244+
let formatOnSave = vscode.workspace.getConfiguration('clang-format').get<boolean>('formatOnSave');
245+
if (!formatOnSave) {
246+
return;
247+
}
248+
249+
if (!availableLanguages[document.languageId] || ignoreNextSave.has(document)) {
250+
return;
251+
}
252+
253+
let textEditor = vscode.window.activeTextEditor;
254+
formatter.formatDocument(document).then(edits => {
255+
return textEditor.edit(editBuilder => {
256+
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText))
257+
});
258+
}).then(applied => {
259+
ignoreNextSave.add(document);
260+
return document.save();
261+
}).then(
262+
() => {
263+
ignoreNextSave.delete(document);
264+
}, () => {
265+
// Catch any errors and ignore so that we still trigger
266+
// the file save.
267+
}
268+
);
269+
} catch(e) {
270+
console.error('formate when save file failed.' + e.toString());
271+
}
272+
});
223273
}

0 commit comments

Comments
 (0)