11import { IDL_LSP_LOG } from '@idl/logger' ;
2- import { IDL_JSON_URI , IDL_NOTEBOOK_EXTENSION } from '@idl/shared' ;
2+ import { PRO_CODE_GLOB_PATTERN } from '@idl/shared' ;
33import { IDL_TRANSLATION } from '@idl/translation' ;
44import {
55 FormatWorkspacePayload ,
@@ -37,12 +37,9 @@ export const ON_FORMAT_WORKSPACE = async (
3737 /**
3838 * Find files and exclude notebooks
3939 */
40- const files = ( await IDL_INDEX . findFiles ( event . folders ) ) . filter (
41- ( file ) =>
42- ! (
43- file . toLowerCase ( ) . endsWith ( IDL_NOTEBOOK_EXTENSION ) ||
44- file . toLowerCase ( ) . endsWith ( IDL_JSON_URI )
45- )
40+ const files = await IDL_INDEX . findFiles (
41+ event . folders ,
42+ PRO_CODE_GLOB_PATTERN
4643 ) ;
4744
4845 /** Track file failures */
@@ -63,74 +60,92 @@ export const ON_FORMAT_WORKSPACE = async (
6360 }
6461 ) ;
6562
63+ /**
64+ * Track all work
65+ */
66+ const promises : Promise < any > [ ] = [ ] ;
67+
6668 /**
6769 * Format each file
6870 */
6971 for ( let i = 0 ; i < files . length ; i ++ ) {
70- try {
71- /**
72- * Resolve the fspath to our cell and retrieve code
73- */
74- const info = await ResolveFSPathAndCodeForURI (
75- URI . file ( files [ i ] ) . toString ( )
76- ) ;
77-
78- // return if nothing found
79- if ( info === undefined ) {
80- return undefined ;
81- }
82-
83- /**
84- * Attempt to format
85- */
86- const formatted = await FormatFile ( {
87- // options are ignored
88- options : {
89- tabSize : 2 ,
90- insertSpaces : true ,
91- } ,
92- textDocument : {
93- uri : info . uri ,
94- } ,
95- } ) ;
96-
97- /**
98- * Check if we failed to format
99- */
100- if ( formatted === undefined ) {
101- failures . push ( files [ i ] ) ;
102- continue ;
103- }
104-
105- // update the doc in VSCode
106- if ( info . doc !== undefined ) {
107- await UpdateDocument ( info . uri , formatted , info . doc ) ;
108- } else {
109- await writeFile ( info . fsPath , formatted , 'utf-8' ) ;
110- }
111- } catch ( err ) {
112- IDL_LANGUAGE_SERVER_LOGGER . log ( {
113- log : IDL_LSP_LOG ,
114- type : 'error' ,
115- content : [
116- `Error trying to format file in workspace: "${ files [ i ] } "` ,
117- err ,
118- ] ,
119- } ) ;
120- failures . push ( files [ i ] ) ;
121- }
122-
123- // update progress
124- SERVER_EVENT_MANAGER . sendNotification (
125- LANGUAGE_SERVER_MESSAGE_LOOKUP . PROGRESS ,
126- {
127- progressId : id ,
128- increment : 100 * ( 1 / files . length ) ,
129- title : IDL_TRANSLATION . lsp . progress . formatWorkspace ,
130- }
72+ promises . push (
73+ // eslint-disable-next-line no-async-promise-executor
74+ new Promise < void > ( async ( res ) => {
75+ try {
76+ /**
77+ * Resolve the fspath to our cell and retrieve code
78+ */
79+ const info = await ResolveFSPathAndCodeForURI (
80+ URI . file ( files [ i ] ) . toString ( )
81+ ) ;
82+
83+ // return if nothing found
84+ if ( info === undefined ) {
85+ res ( ) ;
86+ return ;
87+ }
88+
89+ /**
90+ * Attempt to format
91+ */
92+ const formatted = await FormatFile ( {
93+ // options are ignored
94+ options : {
95+ tabSize : 2 ,
96+ insertSpaces : true ,
97+ } ,
98+ textDocument : {
99+ uri : info . uri ,
100+ } ,
101+ } ) ;
102+
103+ /**
104+ * Check if we failed to format
105+ */
106+ if ( formatted === undefined ) {
107+ failures . push ( files [ i ] ) ;
108+ res ( ) ;
109+ return ;
110+ }
111+
112+ // update the doc in VSCode
113+ if ( info . doc !== undefined ) {
114+ await UpdateDocument ( info . uri , formatted , info . doc ) ;
115+ } else {
116+ await writeFile ( info . fsPath , formatted , 'utf-8' ) ;
117+ }
118+ } catch ( err ) {
119+ IDL_LANGUAGE_SERVER_LOGGER . log ( {
120+ log : IDL_LSP_LOG ,
121+ type : 'error' ,
122+ content : [
123+ `Error trying to format file in workspace: "${ files [ i ] } "` ,
124+ err ,
125+ ] ,
126+ } ) ;
127+ failures . push ( files [ i ] ) ;
128+ }
129+
130+ // update progress
131+ SERVER_EVENT_MANAGER . sendNotification (
132+ LANGUAGE_SERVER_MESSAGE_LOOKUP . PROGRESS ,
133+ {
134+ progressId : id ,
135+ increment : 100 * ( 1 / files . length ) ,
136+ title : IDL_TRANSLATION . lsp . progress . formatWorkspace ,
137+ }
138+ ) ;
139+
140+ // finish promise
141+ res ( ) ;
142+ } )
131143 ) ;
132144 }
133145
146+ // wait for promises to finish
147+ await Promise . all ( promises ) ;
148+
134149 // update progress
135150 SERVER_EVENT_MANAGER . sendNotification (
136151 LANGUAGE_SERVER_MESSAGE_LOOKUP . PROGRESS ,
0 commit comments