@@ -52,25 +52,7 @@ export class FortranFormattingProvider implements vscode.DocumentFormattingEditP
52
52
) ;
53
53
return undefined ;
54
54
}
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' ] ) ;
74
56
}
75
57
76
58
/**
@@ -79,23 +61,30 @@ export class FortranFormattingProvider implements vscode.DocumentFormattingEditP
79
61
* @param document vscode.TextDocument document to operate on
80
62
*/
81
63
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 [ ] > {
83
72
const formatterPath : string =
84
73
this . formatterPath === '' ? '' : pathRelToAbs ( this . formatterPath , document . uri ) ;
85
- const formatter : string = path . join ( formatterPath , formatterName ) ;
74
+ const formatter : string = path . join ( formatterPath , name ) ;
86
75
// If no formatter is detected try and install it
87
76
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' ] ) ;
91
80
}
92
81
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 ) ;
95
84
const edits : vscode . TextEdit [ ] = [ ] ;
96
85
const [ stdout , stderr ] = await spawnAsPromise ( formatter , args , undefined , document . getText ( ) ) ;
97
86
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 } ` ) ;
99
88
return edits ;
100
89
}
101
90
0 commit comments