To create a VS Code extension that prints the number of errors and warnings in the status bar, we'll use the vscode.languages.getDiagnostics() API. This method retrieves all diagnostics (errors, warnings, etc.) for the currently open documents in the workspace.
The extension will:
- Count the number of errors and warnings.
- Display the counts in the status bar.
- Automatically update the status bar when the user edits files.
✅ Step-by-Step Implementation
1. Create Extension Files
Create a new folder for your extension (e.g., problem-counter), and inside it, create these two files:
package.json
{
"name": "problem-counter",
"version": "0.0.1",
"description": "Counts errors and warnings in the status bar",
"main": "extension.js",
"scripts": {
"vscode-eslint": "eslint . --ext .js",
"pretest": "npm run vscode-eslint",
"test": "node ./node_modules/mocha/bin/mocha"
},
"devDependencies": {
"eslint": "^8.56.0",
"mocha": "^10.2.0"
},
"activationEvents": ["*"],
"contributes": {
"commands": []
},
"engines": {
"vscode": "^1.60.0"
}
}
extension.js
const vscode = require('vscode');
function activate(context) {
// Create the status bar item
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
statusBarItem.command = 'workbench.actions.showProblems';
statusBarItem.text = `$(error) 0 $(warning) 0`;
statusBarItem.tooltip = 'Click to show problems';
statusBarItem.show();
// Function to update the status bar
function updateStatusBar() {
let errorCount = 0;
let warningCount = 0;
// Get all diagnostics
const diagnostics = vscode.languages.getDiagnostics();
// Count errors and warnings
for (const [uri, diagnosticsList] of diagnostics) {
for (const diag of diagnosticsList) {
if (diag.severity === vscode.DiagnosticSeverity.Error) {
errorCount++;
} else if (diag.severity === vscode.DiagnosticSeverity.Warning) {
warningCount++;
}
}
}
// Update the status bar
statusBarItem.text = `$(error) ${errorCount} $(warning) ${warningCount}`;
statusBarItem.tooltip = `Errors: ${errorCount}, Warnings: ${warningCount}`;
}
// Initial update
updateStatusBar();
// Update on text document changes
vscode.workspace.onDidChangeTextDocument(() => {
updateStatusBar();
});
// Add to subscriptions to dispose on deactivation
context.subscriptions.push(statusBarItem);
}
function deactivate() {}
module.exports = {
activate,
deactivate
};
🧪 How to Test the Extension
- Open the folder in Visual Studio Code.
- Press
F5 to launch the extension in a new VS Code window.
- Open a file with errors or warnings (e.g., a
.js file with syntax issues).
- You should see the error and warning counts appear in the right side of the status bar.
- Click the status bar to open the Problems panel.
🧠 Notes and Tips
- Limitation:
getDiagnostics() only includes open files. If you want to scan the entire project, consider integrating a linter like ESLint or Pylint.
- Customization: You can enhance this by:
- Filtering by severity.
- Adding icons or colors.
- Showing the count in the title bar or as a badge.
- Performance: This version is lightweight and suitable for small to medium projects. For large workspaces, consider optimizing the diagnostic scanning logic.
🛠️ Optional: Add a Command for Manual Refresh
You can extend the extension to allow manual refresh via a command:
Add to package.json:
"contributes": {
"commands": [
{
"command": "problem-counter.refresh",
"title": "Refresh Problem Count"
}
]
}
Add to extension.js:
vscode.commands.registerCommand('problem-counter.refresh', () => {
updateStatusBar();
});
This simple yet effective extension helps you track and monitor issues in your project in real time, improving your productivity while working in VS Code.