@@ -10,95 +10,100 @@ import { FORMATTERS } from '../lib/tools';
1010import { LoggingService } from '../services/logging-service' ;
1111import { EXTENSION_ID , promptForMissingTool } from '../lib/helper' ;
1212
13+ export class FortranFormattingProvider implements vscode . DocumentFormattingEditProvider {
14+ constructor ( private logger : LoggingService ) { }
1315
14- export class FortranFormattingProvider
15- implements vscode . DocumentFormattingEditProvider {
16-
17- constructor ( private logger : LoggingService ) { }
18-
19- public provideDocumentFormattingEdits ( document : vscode . TextDocument , options : vscode . FormattingOptions , token : vscode . CancellationToken ) : vscode . ProviderResult < vscode . TextEdit [ ] > {
20-
21- let formatterName : string = this . getFormatter ( ) ;
16+ public provideDocumentFormattingEdits (
17+ document : vscode . TextDocument ,
18+ options : vscode . FormattingOptions ,
19+ token : vscode . CancellationToken
20+ ) : vscode . ProviderResult < vscode . TextEdit [ ] > {
21+ const formatterName : string = this . getFormatter ( ) ;
2222
2323 if ( formatterName === 'fprettify' ) {
2424 this . doFormatFprettify ( document ) ;
25- }
26- else if ( formatterName === 'findent' ) {
25+ } else if ( formatterName === 'findent' ) {
2726 this . doFormatFindent ( document ) ;
28- }
29- else {
30- this . logger . logError ( 'Cannot format document with formatter set to Disabled' )
27+ } else {
28+ this . logger . logError ( 'Cannot format document with formatter set to Disabled' ) ;
3129 }
3230
33- return
31+ return ;
3432 }
3533
3634 /**
3735 * Use `fprettify` to format a Fortran file.
38- *
36+ *
3937 * @param document vscode.TextDocument document to operate on
4038 */
4139 private doFormatFprettify ( document : vscode . TextDocument ) {
42-
4340 // fprettify can only do FortranFreeFrom
4441 if ( document . languageId !== 'FortranFreeForm' ) {
4542 this . logger . logError ( `fprettify can only format FortranFreeForm, change
4643 to findent for FortranFixedForm formatting` ) ;
47- return
44+ return ;
4845 }
4946
50- const formatterName : string = 'fprettify' ;
51- let formatterPath : string = this . getFormatterPath ( ) ;
47+ const formatterName = 'fprettify' ;
48+ const formatterPath : string = this . getFormatterPath ( ) ;
5249 // If no formatter path is present check that formatter is present in $PATH
5350 if ( ! formatterPath ) {
5451 if ( ! which . sync ( formatterName , { nothrow : true } ) ) {
5552 this . logger . logWarning ( `Formatter: ${ formatterName } not detected in your system.
5653 Attempting to install now.` ) ;
57- let msg = `Installing ${ formatterName } through pip with --user option` ;
54+ const msg = `Installing ${ formatterName } through pip with --user option` ;
5855 promptForMissingTool ( formatterName , msg , 'Python' ) ;
5956 }
6057 }
61- let formatter : string = path . join ( formatterPath , formatterName ) ;
58+ const formatter : string = path . join ( formatterPath , formatterName ) ;
6259
63- let args : string [ ] = [ document . fileName , ...this . getFormatterArgs ( ) ] ;
60+ const args : string [ ] = [ document . fileName , ...this . getFormatterArgs ( ) ] ;
6461 // args.push('--silent'); // TODO: pass?
6562
6663 // Get current file (name rel to path), run extension can be in a shell??
67- let process = cp . spawn ( formatter , args ) ;
64+ const process = cp . spawn ( formatter , args ) ;
6865
6966 // if the findent then capture the output from that and parse it back to the file
70- process . stdout . on ( 'data' , ( data ) => { this . logger . logInfo ( `formatter stdout: ${ data } ` ) } ) ;
71- process . stderr . on ( 'data' , ( data ) => { this . logger . logError ( `formatter stderr: ${ data } ` ) } ) ;
72- process . on ( 'close' , ( code : number ) => { if ( code !== 0 ) this . logger . logInfo ( `formatter exited with code: ${ code } ` ) } ) ;
73- process . on ( 'error' , ( code ) => { this . logger . logInfo ( `formatter exited with code: ${ code } ` ) } ) ;
74-
67+ process . stdout . on ( 'data' , data => {
68+ this . logger . logInfo ( `formatter stdout: ${ data } ` ) ;
69+ } ) ;
70+ process . stderr . on ( 'data' , data => {
71+ this . logger . logError ( `formatter stderr: ${ data } ` ) ;
72+ } ) ;
73+ process . on ( 'close' , ( code : number ) => {
74+ if ( code !== 0 ) this . logger . logInfo ( `formatter exited with code: ${ code } ` ) ;
75+ } ) ;
76+ process . on ( 'error' , code => {
77+ this . logger . logInfo ( `formatter exited with code: ${ code } ` ) ;
78+ } ) ;
7579 }
7680
7781 /**
7882 * Use `findent` to format a Fortran file.
7983 * Creates a temporary file where the output is placed and then deleted
80- *
84+ *
8185 * @param document vscode.TextDocument document to operate on
8286 */
8387 private doFormatFindent ( document : vscode . TextDocument ) {
84-
85- const formatterName : string = 'findent' ;
86- let formatterPath : string = this . getFormatterPath ( ) ;
88+ const formatterName = 'findent' ;
89+ const formatterPath : string = this . getFormatterPath ( ) ;
8790 // If no formatter path is present check that formatter is present in $PATH
8891 if ( ! formatterPath ) {
8992 if ( ! which . sync ( formatterName , { nothrow : true } ) ) {
9093 this . logger . logWarning ( `Formatter: ${ formatterName } not detected in your system.
9194 Attempting to install now.` ) ;
92- let msg = `Installing ${ formatterName } through pip with --user option` ;
95+ const msg = `Installing ${ formatterName } through pip with --user option` ;
9396 promptForMissingTool ( formatterName , msg , 'Python' ) ;
9497 }
9598 }
9699 let formatter : string = path . join ( formatterPath , formatterName ) ;
97100
98101 // Annoyingly findent only outputs to a file and not to a stream so
99102 // let us go and create a temporary file
100- let out = document . uri . path + '.findent.tmp' ;
101- let args : string = [ '< ' + document . fileName + ' >' , out , ...this . getFormatterArgs ( ) ] . join ( ' ' ) ;
103+ const out = document . uri . path + '.findent.tmp' ;
104+ const args : string = [ '< ' + document . fileName + ' >' , out , ...this . getFormatterArgs ( ) ] . join (
105+ ' '
106+ ) ;
102107 formatter = formatter + ' ' + args ;
103108
104109 // @note It is wise to have all IO operations being synchronous we don't
@@ -108,52 +113,50 @@ export class FortranFormattingProvider
108113 cp . execSync ( formatter , { stdio : 'inherit' } ) ;
109114 fs . copyFileSync ( out , document . fileName ) ;
110115 fs . unlinkSync ( out ) ;
111-
112116 }
113117
114118 /**
115119 * Get the formatter type
116120 * Currently supporting: `findent` and `fprettify`
117- *
121+ *
118122 * Formatters are defined in FORMATTERS (./lib/tools.ts)
119- *
123+ *
120124 * @returns {string } formatter name or `Disabled`
121125 */
122126 private getFormatter ( ) : string {
123-
124- let config = vscode . workspace . getConfiguration ( EXTENSION_ID )
125- const formatter : string = config . get ( 'formatting.formatter' , 'Disabled' )
127+ const config = vscode . workspace . getConfiguration ( EXTENSION_ID ) ;
128+ const formatter : string = config . get ( 'formatting.formatter' , 'Disabled' ) ;
126129
127130 if ( ! FORMATTERS . includes ( formatter ) ) {
128- this . logger . logError ( `Unsupported formatter: ${ formatter } ` )
131+ this . logger . logError ( `Unsupported formatter: ${ formatter } ` ) ;
129132 }
130- return formatter
133+ return formatter ;
131134 }
132135
133136 /**
134137 * Read in any custom arguments for the formatter
135- *
138+ *
136139 * @returns {string[] } list of additional arguments
137140 */
138141 private getFormatterArgs ( ) : string [ ] {
139- let config = vscode . workspace . getConfiguration ( EXTENSION_ID )
140- const args : string [ ] = config . get ( 'formatting.args' , [ ] )
142+ const config = vscode . workspace . getConfiguration ( EXTENSION_ID ) ;
143+ const args : string [ ] = config . get ( 'formatting.args' , [ ] ) ;
141144
142- return args
145+ return args ;
143146 }
144147
145148 /**
146149 * Installation directory for formatter (if not in PATH)
147- *
150+ *
148151 * @returns {string } path of formatter
149152 */
150153 private getFormatterPath ( ) : string {
151- let config = vscode . workspace . getConfiguration ( EXTENSION_ID )
152- const formatterPath : string = config . get ( 'formatting.path' , '' )
154+ const config = vscode . workspace . getConfiguration ( EXTENSION_ID ) ;
155+ const formatterPath : string = config . get ( 'formatting.path' , '' ) ;
153156 if ( formatterPath !== '' ) {
154- this . logger . logInfo ( `Formatter located in: ${ formatterPath } ` )
157+ this . logger . logInfo ( `Formatter located in: ${ formatterPath } ` ) ;
155158 }
156159
157- return formatterPath
160+ return formatterPath ;
158161 }
159162}
0 commit comments