@@ -67,8 +67,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<SFCAEx
67
67
const onDidSaveTextDocument = ( listener : ( e : unknown ) => unknown ) : void => {
68
68
context . subscriptions . push ( vscode . workspace . onDidSaveTextDocument ( listener ) ) ;
69
69
}
70
- const onDidOpenTextDocument = ( listener : ( e : unknown ) => unknown ) : void => {
71
- context . subscriptions . push ( vscode . workspace . onDidOpenTextDocument ( listener ) ) ;
70
+ const onDidChangeActiveTextEditor = ( listener : ( e : unknown ) => unknown ) : void => {
71
+ context . subscriptions . push ( vscode . window . onDidChangeActiveTextEditor ( listener ) ) ;
72
72
}
73
73
74
74
// Prepare utilities
@@ -84,6 +84,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<SFCAEx
84
84
const diagnosticManager : DiagnosticManager = new DiagnosticManagerImpl ( diagnosticCollection , telemetryService , logger ) ;
85
85
context . subscriptions . push ( diagnosticManager ) ;
86
86
const codeAnalyzerRunner : CodeAnalyzerRunner = new CodeAnalyzerRunner ( diagnosticManager , settingsManager , telemetryService , logger ) ;
87
+ const scanMonitor : ScanMonitor = new ScanMonitor ( ) ;
88
+ context . subscriptions . push ( scanMonitor ) ;
89
+
87
90
88
91
// We need to do this first in case any other services need access to those provided by the core extension.
89
92
// TODO: Soon we should get rid of this CoreExtensionService stuff in favor of putting things inside of the ExternalServiceProvider
@@ -108,16 +111,25 @@ export async function activate(context: vscode.ExtensionContext): Promise<SFCAEx
108
111
109
112
return codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ document . fileName ] ) ;
110
113
} ) ;
111
- // ... also invoked by opening a file if the user has set things to do so.
112
- onDidOpenTextDocument ( async ( textDocument : vscode . TextDocument ) => {
113
- if ( settingsManager . getAnalyzeOnOpen ( ) && _isValidFileForAnalysis ( textDocument . uri ) ) {
114
- await codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ textDocument . fileName ] ) ;
114
+
115
+ onDidChangeActiveTextEditor ( async ( editor : vscode . TextEditor ) => {
116
+ const shouldScan : boolean = editor !== undefined && editor . document . uri . scheme === 'file' && settingsManager . getAnalyzeOnOpen ( ) &&
117
+ _isValidFileForAnalysis ( editor . document . uri ) && ! scanMonitor . haveAlreadyScannedFile ( editor . document . fileName ) ;
118
+ if ( shouldScan ) {
119
+ scanMonitor . addFileToAlreadyScannedFiles ( editor . document . fileName ) ;
120
+ await codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ editor . document . fileName ] ) ;
115
121
}
116
122
} ) ;
117
- // ... also invoked by saving a file if the user has set things to do so.
118
- onDidSaveTextDocument ( async ( textDocument : vscode . TextDocument ) => {
119
- if ( settingsManager . getAnalyzeOnSave ( ) && _isValidFileForAnalysis ( textDocument . uri ) ) {
120
- await codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ textDocument . fileName ] ) ;
123
+
124
+ onDidSaveTextDocument ( async ( document : vscode . TextDocument ) => {
125
+ if ( document !== undefined && document . uri . scheme === 'file' ) {
126
+ scanMonitor . removeFileFromAlreadyScannedFiles ( document . fileName ) ;
127
+
128
+ const shouldScan : boolean = settingsManager . getAnalyzeOnSave ( ) && _isValidFileForAnalysis ( document . uri ) ;
129
+ if ( shouldScan ) {
130
+ scanMonitor . addFileToAlreadyScannedFiles ( document . fileName ) ;
131
+ await codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ document . fileName ] ) ;
132
+ }
121
133
}
122
134
} ) ;
123
135
@@ -295,8 +307,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<SFCAEx
295
307
// Work Around: For reject & reject all, we really should be restoring the diagnostic that we removed
296
308
// but CodeGenie doesn't let us keep the diagnostic information around at this point. So instead we must
297
309
// rerun the scan instead to get the diagnostic restored.
298
- await document . save ( ) ; // TODO: saving the document should be built in to the runAndDisplay command in my opinion
299
- return codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ document . fileName ] ) ;
310
+ await document . save ( ) ; // TODO: This whole space will be refactored soon so that we don't need to do a save and rerun.
311
+ if ( ! settingsManager . getAnalyzeOnSave ( ) ) {
312
+ return codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ document . fileName ] ) ;
313
+ }
300
314
} ) ;
301
315
302
316
// Invoked by the "Accept All" button on the CodeGenie Unified Diff tool
@@ -322,8 +336,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<SFCAEx
322
336
// Work Around: For reject & reject all, we really should be restoring the diagnostic that we removed
323
337
// but CodeGenie doesn't let us keep the diagnostic information around at this point. So instead we must
324
338
// rerun the scan instead to get the diagnostic restored.
325
- await document . save ( ) ; // TODO: saving the document should be built in to the runAndDisplay command in my opinion
326
- return codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ document . fileName ] ) ;
339
+ await document . save ( ) ; // TODO: This whole space will be refactored soon so that we don't need to do a save and rerun.
340
+ if ( ! settingsManager . getAnalyzeOnSave ( ) ) {
341
+ return codeAnalyzerRunner . runAndDisplay ( Constants . COMMAND_RUN_ON_ACTIVE_FILE , [ document . fileName ] ) ;
342
+ }
327
343
} ) ;
328
344
329
345
@@ -364,7 +380,7 @@ export function deactivate(): void {
364
380
// ....files. So We might not be able to get this perfect. Need to discuss this soon.
365
381
export function _isValidFileForAnalysis ( documentUri : vscode . Uri ) : boolean {
366
382
const allowedFileTypes :string [ ] = [ '.cls' , '.js' , '.apex' , '.trigger' , '.ts' ] ;
367
- return allowedFileTypes . includes ( path . extname ( documentUri . path ) ) ;
383
+ return allowedFileTypes . includes ( path . extname ( documentUri . fsPath ) ) ;
368
384
}
369
385
370
386
// Inside our package.json you'll see things like:
@@ -376,4 +392,24 @@ async function establishVariableInContext(varUsedInPackageJson: string, getValue
376
392
vscode . workspace . onDidChangeConfiguration ( async ( ) => {
377
393
await vscode . commands . executeCommand ( 'setContext' , varUsedInPackageJson , await getValueFcn ( ) ) ;
378
394
} ) ;
395
+ }
396
+
397
+ class ScanMonitor implements vscode . Disposable {
398
+ private alreadyScannedFiles : Set < string > = new Set ( ) ;
399
+
400
+ haveAlreadyScannedFile ( file : string ) : boolean {
401
+ return this . alreadyScannedFiles . has ( file ) ;
402
+ }
403
+
404
+ removeFileFromAlreadyScannedFiles ( file : string ) : void {
405
+ this . alreadyScannedFiles . delete ( file ) ;
406
+ }
407
+
408
+ addFileToAlreadyScannedFiles ( file : string ) {
409
+ this . alreadyScannedFiles . add ( file ) ;
410
+ }
411
+
412
+ public dispose ( ) : void {
413
+ this . alreadyScannedFiles . clear ( ) ;
414
+ }
379
415
}
0 commit comments