@@ -10,95 +10,100 @@ import { FORMATTERS } from '../lib/tools';
10
10
import { LoggingService } from '../services/logging-service' ;
11
11
import { EXTENSION_ID , promptForMissingTool } from '../lib/helper' ;
12
12
13
+ export class FortranFormattingProvider implements vscode . DocumentFormattingEditProvider {
14
+ constructor ( private logger : LoggingService ) { }
13
15
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 ( ) ;
22
22
23
23
if ( formatterName === 'fprettify' ) {
24
24
this . doFormatFprettify ( document ) ;
25
- }
26
- else if ( formatterName === 'findent' ) {
25
+ } else if ( formatterName === 'findent' ) {
27
26
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' ) ;
31
29
}
32
30
33
- return
31
+ return ;
34
32
}
35
33
36
34
/**
37
35
* Use `fprettify` to format a Fortran file.
38
- *
36
+ *
39
37
* @param document vscode.TextDocument document to operate on
40
38
*/
41
39
private doFormatFprettify ( document : vscode . TextDocument ) {
42
-
43
40
// fprettify can only do FortranFreeFrom
44
41
if ( document . languageId !== 'FortranFreeForm' ) {
45
42
this . logger . logError ( `fprettify can only format FortranFreeForm, change
46
43
to findent for FortranFixedForm formatting` ) ;
47
- return
44
+ return ;
48
45
}
49
46
50
- const formatterName : string = 'fprettify' ;
51
- let formatterPath : string = this . getFormatterPath ( ) ;
47
+ const formatterName = 'fprettify' ;
48
+ const formatterPath : string = this . getFormatterPath ( ) ;
52
49
// If no formatter path is present check that formatter is present in $PATH
53
50
if ( ! formatterPath ) {
54
51
if ( ! which . sync ( formatterName , { nothrow : true } ) ) {
55
52
this . logger . logWarning ( `Formatter: ${ formatterName } not detected in your system.
56
53
Attempting to install now.` ) ;
57
- let msg = `Installing ${ formatterName } through pip with --user option` ;
54
+ const msg = `Installing ${ formatterName } through pip with --user option` ;
58
55
promptForMissingTool ( formatterName , msg , 'Python' ) ;
59
56
}
60
57
}
61
- let formatter : string = path . join ( formatterPath , formatterName ) ;
58
+ const formatter : string = path . join ( formatterPath , formatterName ) ;
62
59
63
- let args : string [ ] = [ document . fileName , ...this . getFormatterArgs ( ) ] ;
60
+ const args : string [ ] = [ document . fileName , ...this . getFormatterArgs ( ) ] ;
64
61
// args.push('--silent'); // TODO: pass?
65
62
66
63
// 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 ) ;
68
65
69
66
// 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
+ } ) ;
75
79
}
76
80
77
81
/**
78
82
* Use `findent` to format a Fortran file.
79
83
* Creates a temporary file where the output is placed and then deleted
80
- *
84
+ *
81
85
* @param document vscode.TextDocument document to operate on
82
86
*/
83
87
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 ( ) ;
87
90
// If no formatter path is present check that formatter is present in $PATH
88
91
if ( ! formatterPath ) {
89
92
if ( ! which . sync ( formatterName , { nothrow : true } ) ) {
90
93
this . logger . logWarning ( `Formatter: ${ formatterName } not detected in your system.
91
94
Attempting to install now.` ) ;
92
- let msg = `Installing ${ formatterName } through pip with --user option` ;
95
+ const msg = `Installing ${ formatterName } through pip with --user option` ;
93
96
promptForMissingTool ( formatterName , msg , 'Python' ) ;
94
97
}
95
98
}
96
99
let formatter : string = path . join ( formatterPath , formatterName ) ;
97
100
98
101
// Annoyingly findent only outputs to a file and not to a stream so
99
102
// 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
+ ) ;
102
107
formatter = formatter + ' ' + args ;
103
108
104
109
// @note It is wise to have all IO operations being synchronous we don't
@@ -108,52 +113,50 @@ export class FortranFormattingProvider
108
113
cp . execSync ( formatter , { stdio : 'inherit' } ) ;
109
114
fs . copyFileSync ( out , document . fileName ) ;
110
115
fs . unlinkSync ( out ) ;
111
-
112
116
}
113
117
114
118
/**
115
119
* Get the formatter type
116
120
* Currently supporting: `findent` and `fprettify`
117
- *
121
+ *
118
122
* Formatters are defined in FORMATTERS (./lib/tools.ts)
119
- *
123
+ *
120
124
* @returns {string } formatter name or `Disabled`
121
125
*/
122
126
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' ) ;
126
129
127
130
if ( ! FORMATTERS . includes ( formatter ) ) {
128
- this . logger . logError ( `Unsupported formatter: ${ formatter } ` )
131
+ this . logger . logError ( `Unsupported formatter: ${ formatter } ` ) ;
129
132
}
130
- return formatter
133
+ return formatter ;
131
134
}
132
135
133
136
/**
134
137
* Read in any custom arguments for the formatter
135
- *
138
+ *
136
139
* @returns {string[] } list of additional arguments
137
140
*/
138
141
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' , [ ] ) ;
141
144
142
- return args
145
+ return args ;
143
146
}
144
147
145
148
/**
146
149
* Installation directory for formatter (if not in PATH)
147
- *
150
+ *
148
151
* @returns {string } path of formatter
149
152
*/
150
153
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' , '' ) ;
153
156
if ( formatterPath !== '' ) {
154
- this . logger . logInfo ( `Formatter located in: ${ formatterPath } ` )
157
+ this . logger . logInfo ( `Formatter located in: ${ formatterPath } ` ) ;
155
158
}
156
159
157
- return formatterPath
160
+ return formatterPath ;
158
161
}
159
162
}
0 commit comments