Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as vscode from 'vscode';
import * as cp from 'child_process';
import * as xml2js from 'xml2js';
import * as crypto from 'crypto';

import { documentationLinkMap, getPremiumCertLink } from './util/documentation';
import { runCommand } from './util/scripts';
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<string, string> = {};

enum SeverityNumber {
Info = 0,
Warning = 1,
Expand Down Expand Up @@ -64,6 +68,13 @@ function parseMinSeverity(str: string): SeverityNumber {
}
}

function getDocumentSha1(document: vscode.TextDocument): string {
return crypto
.createHash('sha1')
.update(document.getText(), 'utf8')
.digest('hex');
}

// This method is called when your extension is activated.
// Your extension is activated the very first time the command is executed.
export async function activate(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -92,6 +103,15 @@ export async function activate(context: vscode.ExtensionContext) {
return;
}

if ((Object.keys(documentHashMemory) as Array<string>).includes(document.fileName)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can there be some situation when results are not shown for a file and user wants to save to trigger analysis? is cppcheck always executed when a file is opened? even if user doesn't open the file but vscode opens the file automatically during startup or something?

// Check file content against memory, if it has not changed since last check do early return
const newHash = getDocumentSha1(document);
const oldHash = documentHashMemory[document.fileName];
if (newHash === oldHash) {
return;
}
}

// Check if the document is visible in any editor
const isVisible = vscode.window.visibleTextEditors.some(editor =>
editor.document.uri.toString().replaceAll('\\', '/') === document.uri.toString().replaceAll('\\', '/'));
Expand Down Expand Up @@ -148,6 +168,10 @@ export async function activate(context: vscode.ExtensionContext) {
minSevString,
diagnosticCollection
);

// Save hashed document content to memory
const hashedContentOfFile = getDocumentSha1(document);
documentHashMemory[document.fileName] = hashedContentOfFile;
}

// Listen for file saves.
Expand Down
Loading