Skip to content

Commit 7895c39

Browse files
authored
@W-15093436@ trigger scan on save when a file is saved (#48)
* @W-15093436@ trigger scan on save when a file is saved * review comments * change flag description based on UI review + turning the flag off by default
1 parent 83a8745 commit 7895c39

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
"lint": "eslint src --ext ts",
6969
"test": "node ./out/test/runTest.js"
7070
},
71+
"activationEvents": [
72+
"workspaceContains:sfdx-project.json"
73+
],
7174
"main": "./out/extension.js",
7275
"contributes": {
7376
"commands": [
@@ -111,10 +114,10 @@
111114
"type": "string",
112115
"description": "The Java Virtual Machine (JVM) arguments used to optimize Salesforce Graph Engine execution for your system."
113116
},
114-
"codeAnalyzer.analyze-on-save.enabled": {
117+
"codeAnalyzer.analyzeOnSave.enabled": {
115118
"type": "boolean",
116-
"default": true,
117-
"description": "Automatically scans the current file on save."
119+
"default": false,
120+
"description": "Scan the current file on save automatically."
118121
}
119122
}
120123
},

src/extension.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import * as vscode from 'vscode';
1010
import * as targeting from './lib/targeting';
1111
import {ScanRunner} from './lib/scanner';
12+
import {SettingsManager} from './lib/settings';
1213
import {SfCli} from './lib/sf-cli';
1314

1415
import {RuleResult} from './types';
@@ -31,7 +32,8 @@ type RunInfo = {
3132
let diagnosticCollection: vscode.DiagnosticCollection = null;
3233

3334
/**
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
3537
* Registers the necessary diagnostic collections and commands.
3638
*/
3739
export async function activate(context: vscode.ExtensionContext): Promise<vscode.ExtensionContext> {
@@ -40,8 +42,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<vscode
4042
// We need to do this first in case any other services need access to those provided by the core extension.
4143
await CoreExtensionService.loadDependencies(context);
4244

43-
console.log(`Extension sfdx-code-analyzer-vscode activated.`);
44-
4545
// Define a diagnostic collection in the `activate()` scope so it can be used repeatedly.
4646
diagnosticCollection = vscode.languages.createDiagnosticCollection('sfca');
4747
context.subscriptions.push(diagnosticCollection);
@@ -74,6 +74,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<vscode
7474
outputChannel
7575
});
7676
});
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.');
7780
const graphEngineStatus: vscode.StatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
7881
graphEngineStatus.name = messages.graphEngine.statusBarName;
7982
context.subscriptions.push(graphEngineStatus);
@@ -85,6 +88,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<vscode
8588
});
8689
context.subscriptions.push(runOnActiveFile, runOnSelected, runDfaOnSelectedMethod);
8790
TelemetryService.sendExtensionActivationEvent(extensionHrStart);
91+
outputChannel.appendLine(`Extension sfdx-code-analyzer-vscode activated.`);
8892
return Promise.resolve(context);
8993
}
9094

@@ -99,6 +103,23 @@ async function verifyPluginInstallation(): Promise<void> {
99103
}
100104
}
101105

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+
102123
/**
103124
* Runs non-Path-based rules against the selected files/directories, or the active file if nothing was selected.
104125
* @param selections The files/directories manually selected by the user.

src/lib/settings.ts

+4
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ export class SettingsManager {
2929
public static getGraphEngineJvmArgs(): string {
3030
return vscode.workspace.getConfiguration('codeAnalyzer.graphEngine').get('jvmArgs');
3131
}
32+
33+
public static getAnalyzeOnSave(): boolean {
34+
return vscode.workspace.getConfiguration('codeAnalyzer.analyzeOnSave').get('enabled');
35+
}
3236
}

0 commit comments

Comments
 (0)