Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ea033b4

Browse files
committedSep 26, 2022
refactor: remove code duplication
1 parent 721df4c commit ea033b4

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed
 

‎src/features/formatting-provider.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,7 @@ export class FortranFormattingProvider implements vscode.DocumentFormattingEditP
5252
);
5353
return undefined;
5454
}
55-
56-
const formatterName = process.platform !== 'win32' ? 'fprettify' : 'fprettify.exe';
57-
const formatterPath: string =
58-
this.formatterPath === '' ? '' : pathRelToAbs(this.formatterPath, document.uri);
59-
const formatter: string = path.join(formatterPath, formatterName);
60-
// If no formatter is detected try and install it
61-
if (!which.sync(formatter, { nothrow: true })) {
62-
this.logger.warn(`[format] ${formatterName} not found. Attempting to install now.`);
63-
const msg = `Installing ${formatterName} through pip with --user option`;
64-
promptForMissingTool(formatterName, msg, 'Python', ['Install']);
65-
}
66-
67-
const args: string[] = ['--stdout', ...this.getFormatterArgs()];
68-
this.logger.debug(`[format] fprettify args:`, args);
69-
const edits: vscode.TextEdit[] = [];
70-
const [stdout, stderr] = await spawnAsPromise(formatter, args, undefined, document.getText());
71-
edits.push(new vscode.TextEdit(getWholeFileRange(document), stdout));
72-
if (stderr) this.logger.error(`[format] fprettify error output: ${stderr}`);
73-
return edits;
55+
return this.spawnFormatBase(document, 'fprettify', ['--stdout']);
7456
}
7557

7658
/**
@@ -79,23 +61,30 @@ export class FortranFormattingProvider implements vscode.DocumentFormattingEditP
7961
* @param document vscode.TextDocument document to operate on
8062
*/
8163
private async doFormatFindent(document: vscode.TextDocument): Promise<vscode.TextEdit[]> {
82-
const formatterName = process.platform !== 'win32' ? 'findent' : 'findent.exe';
64+
return this.spawnFormatBase(document, 'findent');
65+
}
66+
67+
private async spawnFormatBase(
68+
document: vscode.TextDocument,
69+
name: string,
70+
defaultArgs?: string[]
71+
): Promise<vscode.TextEdit[]> {
8372
const formatterPath: string =
8473
this.formatterPath === '' ? '' : pathRelToAbs(this.formatterPath, document.uri);
85-
const formatter: string = path.join(formatterPath, formatterName);
74+
const formatter: string = path.join(formatterPath, name);
8675
// If no formatter is detected try and install it
8776
if (!which.sync(formatter, { nothrow: true })) {
88-
this.logger.warn(`[format] ${formatterName} not found! Attempting to install now.`);
89-
const msg = `Installing ${formatterName} through pip with --user option`;
90-
promptForMissingTool(formatterName, msg, 'Python', ['Install']);
77+
this.logger.warn(`[format] ${name} not found! Attempting to install now.`);
78+
const msg = `Installing ${name} through pip with --user option`;
79+
await promptForMissingTool(name, msg, 'Python', ['Install']);
9180
}
9281

93-
const args: string[] = this.getFormatterArgs();
94-
this.logger.debug(`[format] findent args:`, args);
82+
const args: string[] = [...(defaultArgs || []), ...this.getFormatterArgs()];
83+
this.logger.debug(`[format] ${name} args:`, args);
9584
const edits: vscode.TextEdit[] = [];
9685
const [stdout, stderr] = await spawnAsPromise(formatter, args, undefined, document.getText());
9786
edits.push(new vscode.TextEdit(getWholeFileRange(document), stdout));
98-
if (stderr) this.logger.error(`[format] findent error output: ${stderr}`);
87+
if (stderr) this.logger.error(`[format] ${name} error output: ${stderr}`);
9988
return edits;
10089
}
10190

0 commit comments

Comments
 (0)
Please sign in to comment.