Skip to content

Commit 73708f7

Browse files
authored
fix / #75 run checks when changing file open in active text editor (#76)
* fix / #75 run checks when changing file open in active text editor * wip * fix timer for preview file auto analysis on open * removed console logs * fix preview analysis running * restored fix from other branch to avoid conflict
1 parent 5a7fdb2 commit 73708f7

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/extension.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { looksLikePath, resolvePath, findWorkspaceRoot } from './util/path';
1010
// To keep track of document changes we save hashed versions of their content to this record
1111
let documentHashMemory : Record<string, string> = {};
1212

13+
let previewAnalysisTimer: NodeJS.Timeout | undefined;
14+
let previewedDocument: vscode.TextDocument | undefined;
1315
let cppcheckProgressIndicator: vscode.StatusBarItem;
1416
let checksRunning = false;
1517

@@ -193,6 +195,39 @@ export async function activate(context: vscode.ExtensionContext) {
193195
// Run cppcheck when a file is opened
194196
vscode.workspace.onDidOpenTextDocument(handleDocument, null, context.subscriptions);
195197

198+
// Run cppcheck when changing files viewed in text editor
199+
vscode.window.tabGroups.onDidChangeTabs(async e => {
200+
clearTimeout(previewAnalysisTimer);
201+
for (const tab of e.changed) {
202+
if (tab.input instanceof vscode.TabInputText) {
203+
const uri = tab.input.uri;
204+
const document =
205+
vscode.workspace.textDocuments.find(
206+
doc => doc.uri.toString() === uri.toString()
207+
) ?? await vscode.workspace.openTextDocument(uri);
208+
// Only analyze previewed files if user stays on them for 10 seconds
209+
if (tab && tab.isPreview) {
210+
previewAnalysisTimer = setTimeout(() => {
211+
handleDocument(document);
212+
previewedDocument = document;
213+
}, 10000);
214+
} else {
215+
// If file is properly opened we run analysis right away
216+
handleDocument(document);
217+
}
218+
}
219+
}
220+
}, null, context.subscriptions);
221+
222+
// Clear diagnostics of previewed files when no longer viewed
223+
vscode.window.onDidChangeActiveTextEditor(() => {
224+
if (previewedDocument) {
225+
diagnosticCollection.delete(previewedDocument.uri);
226+
documentHashMemory[previewedDocument.fileName] = '';
227+
previewedDocument = undefined;
228+
}
229+
});
230+
196231
// Run cppcheck for all open files when the workspace is opened
197232
vscode.workspace.onDidChangeWorkspaceFolders(() => {
198233
vscode.workspace.textDocuments.forEach(handleDocument);

0 commit comments

Comments
 (0)