Skip to content

Commit

Permalink
Optimised with new API response
Browse files Browse the repository at this point in the history
- Now extension only highlights the required part of code not the whole line
- Severity Label added
  • Loading branch information
typhonshambo committed Jul 9, 2024
1 parent 637d2a2 commit 66298e9
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@ import axios from 'axios';
interface Issue {
line: number;
message: string;
start_char: number;
end_char: number;
severity: string;
}

let analyzedDocuments: Map<string, Issue[]> = new Map();
let diagnosticCollection: vscode.DiagnosticCollection;
const diagnosticCollection = vscode.languages.createDiagnosticCollection('codeAnalyzer');

// Convert severity from string to DiagnosticSeverity
function getSeverity(severity: string): vscode.DiagnosticSeverity {
switch (severity.toUpperCase()) {
case 'ERROR':
return vscode.DiagnosticSeverity.Error;
case 'WARNING':
return vscode.DiagnosticSeverity.Warning;
case 'INFORMATION':
return vscode.DiagnosticSeverity.Information;
case 'HINT':
return vscode.DiagnosticSeverity.Hint;
default:
return vscode.DiagnosticSeverity.Information;
}
}

// Command to analyze code
const analyzeCommand = vscode.commands.registerCommand('extension.analyzeCode', async () => {
Expand All @@ -32,12 +51,13 @@ const analyzeCommand = vscode.commands.registerCommand('extension.analyzeCode',

if (styleGuide && Array.isArray(styleGuide.issues)) {
vscode.window.showInformationMessage('Code analysis complete!');

// Store issues for the current document
analyzedDocuments.set(document.uri.toString(), styleGuide.issues);

// Update diagnostics
updateDiagnostics(document, styleGuide.issues);

} else {
vscode.window.showErrorMessage('Code analysis failed or returned unexpected results.');
}
Expand All @@ -55,9 +75,8 @@ const analyzeCommand = vscode.commands.registerCommand('extension.analyzeCode',

function updateDiagnostics(document: vscode.TextDocument, issues: Issue[]) {
const diagnostics: vscode.Diagnostic[] = issues.map(issue => {
const line = issue.line - 1;
const range = new vscode.Range(line, 0, line, document.lineAt(line).range.end.character);
const diagnostic = new vscode.Diagnostic(range, issue.message, vscode.DiagnosticSeverity.Warning);
const range = new vscode.Range(new vscode.Position(issue.line - 1, issue.start_char), new vscode.Position(issue.line - 1, issue.end_char));
const diagnostic = new vscode.Diagnostic(range, issue.message, getSeverity(issue.severity));
diagnostic.source = 'Code Analyzer';
return diagnostic;
});
Expand All @@ -69,21 +88,32 @@ export function activate(context: vscode.ExtensionContext) {
const outputChannel = vscode.window.createOutputChannel("Code Analyzer");
context.subscriptions.push(outputChannel);

diagnosticCollection = vscode.languages.createDiagnosticCollection('codeAnalyzer');
context.subscriptions.push(diagnosticCollection);

context.subscriptions.push(analyzeCommand);

const statusBarButton = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarButton.text = "$(beaker) Analyze Code";
statusBarButton.command = "extension.analyzeCode";
statusBarButton.show();
context.subscriptions.push(statusBarButton);

context.subscriptions.push(diagnosticCollection);

vscode.commands.registerCommand('extension.showIssueDetails', (issue) => {
outputChannel.clear();
outputChannel.appendLine("Issue Details:");
outputChannel.appendLine(`Line ${issue.line}: ${issue.message}`);
outputChannel.show();
});

vscode.workspace.onDidOpenTextDocument((document) => {
// Clear diagnostics for other documents
if (!analyzedDocuments.has(document.uri.toString())) {
diagnosticCollection.delete(document.uri);
}
});
}

export function deactivate() {
analyzedDocuments.clear();
if (diagnosticCollection) {
diagnosticCollection.dispose();
}
diagnosticCollection.clear();
}

0 comments on commit 66298e9

Please sign in to comment.