@@ -27,7 +27,7 @@ type ForgeFmtResult = {
27
27
28
28
function isFmtInstalled ( ) : boolean {
29
29
try {
30
- exec ( " forge fmt --version" , ( error , _stdout , _stderr ) => {
30
+ exec ( ' forge fmt --version' , ( error , _stdout , _stderr ) => {
31
31
if ( error ) {
32
32
throw error ;
33
33
}
@@ -38,35 +38,30 @@ function isFmtInstalled(): boolean {
38
38
}
39
39
}
40
40
41
- function forgeFmt (
42
- args : ForgeFmtArgs ,
43
- debug ?: boolean ,
44
- ) : Promise < ForgeFmtResult > {
41
+ function forgeFmt ( args : ForgeFmtArgs , debug ?: boolean ) : Promise < ForgeFmtResult > {
45
42
const { options, files } = args ;
46
43
const { root, check, raw } = options ;
47
44
48
- const commandArgs = [ " fmt" ] ;
45
+ const commandArgs = [ ' fmt' ] ;
49
46
50
47
if ( root ) {
51
- commandArgs . push ( " --root" , `"${ root } "` ) ;
48
+ commandArgs . push ( ' --root' , `"${ root } "` ) ;
52
49
}
53
50
54
51
if ( check ) {
55
- commandArgs . push ( " --check" ) ;
52
+ commandArgs . push ( ' --check' ) ;
56
53
}
57
54
58
55
if ( raw ) {
59
- commandArgs . push ( " --raw" ) ;
56
+ commandArgs . push ( ' --raw' ) ;
60
57
}
61
58
62
- commandArgs . push (
63
- ...files . map ( ( file ) => ( file . includes ( " " ) ? `"${ file } "` : file ) ) ,
64
- ) ;
59
+ commandArgs . push ( ...files . map ( ( file ) => ( file . includes ( ' ' ) ? `"${ file } "` : file ) ) ) ;
65
60
66
- const command = `forge ${ commandArgs . join ( " " ) } ` ;
61
+ const command = `forge ${ commandArgs . join ( ' ' ) } ` ;
67
62
68
63
if ( debug ) {
69
- console . debug ( " command =>" , command ) ;
64
+ console . debug ( ' command =>' , command ) ;
70
65
}
71
66
72
67
return new Promise ( ( resolve , reject ) => {
@@ -159,13 +154,6 @@ function registerForgeFmtLinter(context: vscode.ExtensionContext): {fileDisposab
159
154
return ;
160
155
}
161
156
162
- if ( ! vscode . workspace . workspaceFolders ?. [ 0 ] ) {
163
- vscode . window . showErrorMessage (
164
- "Unable to find workspace root. Please open a folder and try again." ,
165
- ) ;
166
- return ;
167
- }
168
-
169
157
const options : ForgeFmtOptions = {
170
158
root : vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ,
171
159
check : false ,
@@ -174,68 +162,97 @@ function registerForgeFmtLinter(context: vscode.ExtensionContext): {fileDisposab
174
162
175
163
const args : ForgeFmtArgs = {
176
164
options,
177
- files : [ vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ] ,
165
+ files : [ document . fileName ] ,
178
166
} ;
179
167
180
168
forgeFmt ( args )
181
169
. then ( ( result ) => {
182
170
if ( result . exitCode === 0 ) {
183
- vscode . window . showInformationMessage ( " Forge fmt ran successfully." ) ;
171
+ vscode . window . showInformationMessage ( ' Forge fmt ran successfully.' ) ;
184
172
} else {
185
- vscode . window . showErrorMessage (
186
- "Forge fmt failed. Please check the output for details." ,
187
- ) ;
173
+ vscode . window . showErrorMessage ( 'Forge fmt failed. Please check the output for details.' ) ;
188
174
189
175
console . log ( result . output ) ;
190
176
}
191
177
} )
192
178
. catch ( ( error ) => {
193
- vscode . window . showErrorMessage (
194
- "Forge fmt failed. Please check the output for details." ,
195
- ) ;
179
+ vscode . window . showErrorMessage ( 'Forge fmt failed. Please check the output for details.' ) ;
196
180
console . error ( error ) ;
197
181
} ) ;
198
- } ,
199
- ) ;
182
+ } else {
183
+ vscode . window . showErrorMessage ( 'Forge fmt is only available for solidity files.' ) ;
184
+ }
185
+ } ) ;
200
186
201
- const formatter = vscode . languages . registerDocumentFormattingEditProvider (
202
- "solidity" ,
203
- {
204
- provideDocumentFormattingEdits : ( document ) => {
205
- if ( ! isFmtInstalled ( ) ) {
206
- vscode . window . showErrorMessage (
207
- "Forge fmt is not installed. Please install it and try again." ,
208
- ) ;
209
- return ;
187
+ const lintSolWorkspace = vscode . commands . registerCommand ( 'osmium.format-sol-workspace' , function ( ) {
188
+ if ( ! isFmtInstalled ( ) ) {
189
+ vscode . window . showErrorMessage ( 'Forge fmt is not installed. Please install it and try again.' ) ;
190
+ return ;
191
+ }
192
+
193
+ if ( ! vscode . workspace . workspaceFolders ?. [ 0 ] ) {
194
+ vscode . window . showErrorMessage ( 'Unable to find workspace root. Please open a folder and try again.' ) ;
195
+ return ;
196
+ }
197
+
198
+ const options : ForgeFmtOptions = {
199
+ root : vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ,
200
+ check : false ,
201
+ raw : false ,
202
+ } ;
203
+
204
+ const args : ForgeFmtArgs = {
205
+ options,
206
+ files : [ vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ] ,
207
+ } ;
208
+
209
+ forgeFmt ( args )
210
+ . then ( ( result ) => {
211
+ if ( result . exitCode === 0 ) {
212
+ vscode . window . showInformationMessage ( 'Forge fmt ran successfully.' ) ;
213
+ } else {
214
+ vscode . window . showErrorMessage ( 'Forge fmt failed. Please check the output for details.' ) ;
215
+
216
+ console . log ( result . output ) ;
210
217
}
218
+ } )
219
+ . catch ( ( error ) => {
220
+ vscode . window . showErrorMessage ( 'Forge fmt failed. Please check the output for details.' ) ;
221
+ console . error ( error ) ;
222
+ } ) ;
223
+ } ) ;
211
224
212
- const options : ForgeFmtOptions = {
213
- root : vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ,
214
- check : false ,
215
- raw : false ,
216
- } ;
225
+ const formatter = vscode . languages . registerDocumentFormattingEditProvider ( 'solidity' , {
226
+ provideDocumentFormattingEdits : ( document ) => {
227
+ if ( ! isFmtInstalled ( ) ) {
228
+ vscode . window . showErrorMessage ( 'Forge fmt is not installed. Please install it and try again.' ) ;
229
+ return ;
230
+ }
217
231
218
- const args : ForgeFmtArgs = {
219
- options,
220
- files : [ document . fileName ] ,
221
- } ;
232
+ const options : ForgeFmtOptions = {
233
+ root : vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ,
234
+ check : false ,
235
+ raw : false ,
236
+ } ;
222
237
223
- return forgeFmt ( args ) . then ( ( result ) => {
224
- if ( result . exitCode === 0 ) {
225
- vscode . window . showInformationMessage ( "Forge fmt ran successfully." ) ;
226
- } else {
227
- vscode . window . showErrorMessage (
228
- "Forge fmt failed. Please check the output for details." ,
229
- ) ;
238
+ const args : ForgeFmtArgs = {
239
+ options,
240
+ files : [ document . fileName ] ,
241
+ } ;
230
242
231
- console . log ( result . output ) ;
232
- }
243
+ return forgeFmt ( args ) . then ( ( result ) => {
244
+ if ( result . exitCode === 0 ) {
245
+ vscode . window . showInformationMessage ( 'Forge fmt ran successfully.' ) ;
246
+ } else {
247
+ vscode . window . showErrorMessage ( 'Forge fmt failed. Please check the output for details.' ) ;
233
248
234
- return [ ] ;
235
- } ) ;
236
- } ,
249
+ console . log ( result . output ) ;
250
+ }
251
+
252
+ return [ ] ;
253
+ } ) ;
237
254
} ,
238
- ) ;
255
+ } ) ;
239
256
240
257
context . subscriptions . push ( lintSolFile ) ;
241
258
context . subscriptions . push ( lintSolWorkspace ) ;
0 commit comments