-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |