Skip to content

Commit

Permalink
CM-38538 - Fix CodeLens updating
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX committed Jul 31, 2024
1 parent 2ac77fc commit 48074fa
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 38 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [v1.9.2]

- Fix CodeLens updating

## [v1.9.1]

- Integrate Sentry
Expand Down Expand Up @@ -73,6 +77,8 @@

The first stable release with the support of Secrets, SCA, TreeView, Violation Card, and more.

[v1.9.2]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.9.2

[v1.9.1]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.9.1

[v1.9.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.9.0
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/cycodehq/vscode-extension"
},
"homepage": "https://cycode.com/",
"version": "1.9.1",
"version": "1.9.2",
"publisher": "cycode",
"engines": {
"vscode": "^1.63.0"
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export enum ScanType {

export enum ScanTypeDisplayName {
Secrets = 'Hardcoded Secrets',
Sca = 'Open Source Threat (beta)',
Sca = 'Open Source Threat',
Sast = 'Code Security',
Iac = 'Infrastructure As Code',
}
Expand Down
81 changes: 45 additions & 36 deletions src/providers/CodelensProvider.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,77 @@
import * as vscode from 'vscode';
import {extensionId} from '../utils/texts';
import {validateTextRangeInOpenDoc} from '../utils/range';

/**
* Cycode CodelensProvider
*/
export class CodelensProvider implements vscode.CodeLensProvider {
private codeLenses: vscode.CodeLens[] = [];
private _onDidChangeCodeLenses: vscode.EventEmitter<void> =
new vscode.EventEmitter<void>();
public readonly onDidChangeCodeLenses: vscode.Event<void> =
this._onDidChangeCodeLenses.event;

constructor() {
vscode.workspace.onDidChangeTextDocument(() => this.onDidChangeTextDocument(), this);
}

private onDidChangeTextDocument() {
this._onDidChangeCodeLenses.fire();
}

public provideCodeLenses(
document: vscode.TextDocument
): vscode.CodeLens[] | Thenable<vscode.CodeLens[]> {
const diagnostics = vscode.languages.getDiagnostics(document.uri);

const lineObj: { [key: number]: number } = {};
const lineToDetectionsCount: { [key: number]: number } = {};

diagnostics
.filter((diag) => diag.source === extensionId)
.forEach((diagnostic) => {
const range = diagnostic.range;
lineObj[range.start.line] = (lineObj[range.start.line] || 0) + 1;
const codeLens = new vscode.CodeLens(range);
// only count valid detections
if (validateTextRangeInOpenDoc(document.uri, diagnostic.range)) {
const startLine = diagnostic.range.start.line;
lineToDetectionsCount[startLine] = (lineToDetectionsCount[startLine] || 0) + 1;
}
});

const usedLines = new Set<number>();
return diagnostics
.filter((diag) => diag.source === extensionId)
.map((diagnostic) => {
if (!validateTextRangeInOpenDoc(document.uri, diagnostic.range)) {
return null;
}

const startLine = diagnostic.range.start.line;
// Avoid duplicate code lenses on the same line
if (usedLines.has(startLine)) {
return null;
}

const detectionCount = lineToDetectionsCount[startLine];
if (!detectionCount) {
return null;
}

const pluralPart = detectionCount === 1 ? '' : 's';
const title = `Cycode: ${detectionCount} detection${pluralPart}`;

const codeLens = new vscode.CodeLens(diagnostic.range);
codeLens.command = {
title: 'Cycode secret detection',
title,
tooltip: 'Cycode secret detection',
command: '',
arguments: ['Argument 1', false],
};
return codeLens;
});

this.codeLenses = Object.keys(lineObj).map((key: string) => {
const line = parseInt(key);
const range = new vscode.Range(
new vscode.Position(line, 0),
new vscode.Position(line, 0)
);
const codeLens = new vscode.CodeLens(range);
codeLens.command = {
title: `Cycode: ${lineObj[line]} ${
lineObj[line] === 1 ? 'detection' : 'detections'
}`,
tooltip: 'Cycode secret detection',
command: '',
arguments: ['Argument 1', false],
};
return codeLens;
});
usedLines.add(startLine);

return this.codeLenses;
return codeLens;
})
.filter((codeLens) => codeLens !== null) as vscode.CodeLens[];
}

public resolveCodeLens(
codeLens: vscode.CodeLens
) {
codeLens.command = {
title: 'Cycode secret detection',
tooltip: 'Cycode secret detection',
command: 'codelens-sample.codelensAction',
arguments: ['Argument 1', false],
};
return codeLens;
}
}
20 changes: 20 additions & 0 deletions src/utils/range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as vscode from 'vscode';

export const validateTextRangeInOpenDoc = (uri: vscode.Uri, range: vscode.Range): boolean => {
const document = vscode.workspace.textDocuments.find((doc) => doc.uri.toString() === uri.toString());
if (!document) {
// the document is not open; cannot validate
return false;
}

// out of line bounds check
if (range.start.line >= document.lineCount || range.end.line >= document.lineCount) {
return false;
}

const startChar = document.lineAt(range.start.line).range.start.character;
const endChar = document.lineAt(range.end.line).range.end.character;

// out of character bounds check
return !(range.start.character < startChar || range.end.character > endChar);
};

0 comments on commit 48074fa

Please sign in to comment.