UNPKG

w1-system-font-validator

Version:

VS Code extension for validating W1 System font variables (both fontConfig.json and localFontConfig.json)

122 lines (102 loc) 4.35 kB
import * as vscode from "vscode"; import { UnifiedFontValidator } from "./validators/unifiedValidator"; let validator: UnifiedFontValidator; export function activate(context: vscode.ExtensionContext) { try { // Create the unified font validator validator = new UnifiedFontValidator(); } catch (error) { console.error("W1 Font Validator: Failed to initialize validator:", error); vscode.window.showErrorMessage("W1 Font Validator failed to initialize: " + error); return; } // Register commands const validateCommand = vscode.commands.registerCommand("w1FontValidator.validateFonts", () => { const activeEditor = vscode.window.activeTextEditor; if (activeEditor) { const fileName = activeEditor.document.fileName; const baseName = fileName.split(/[/\\]/).pop() || ""; const shouldValidate = shouldValidateDocument(activeEditor.document); if (shouldValidate) { validator.validateDocument(activeEditor.document); vscode.window.showInformationMessage("W1 Font validation complete!"); } else { vscode.window.showInformationMessage("No active CSS file to validate."); } } }); const refreshConfigCommand = vscode.commands.registerCommand("w1FontValidator.refreshConfig", () => { validator.refreshConfig(); vscode.window.showInformationMessage("Font configuration refreshed!"); }); // Register document change listener for real-time validation WITH DEBOUNCING const documentChangeListener = vscode.workspace.onDidChangeTextDocument((event) => { if (shouldValidateDocument(event.document)) { const config = vscode.workspace.getConfiguration("w1FontValidator"); if (config.get("validateOnType", true)) { validator.validateDocumentDebounced(event.document); } } }); // Register document open listener const documentOpenListener = vscode.workspace.onDidOpenTextDocument((document) => { if (shouldValidateDocument(document)) { validator.validateDocument(document); } }); // Register hover provider for font information const hoverProvider = vscode.languages.registerHoverProvider(["css", "scss", "less"], { provideHover(document, position, token) { const config = vscode.workspace.getConfiguration("w1FontValidator"); if (config.get("showHoverInfo", true)) { return validator.provideHover(document, position); } return undefined; }, }); // Register document close listener for cleanup const documentCloseListener = vscode.workspace.onDidCloseTextDocument((document) => { if (shouldValidateDocument(document)) { validator.cleanupDocument(document); } }); // Add to subscriptions for cleanup context.subscriptions.push(validateCommand, refreshConfigCommand, documentChangeListener, documentOpenListener, hoverProvider, documentCloseListener); // Validate all currently open documents vscode.workspace.textDocuments.forEach((document) => { if (shouldValidateDocument(document)) { validator.validateDocument(document); } }); } export function deactivate() { // Proper cleanup of the validator if (validator) { validator.dispose(); } } /** * Check if document should be validated */ function shouldValidateDocument(document: vscode.TextDocument): boolean { const fileName = document.fileName; // Exclude certain directories if (fileName.includes("node_modules") || fileName.includes(".vscode") || fileName.includes("dist")) { return false; } // Use path.basename for cross-platform compatibility const baseName = fileName.split(/[/\\]/).pop() || ""; // CSS Modules if (baseName.endsWith(".module.css") || baseName.endsWith(".module.scss") || baseName.endsWith(".module.less")) { return true; } // Global CSS files - Always validate regardless of location if (baseName === "globals.css" || baseName === "global.css" || baseName === "global.scss") { return true; } // Files in styles directories (check for both forward and back slashes) if ((fileName.includes("/styles/") || fileName.includes("\\styles\\")) && (baseName.endsWith(".css") || baseName.endsWith(".scss"))) { return true; } return false; }