diff --git a/src/extension.ts b/src/extension.ts index 7e75948..feb3250 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -10,6 +10,8 @@ import { looksLikePath, resolvePath, findWorkspaceRoot } from './util/path'; // To keep track of document changes we save hashed versions of their content to this record let documentHashMemory : Record = {}; +let previewAnalysisTimer: NodeJS.Timeout | undefined; +let previewedDocument: vscode.TextDocument | undefined; let cppcheckProgressIndicator: vscode.StatusBarItem; let checksRunning = false; @@ -192,6 +194,39 @@ export async function activate(context: vscode.ExtensionContext) { // Run cppcheck when a file is opened vscode.workspace.onDidOpenTextDocument(handleDocument, null, context.subscriptions); + // Run cppcheck when changing files viewed in text editor + vscode.window.tabGroups.onDidChangeTabs(async e => { + clearTimeout(previewAnalysisTimer); + for (const tab of e.changed) { + if (tab.input instanceof vscode.TabInputText) { + const uri = tab.input.uri; + const document = + vscode.workspace.textDocuments.find( + doc => doc.uri.toString() === uri.toString() + ) ?? await vscode.workspace.openTextDocument(uri); + // Only analyze previewed files if user stays on them for 10 seconds + if (tab && tab.isPreview) { + previewAnalysisTimer = setTimeout(() => { + handleDocument(document); + previewedDocument = document; + }, 10000); + } else { + // If file is properly opened we run analysis right away + handleDocument(document); + } + } + } + }, null, context.subscriptions); + + // Clear diagnostics of previewed files when no longer viewed + vscode.window.onDidChangeActiveTextEditor(() => { + if (previewedDocument) { + diagnosticCollection.delete(previewedDocument.uri); + documentHashMemory[previewedDocument.fileName] = ''; + previewedDocument = undefined; + } + }); + // Run cppcheck for all open files when the workspace is opened vscode.workspace.onDidChangeWorkspaceFolders(() => { vscode.workspace.textDocuments.forEach(handleDocument);