Skip to content

Commit 2b88793

Browse files
committed
fix: add missing pre and post steps around pg_bsd_indent
1 parent 59acb0c commit 2b88793

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

src/formatter.ts

+57-3
Original file line numberDiff line numberDiff line change
@@ -379,23 +379,77 @@ class PgindentDocumentFormatterProvider implements vscode.DocumentFormattingEdit
379379

380380
return await this.findExistingPgbsdindent(workspace);
381381
}
382-
382+
383+
private runPreIndent(contents: string): string {
384+
function replace(regex: any, replacement: any) {
385+
contents = contents.replace(regex, replacement)
386+
}
387+
388+
// Convert // comments to /* */
389+
replace(/^([ \t]*)\/\/(.*)$/gm, '$1/* $2 */');
390+
391+
// Adjust dash-protected block comments so indent won't change them
392+
replace(/\/\* \+---/gm, '/*---X_X');
393+
394+
// Prevent indenting of code in 'extern "C"' blocks
395+
// we replace the braces with comments which we'll reverse later
396+
replace(/(^#ifdef[ \t]+__cplusplus.*\nextern[ \t]+"C"[ \t]*\n)\{[ \t]*$/gm,
397+
'$1/\* Open extern "C" */');
398+
replace(/(^#ifdef[ \t]+__cplusplus.*\n)\}[ \t]*$/gm,
399+
'$1/\* Close extern "C" */');
400+
401+
// Protect wrapping in CATALOG()
402+
replace(/^(CATALOG\(.*)$/gm, '/*$1*/');
403+
404+
return contents;
405+
}
406+
407+
private runPostIndent(contents: string): string {
408+
function replace(regex: any, replacement: any) {
409+
contents = contents.replace(regex, replacement);
410+
}
411+
412+
// Restore CATALOG lines
413+
replace(/^\/\*(CATALOG\(.*)\*\/$/gm, '$1');
414+
415+
// put back braces for extern "C"
416+
replace(/^\/\* Open extern "C" \*\//gm, '{');
417+
replace(/^\/\* Close extern "C" \*\/$/gm, '}');
418+
419+
// Undo change of dash-protected block comments
420+
replace(/\/\*---X_X/gm, '/* ---');
421+
422+
// Fix run-together comments to have a tab between them
423+
replace(/\*\/(\/\*.*\*\/)$/gm, '*/\t$1');
424+
425+
// Use a single space before '*' in function return types
426+
replace(/^([A-Za-z_]\S*)[ \t]+\*$/gm, '$1 *');
427+
428+
return contents;
429+
}
430+
383431
private async runPgindentInternal(document: vscode.TextDocument,
384432
pg_bsd_indent: vscode.Uri) {
385433
/*
386434
* We use pg_bsd_indent directly instead of pgindent because:
387435
* - different pgindent versions behaves differently
388436
* - direct call to pg_bsd_indent is faster
437+
* - pgindent creates temp files which are not removed if error
438+
* happens - we can not track these files (main reason)
389439
*/
440+
390441
let typedefs = await this.getTypedefs(utils.joinPath(pg_bsd_indent, '..'));
442+
const contents = this.runPreIndent(document.getText());
391443
const {stdout} = await utils.execShell(
392444
pg_bsd_indent.fsPath, [
393445
...PgindentDocumentFormatterProvider.pg_bsd_indentDefaultFlags,
394446
`-U${typedefs.fsPath}`],
395-
{stdin: document.getText()});
447+
{stdin: contents});
448+
const result = this.runPostIndent(stdout);
449+
396450
/* On success cache pg_bsd_indent path */
397451
this.savedPgbsdPath = pg_bsd_indent;
398-
return stdout;
452+
return result;
399453
}
400454

401455
private async runPgindent(document: vscode.TextDocument,

0 commit comments

Comments
 (0)