9
9
import * as vscode from 'vscode' ;
10
10
import * as targeting from './lib/targeting' ;
11
11
import { ScanRunner } from './lib/scanner' ;
12
+ import { SettingsManager } from './lib/settings' ;
12
13
import { SfCli } from './lib/sf-cli' ;
13
14
14
15
import { RuleResult } from './types' ;
@@ -31,7 +32,8 @@ type RunInfo = {
31
32
let diagnosticCollection : vscode . DiagnosticCollection = null ;
32
33
33
34
/**
34
- * This method is invoked when the extension is first activated (i.e., the very first time the command is executed).
35
+ * This method is invoked when the extension is first activated (this is currently configured to be when a sfdx project is loaded).
36
+ * The activation trigger can be changed by changing activationEvents in package.json
35
37
* Registers the necessary diagnostic collections and commands.
36
38
*/
37
39
export async function activate ( context : vscode . ExtensionContext ) : Promise < vscode . ExtensionContext > {
@@ -40,8 +42,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<vscode
40
42
// We need to do this first in case any other services need access to those provided by the core extension.
41
43
await CoreExtensionService . loadDependencies ( context ) ;
42
44
43
- console . log ( `Extension sfdx-code-analyzer-vscode activated.` ) ;
44
-
45
45
// Define a diagnostic collection in the `activate()` scope so it can be used repeatedly.
46
46
diagnosticCollection = vscode . languages . createDiagnosticCollection ( 'sfca' ) ;
47
47
context . subscriptions . push ( diagnosticCollection ) ;
@@ -74,6 +74,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<vscode
74
74
outputChannel
75
75
} ) ;
76
76
} ) ;
77
+ outputChannel . appendLine ( `Registered command as part of sfdx-code-analyzer-vscode activation.` ) ;
78
+ registerScanOnSave ( outputChannel ) ;
79
+ outputChannel . appendLine ( 'Registered scanOnSave as part of sfdx-code-analyzer-vscode activation.' ) ;
77
80
const graphEngineStatus : vscode . StatusBarItem = vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Left , 100 ) ;
78
81
graphEngineStatus . name = messages . graphEngine . statusBarName ;
79
82
context . subscriptions . push ( graphEngineStatus ) ;
@@ -85,6 +88,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<vscode
85
88
} ) ;
86
89
context . subscriptions . push ( runOnActiveFile , runOnSelected , runDfaOnSelectedMethod ) ;
87
90
TelemetryService . sendExtensionActivationEvent ( extensionHrStart ) ;
91
+ outputChannel . appendLine ( `Extension sfdx-code-analyzer-vscode activated.` ) ;
88
92
return Promise . resolve ( context ) ;
89
93
}
90
94
@@ -99,6 +103,23 @@ async function verifyPluginInstallation(): Promise<void> {
99
103
}
100
104
}
101
105
106
+ export function registerScanOnSave ( outputChannel : vscode . LogOutputChannel ) {
107
+ vscode . workspace . onDidSaveTextDocument (
108
+ async ( textDocument : vscode . TextDocument ) => {
109
+ const documentUri = textDocument . uri ;
110
+ if (
111
+ SettingsManager . getAnalyzeOnSave ( )
112
+ ) {
113
+ await _runAndDisplayPathless ( [ documentUri ] , {
114
+ commandName : Constants . COMMAND_RUN_ON_ACTIVE_FILE ,
115
+ diagnosticCollection,
116
+ outputChannel
117
+ } ) ;
118
+ }
119
+ }
120
+ ) ;
121
+ }
122
+
102
123
/**
103
124
* Runs non-Path-based rules against the selected files/directories, or the active file if nothing was selected.
104
125
* @param selections The files/directories manually selected by the user.
0 commit comments