I want to implement something like that.
hover example
Now, here’s some of my extension code. I’m trying to select a code snippet, analyze it and then get some feedback on variables when hovering over them.
Here’s the code for that
export function activate(context: vscode.ExtensionContext) {
// This line of code will only be executed once when the extension is activated
console.log('Congratulations, your "injectguard" is now active!');
let disposable = vscode.commands.registerCommand('injectguard.injection', async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
return;
}
// Retrieve selected code
const selection = editor.selection;
const sourceCode = editor.document.getText(selection);
// Trivial test
const isVulnerable = /d/.test(sourceCode);
// highlight in red if there is an injection vulnerability
const decorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: 'rgba(255,0,0,0.3)' // Light red background
});
if (isVulnerable) {
// Add decoration to the selected text
editor.setDecorations(decorationType, [selection]);
vscode.window.showInformationMessage('Code snippet vulnerable!');
} else {
// Clear decorations if there are no vulnerabilities
editor.setDecorations(decorationType, []);
vscode.window.showInformationMessage('Selected text is fine.');
}
});
context.subscriptions.push(disposable);
}
And package.json
{
"name": "injectguard",
"displayName": "InjectGuard",
"description": "analyzes code sample and determines if it's vulnerable to injection attacks ",
"version": "0.0.1",
"engines": {
"vscode": "^1.89.1"
},
"categories": [
"Other"
],
"capabilities": {
"hoverProvider": "true"
},
"activationEvents": [],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "injectguard.injection",
"title": "InjectGuard : domain analysis "
}
],
"menus": {
"editor/context": [
{
"command": "injectguard.injection",
"group": "bookmarks",
"when": "editorHasSelection"
}
]
}
},
"scripts": {
"generate-client": "openapi-ts --input http://localhost:8000/openapi.json --output ./src/client --client axios",
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "vscode-test"
},
"devDependencies": {
"@hey-api/openapi-ts": "^0.47.2",
"@types/mocha": "^10.0.6",
"@types/node": "18.x",
"@types/vscode": "^1.89.0",
"@typescript-eslint/eslint-plugin": "^7.11.0",
"@typescript-eslint/parser": "^7.11.0",
"@vscode/test-cli": "^0.0.9",
"@vscode/test-electron": "^2.4.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5"
},
"dependencies": {
"axios": "^1.7.2",
"php-parser": "^3.1.5"
}
}
I’ve tried reading the docs but I find it lacking in detail.
Below the links I found when looking for docs :
when looking in the show hover section
I know the code I should be adding looks something like this (asked chatGPT for it)
let disposable = vscode.languages.registerHoverProvider('*', {
provideHover(document, position, token) {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
return;
}
// Retrieve selected code
const selection = editor.selection;
// Get the word at the hovered position
const range = document.getWordRangeAtPosition(position);
const word = document.getText(range);
// Returning a Hover object with content is optional
if(position.line >= selection.start.line && position.line <= selection.end.line )
return new vscode.Hover(`You hovered over the word: ${position.line}`);
}
});
When I tried to run to run this code with the package.json also generated by chatGpt it worked pretty.
Note : the package.json generated by ChatGPT has older version dependencies but seems roughly the same as mine.
Now,
The problem is that I’m trying to add this hover feature to the code I already wrote above. I tried writing two different disposables but the hover disposable doesn’t work while the injectguard disposable works without a problem.
In sum, I’m having trouble understanding how all of this disposable philosophy work and I’d appreciate your feedback on that. Additionally, it would be fantastic if both the inject guard and the hover feature could work simultaneously!
Thanks for sharing !