Skip to content

Commit bfe9e5b

Browse files
authored
CM-40769 - Code refactoring of Tree View (#106)
1 parent 6bc7f99 commit bfe9e5b

36 files changed

+291
-643
lines changed

CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @MarshalX @MichalBor @MaorDavidzon @artem-fedorov @elsapet @gotbadger @cfabianski
1+
* @MarshalX @elsapet

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default tseslint.config(
4545
'format': ['camelCase', 'UPPER_CASE', 'PascalCase'],
4646
},
4747
],
48-
'@typescript-eslint/no-explicit-any': 'warn', // we will make it error pretty soon
48+
'@typescript-eslint/no-explicit-any': 'error',
4949
'@typescript-eslint/prefer-nullish-coalescing': 'off',
5050
// enabled someday:
5151
'@typescript-eslint/no-unnecessary-condition': 'off', // some of this could be useful

package.json

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@
2828
"main": "./dist/extension.js",
2929
"contributes": {
3030
"menus": {
31-
"commandPalette": [
32-
{
33-
"command": "cycode.openViolationInFileFromTreeItemContextMenu",
34-
"when": "false"
35-
},
36-
{
37-
"command": "cycode.openViolationPanelFromTreeItemContextMenu",
38-
"when": "false"
39-
}
40-
],
4131
"explorer/context": [
4232
{
4333
"when": "explorerResourceIsFolder",
@@ -60,47 +50,39 @@
6050
"view/item/context": [
6151
{
6252
"command": "cycode.secretScanForProject",
63-
"when": "viewItem == Secret",
53+
"when": "viewItem == secretScanTypeNode",
6454
"group": "inline"
6555
},
6656
{
6757
"command": "cycode.scaScan",
68-
"when": "viewItem == SCA",
58+
"when": "viewItem == scaScanTypeNode",
6959
"group": "inline"
7060
},
7161
{
7262
"command": "cycode.iacScanForProject",
73-
"when": "viewItem == IaC",
63+
"when": "viewItem == iacScanTypeNode",
7464
"group": "inline"
7565
},
7666
{
7767
"command": "cycode.sastScanForProject",
78-
"when": "viewItem == SAST",
68+
"when": "viewItem == sastScanTypeNode",
7969
"group": "inline"
8070
},
8171
{
8272
"command": "cycode.secretScanForProject",
83-
"when": "view == cycode.view.tree && viewItem == Secret"
84-
},
85-
{
86-
"command": "cycode.iacScanForProject",
87-
"when": "view == cycode.view.tree && viewItem == IaC"
88-
},
89-
{
90-
"command": "cycode.sastScanForProject",
91-
"when": "view == cycode.view.tree && viewItem == SAST"
73+
"when": "view == cycode.view.tree && viewItem == secretScanTypeNode"
9274
},
9375
{
9476
"command": "cycode.scaScan",
95-
"when": "view == cycode.view.tree && viewItem == SCA"
77+
"when": "view == cycode.view.tree && viewItem == scaScanTypeNode"
9678
},
9779
{
98-
"command": "cycode.openViolationInFileFromTreeItemContextMenu",
99-
"when": "view == cycode.view.tree && viewItem =~ /-file-vulnerability$/"
80+
"command": "cycode.iacScanForProject",
81+
"when": "view == cycode.view.tree && viewItem == iacScanTypeNode"
10082
},
10183
{
102-
"command": "cycode.openViolationPanelFromTreeItemContextMenu",
103-
"when": "view == cycode.view.tree && viewItem =~ /-file-vulnerability$/"
84+
"command": "cycode.sastScanForProject",
85+
"when": "view == cycode.view.tree && viewItem == sastScanTypeNode"
10486
}
10587
]
10688
},
@@ -183,14 +165,6 @@
183165
{
184166
"command": "cycode.auth",
185167
"title": "Authenticate with service"
186-
},
187-
{
188-
"command": "cycode.openViolationInFileFromTreeItemContextMenu",
189-
"title": "Show violation in the file"
190-
},
191-
{
192-
"command": "cycode.openViolationPanelFromTreeItemContextMenu",
193-
"title": "Open violation details"
194168
}
195169
],
196170
"viewsContainers": {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/commands/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import iacScanForCurrentProjectCommand from './iac-scan-for-current-project-comm
99
import sastScanCommand from './sast-scan-command';
1010
import sastScanForCurrentProjectCommand from './sast-scan-for-current-project-command';
1111
import onTreeItemClickCommand from './on-tree-item-click-command';
12-
import onOpenViolationInFileFromTreeItemContextMenuCommand
13-
from './on-open-violation-in-file-from-tree-item-context-menu-command';
14-
import onOpenViolationPanelFromTreeItemContextMenuCommand
15-
from './on-open-violation-panel-from-tree-item-context-menu-command';
1612
import openViolationInFileCommand from './open-violation-in-file-command';
1713
import openViolationPanelCommand from './open-violation-panel-command';
1814
import openSettingsCommand from './open-settings-command';
@@ -39,8 +35,6 @@ export enum VscodeCommands {
3935
OpenViolationInFile = 'cycode.openViolationInFile',
4036
OpenViolationPanel = 'cycode.openViolationPanel',
4137
OnTreeItemClick = 'cycode.onTreeItemClick',
42-
OpenViolationInFileFromTreeItemContextMenu = 'cycode.openViolationInFileFromTreeItemContextMenu',
43-
OpenViolationPanelFromTreeItemContextMenu = 'cycode.openViolationPanelFromTreeItemContextMenu',
4438
}
4539

4640
const _VS_CODE_COMMANDS_ID_TO_CALLBACK: Record<string, (...args: never[]) => unknown> = {
@@ -54,8 +48,6 @@ const _VS_CODE_COMMANDS_ID_TO_CALLBACK: Record<string, (...args: never[]) => unk
5448
[VscodeCommands.SastScanCommandId]: sastScanCommand,
5549
[VscodeCommands.SastScanForProjectCommandId]: sastScanForCurrentProjectCommand,
5650
[VscodeCommands.OnTreeItemClick]: onTreeItemClickCommand,
57-
[VscodeCommands.OpenViolationInFileFromTreeItemContextMenu]: onOpenViolationInFileFromTreeItemContextMenuCommand,
58-
[VscodeCommands.OpenViolationPanelFromTreeItemContextMenu]: onOpenViolationPanelFromTreeItemContextMenuCommand,
5951
[VscodeCommands.OpenViolationInFile]: openViolationInFileCommand,
6052
[VscodeCommands.OpenViolationPanel]: openViolationPanelCommand,
6153
[VscodeCommands.OpenSettingsCommandId]: openSettingsCommand,

src/commands/on-open-violation-in-file-from-tree-item-context-menu-command.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/commands/on-open-violation-panel-from-tree-item-context-menu-command.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as vscode from 'vscode';
22
import { VscodeCommands } from './index';
3-
import { TreeDisplayedData } from '../providers/tree-data/types';
3+
import { CliScanType } from '../cli/models/cli-scan-type';
4+
import { DetectionBase } from '../cli/models/scan-result/detection-base';
45

5-
export default async (fullFilePath: string, violation: TreeDisplayedData) => {
6-
await vscode.commands.executeCommand(VscodeCommands.OpenViolationInFile, fullFilePath, violation.lineNumber);
7-
vscode.commands.executeCommand(VscodeCommands.OpenViolationPanel, violation.detectionType, violation.detection);
6+
export default async (scanType: CliScanType, detection: DetectionBase) => {
7+
await vscode.commands.executeCommand(VscodeCommands.OpenViolationInFile, detection);
8+
await vscode.commands.executeCommand(VscodeCommands.OpenViolationPanel, scanType, detection);
89
};

src/commands/open-violation-in-file-command.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
import * as vscode from 'vscode';
2+
import { DetectionBase } from '../cli/models/scan-result/detection-base';
3+
import { SecretDetection } from '../cli/models/scan-result/secret/secret-detection';
4+
import { ScaDetection } from '../cli/models/scan-result/sca/sca-detection';
5+
import { IacDetection } from '../cli/models/scan-result/iac/iac-detection';
6+
import { SastDetection } from '../cli/models/scan-result/sast/sast-detection';
27

3-
export default async (fullFilePath: string, lineNumber: number) => {
4-
const vscodeLineNumber = lineNumber - 1;
5-
const uri = vscode.Uri.file(fullFilePath);
8+
const VSCODE_LINE_NUMBER_DIFF = 1; // CLI starts counting from 0, although vscode starts from line 1.
9+
10+
export default async (detection: DetectionBase) => {
11+
let vscodeLineNumber = 0;
12+
if (detection instanceof SecretDetection) {
13+
// secret detection line is 0-based
14+
vscodeLineNumber = detection.detectionDetails.line + 1;
15+
} else if (detection instanceof ScaDetection) {
16+
vscodeLineNumber = detection.detectionDetails.lineInFile;
17+
} else if (detection instanceof IacDetection) {
18+
vscodeLineNumber = detection.detectionDetails.lineInFile;
19+
} else if (detection instanceof SastDetection) {
20+
vscodeLineNumber = detection.detectionDetails.lineInFile;
21+
}
22+
23+
vscodeLineNumber -= VSCODE_LINE_NUMBER_DIFF;
24+
25+
const uri = vscode.Uri.file(detection.detectionDetails.getFilepath());
626
await vscode.window.showTextDocument(uri, {
727
viewColumn: vscode.ViewColumn.One,
828
selection: new vscode.Range(vscodeLineNumber, 0, vscodeLineNumber, 0),

src/commands/open-violation-panel-command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ExtensionServiceSymbol } from '../symbols';
55
import { DetectionBase } from '../cli/models/scan-result/detection-base';
66
import { CliScanType } from '../cli/models/cli-scan-type';
77

8-
export default (detectionType: CliScanType, detection: DetectionBase) => {
8+
export default (scanType: CliScanType, detection: DetectionBase) => {
99
const extension = container.resolve<IExtensionService>(ExtensionServiceSymbol);
10-
createAndInitPanel(extension.extensionContext, detectionType, detection);
10+
createAndInitPanel(extension.extensionContext, scanType, detection);
1111
};

src/constants.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ export enum ScanTypeDisplayName {
110110
Iac = 'Infrastructure As Code',
111111
}
112112

113-
export const SEVERITY_PRIORITIES_FIRST_LETTERS: readonly string[] = ['C', 'H', 'M', 'L', 'I'];
114-
export const SEVERITY_PRIORITIES: readonly string[] = ['Critical', 'High', 'Medium', 'Low', 'Info'];
115-
116113
const _SCAN_TYPE_TO_DISPLAY_NAME: Record<string, string> = {
117114
[CliScanType.Secret]: ScanTypeDisplayName.Secrets,
118115
[CliScanType.Sca]: ScanTypeDisplayName.Sca,

src/providers/tree-data/constants.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/providers/tree-data/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import * as vscode from 'vscode';
2-
import { TreeView } from './types';
32
import { TreeDataProvider } from './provider';
4-
import { TreeItem } from './item';
3+
import { BaseNode } from './nodes/base-node';
4+
5+
export interface TreeView {
6+
provider: TreeDataProvider;
7+
view: vscode.TreeView<BaseNode>;
8+
}
59

610
export const createTreeView = (context: vscode.ExtensionContext): TreeView => {
711
const provider = new TreeDataProvider();
8-
const view = vscode.window.createTreeView(TreeItem.viewType, {
12+
const view = vscode.window.createTreeView(TreeDataProvider.viewType, {
913
treeDataProvider: provider,
10-
canSelectMany: true,
14+
showCollapseAll: true,
15+
canSelectMany: false,
1116
});
1217

1318
context.subscriptions.push(
1419
vscode.window.registerTreeDataProvider(
15-
TreeItem.viewType,
20+
TreeDataProvider.viewType,
1621
provider,
1722
),
1823
);

src/providers/tree-data/item.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)