Here's a complete VS Code extension that displays the number of errors and warnings in the status bar:
1. Create Extension Structure
First, create a new folder for your extension and initialize it:
bash
mkdir diagnostics-counter
cd diagnostics-counter
npm init -y
2. Install Dependencies
bash
npm install --save-dev @types/vscode @types/node typescript
3. Create package.json
json
{
"name": "diagnostics-counter",
"displayName": "Diagnostics Counter",
"description": "Shows error and warning count in status bar",
"version": "0.0.1",
"engines": {
"vscode": "^1.74.0"
},
"categories": ["Other"],
"activationEvents": [
"onStartupFinished"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "diagnosticsCounter.showDetails",
"title": "Show Diagnostics Details"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@types/vscode": "^1.74.0",
"@types/node": "16.x",
"typescript": "^4.9.4"
}
}
4. Create tsconfig.json
json
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"outDir": "out",
"lib": ["ES2020"],
"sourceMap": true,
"rootDir": "src",
"strict": true
},
"exclude": ["node_modules", ".vscode-test"]
}
5. Create src/extension.ts
typescript
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
// Create status bar item
const statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
100
);
// Set command for clicking the status bar item
statusBarItem.command = 'diagnosticsCounter.showDetails';
statusBarItem.tooltip = 'Click to see diagnostics details';
// Function to update the status bar
function updateDiagnosticsCount() {
const diagnostics = vscode.languages.getDiagnostics();
let errorCount = 0;
let warningCount = 0;
let infoCount = 0;
let hintCount = 0;
// Count diagnostics by severity
diagnostics.forEach(([uri, diagnosticsArray]) => {
diagnosticsArray.forEach(diagnostic => {
switch (diagnostic.severity) {
case vscode.DiagnosticSeverity.Error:
errorCount++;
break;
case vscode.DiagnosticSeverity.Warning:
warningCount++;
break;
case vscode.DiagnosticSeverity.Information:
infoCount++;
break;
case vscode.DiagnosticSeverity.Hint:
hintCount++;
break;
}
});
});
// Update status bar text
const totalProblems = errorCount + warningCount;
if (totalProblems === 0) {
statusBarItem.text = `$(check) No Problems`;
statusBarItem.backgroundColor = undefined;
} else {
// Use VS Code icons for errors and warnings
statusBarItem.text = `$(error) ${errorCount} $(warning) ${warningCount}`;
// Set background color based on severity
if (errorCount > 0) {
statusBarItem.backgroundColor = new vscode.ThemeColor('statusBarItem.errorBackground');
} else if (warningCount > 0) {
statusBarItem.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
}
}
// Update tooltip with all counts
statusBarItem.tooltip = `Errors: ${errorCount}\nWarnings: ${warningCount}\nInfo: ${infoCount}\nHints: ${hintCount}\n\nClick for details`;
statusBarItem.show();
}
// Register command to show detailed diagnostics
const showDetailsCommand = vscode.commands.registerCommand('diagnosticsCounter.showDetails', () => {
const diagnostics = vscode.languages.getDiagnostics();
let errorCount = 0;
let warningCount = 0;
let infoCount = 0;
let hintCount = 0;
const fileProblems: Map<string, { errors: number, warnings: number, info: number, hints: number }> = new Map();
// Count diagnostics by file and severity
diagnostics.forEach(([uri, diagnosticsArray]) => {
const filePath = vscode.workspace.asRelativePath(uri);
const counts = { errors: 0, warnings: 0, info: 0, hints: 0 };
diagnosticsArray.forEach(diagnostic => {
switch (diagnostic.severity) {
case vscode.DiagnosticSeverity.Error:
errorCount++;
counts.errors++;
break;
case vscode.DiagnosticSeverity.Warning:
warningCount++;
counts.warnings++;
break;
case vscode.DiagnosticSeverity.Information:
infoCount++;
counts.info++;
break;
case vscode.DiagnosticSeverity.Hint:
hintCount++;
counts.hints++;
break;
}
});
if (diagnosticsArray.length > 0) {
fileProblems.set(filePath, counts);
}
});
// Create detailed message
let message = `**Diagnostics Summary**\n\n`;
message += `Total Errors: ${errorCount}\n`;
message += `Total Warnings: ${warningCount}\n`;
message += `Total Info: ${infoCount}\n`;
message += `Total Hints: ${hintCount}\n\n`;
if (fileProblems.size > 0) {
message += `**Files with problems:**\n`;
fileProblems.forEach((counts, file) => {
message += `\n${file}:\n`;
if (counts.errors > 0) message += ` - Errors: ${counts.errors}\n`;
if (counts.warnings > 0) message += ` - Warnings: ${counts.warnings}\n`;
if (counts.info > 0) message += ` - Info: ${counts.info}\n`;
if (counts.hints > 0) message += ` - Hints: ${counts.hints}\n`;
});
}
// Show in output channel
const outputChannel = vscode.window.createOutputChannel('Diagnostics Counter');
outputChannel.clear();
outputChannel.appendLine(message.replace(/\*\*/g, ''));
outputChannel.show();
// Also show quick pick
vscode.window.showInformationMessage(
`Errors: ${errorCount}, Warnings: ${warningCount}, Info: ${infoCount}, Hints: ${hintCount}`,
'Open Problems Panel'
).then(selection => {
if (selection === 'Open Problems Panel') {
vscode.commands.executeCommand('workbench.actions.view.problems');
}
});
});
// Initial update
updateDiagnosticsCount();
// Update on diagnostics change
const diagnosticsChangeListener = vscode.languages.onDidChangeDiagnostics(() => {
updateDiagnosticsCount();
});
// Update when active editor changes
const activeEditorChangeListener = vscode.window.onDidChangeActiveTextEditor(() => {
updateDiagnosticsCount();
});
// Register disposables
context.subscriptions.push(statusBarItem);
context.subscriptions.push(showDetailsCommand);
context.subscriptions.push(diagnosticsChangeListener);
context.subscriptions.push(activeEditorChangeListener);
}
export function deactivate() {}
6. Create .vscodeignore
code
.vscode/**
.vscode-test/**
src/**
.gitignore
**/tsconfig.json
**/*.map
**/*.ts
7. Build and Test
-
Compile the TypeScript:
bash
npm run compile
-
Open VS Code in the extension directory:
-
Press F5 to run the extension in a new VS Code window
Features of this Extension:
- Real-time Updates: The status bar updates automatically when diagnostics change
- Visual Indicators:
- Uses VS Code icons for errors ((error))andwarnings((warning))
- Shows green checkmark when no problems exist
- Changes background color based on severity
- Detailed Tooltip: Hover over the status bar item to see all diagnostic counts
- Click Action: Click the status bar item to see a detailed breakdown by file
- Smart Display: Only shows errors and warnings in the main text, but tracks all diagnostic types
Usage:
Once activated, the extension will show in the status bar:
✓ No Problems when there are no issues
⚠ 2 ⚠ 5 when there are 2 errors and 5 warnings
- Background turns red for errors, yellow for warnings
You can customize the extension further by:
- Adding configuration options for what to display
- Filtering by workspace or file type
- Adding more detailed statistics
- Creating custom commands for specific diagnostic actions