@@ -15,49 +15,47 @@ const DEFAULT_HIGHLIGHT_COLORS: HighlightColors = {
1515}
1616
1717export function activate ( context : vscode . ExtensionContext ) : void {
18- const initialConfiguration = getConfiguration ( )
18+ let config = getConfiguration ( )
1919 const sourceHost = new WorkspaceSourceHost ( )
2020 const resolver = new ImportResolver ( sourceHost )
2121 const analyzer = new ComponentLensAnalyzer ( sourceHost , resolver )
22- const decorations = new LensDecorations ( initialConfiguration . highlightColors )
22+ const decorations = new LensDecorations ( config . highlightColors )
2323
2424 context . subscriptions . push ( decorations )
2525
2626 let refreshTimer : NodeJS . Timeout | undefined
2727 let watcherDisposables : vscode . Disposable [ ] = [ ]
2828
29- const clearCachesAndRefresh = (
30- delay = getConfiguration ( ) . debounceMs ,
31- ) : void => {
29+ const clearCachesAndRefresh = ( delay = config . debounceMs ) : void => {
3230 analyzer . clear ( )
3331 scheduleRefresh ( delay )
3432 }
3533
36- const refreshVisibleEditors = ( ) : void => {
37- for ( const editor of vscode . window . visibleTextEditors ) {
38- refreshEditor ( editor )
39- }
34+ const refreshVisibleEditors = async ( ) : Promise < void > => {
35+ await Promise . all (
36+ vscode . window . visibleTextEditors . map ( ( editor ) => refreshEditor ( editor ) ) ,
37+ )
4038 }
4139
42- const scheduleRefresh = ( delay = getConfiguration ( ) . debounceMs ) : void => {
40+ const scheduleRefresh = ( delay = config . debounceMs ) : void => {
4341 if ( refreshTimer ) {
4442 clearTimeout ( refreshTimer )
4543 }
4644
4745 refreshTimer = setTimeout ( ( ) => {
4846 refreshTimer = undefined
49- refreshVisibleEditors ( )
47+ void refreshVisibleEditors ( )
5048 } , delay )
5149 }
5250
53- const refreshEditor = ( editor : vscode . TextEditor ) : void => {
54- if ( ! getConfiguration ( ) . enabled || ! isSupportedDocument ( editor . document ) ) {
51+ const refreshEditor = async ( editor : vscode . TextEditor ) : Promise < void > => {
52+ if ( ! config . enabled || ! isSupportedDocument ( editor . document ) ) {
5553 decorations . clear ( editor )
5654 return
5755 }
5856
5957 const signature = createOpenSignature ( editor . document . version )
60- const usages = analyzer . analyzeDocument (
58+ const usages = await analyzer . analyzeDocument (
6159 editor . document . fileName ,
6260 editor . document . getText ( ) ,
6361 signature ,
@@ -99,9 +97,9 @@ export function activate(context: vscode.ExtensionContext): void {
9997 }
10098
10199 context . subscriptions . push (
102- vscode . commands . registerCommand ( 'reactComponentLens.refresh' , ( ) => {
100+ vscode . commands . registerCommand ( 'reactComponentLens.refresh' , async ( ) => {
103101 analyzer . clear ( )
104- refreshVisibleEditors ( )
102+ await refreshVisibleEditors ( )
105103 void vscode . window . showInformationMessage (
106104 'React Component Lens refreshed.' ,
107105 )
@@ -114,14 +112,17 @@ export function activate(context: vscode.ExtensionContext): void {
114112 vscode . window . onDidChangeVisibleTextEditors ( ( ) => {
115113 scheduleRefresh ( 0 )
116114 } ) ,
117- vscode . workspace . onDidChangeTextDocument ( ( ) => {
118- clearCachesAndRefresh ( )
115+ vscode . workspace . onDidChangeTextDocument ( ( event ) => {
116+ analyzer . invalidateFile ( event . document . fileName )
117+ scheduleRefresh ( )
119118 } ) ,
120- vscode . workspace . onDidOpenTextDocument ( ( ) => {
121- clearCachesAndRefresh ( )
119+ vscode . workspace . onDidOpenTextDocument ( ( document ) => {
120+ analyzer . invalidateFile ( document . fileName )
121+ scheduleRefresh ( )
122122 } ) ,
123- vscode . workspace . onDidSaveTextDocument ( ( ) => {
124- clearCachesAndRefresh ( 0 )
123+ vscode . workspace . onDidSaveTextDocument ( ( document ) => {
124+ analyzer . invalidateFile ( document . fileName )
125+ scheduleRefresh ( 0 )
125126 } ) ,
126127 vscode . workspace . onDidChangeWorkspaceFolders ( ( ) => {
127128 analyzer . clear ( )
@@ -133,8 +134,10 @@ export function activate(context: vscode.ExtensionContext): void {
133134 return
134135 }
135136
137+ config = getConfiguration ( )
138+
136139 if ( event . affectsConfiguration ( 'reactComponentLens.highlightColors' ) ) {
137- decorations . updateColors ( getConfiguration ( ) . highlightColors )
140+ decorations . updateColors ( config . highlightColors )
138141 }
139142
140143 scheduleRefresh ( 0 )
@@ -202,12 +205,12 @@ class WorkspaceSourceHost implements SourceHost {
202205 return createOpenSignature ( openDocument . version )
203206 }
204207
205- if ( ! fs . existsSync ( filePath ) ) {
208+ try {
209+ const stats = fs . statSync ( filePath )
210+ return createDiskSignature ( stats . mtimeMs , stats . size )
211+ } catch {
206212 return undefined
207213 }
208-
209- const stats = fs . statSync ( filePath )
210- return createDiskSignature ( stats . mtimeMs , stats . size )
211214 }
212215
213216 public readFile ( filePath : string ) : string | undefined {
@@ -216,11 +219,40 @@ class WorkspaceSourceHost implements SourceHost {
216219 return openDocument . getText ( )
217220 }
218221
219- if ( ! fs . existsSync ( filePath ) ) {
222+ try {
223+ return fs . readFileSync ( filePath , 'utf8' )
224+ } catch {
225+ return undefined
226+ }
227+ }
228+
229+ public async readFileAsync ( filePath : string ) : Promise < string | undefined > {
230+ const openDocument = this . getOpenDocument ( filePath )
231+ if ( openDocument ) {
232+ return openDocument . getText ( )
233+ }
234+
235+ try {
236+ return await fs . promises . readFile ( filePath , 'utf8' )
237+ } catch {
220238 return undefined
221239 }
240+ }
222241
223- return fs . readFileSync ( filePath , 'utf8' )
242+ public async getSignatureAsync (
243+ filePath : string ,
244+ ) : Promise < string | undefined > {
245+ const openDocument = this . getOpenDocument ( filePath )
246+ if ( openDocument ) {
247+ return createOpenSignature ( openDocument . version )
248+ }
249+
250+ try {
251+ const stats = await fs . promises . stat ( filePath )
252+ return createDiskSignature ( stats . mtimeMs , stats . size )
253+ } catch {
254+ return undefined
255+ }
224256 }
225257
226258 private getOpenDocument ( filePath : string ) : vscode . TextDocument | undefined {
0 commit comments