diff --git a/src/extension.ts b/src/extension.ts index 1a03e14..aefec69 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -634,9 +634,11 @@ async function runCppcheckOnFileXML( } : e.$.id; // If warning has a symbol we keep track of it - if (e.symbol?.[0]) { - diagnosticMetadataStore.set(diagnostic, {symbolName: e.symbol?.[0]}); - } + const symbolName = e.symbol?.[0] ?? ''; + // Save line of code at main location if we can access it + const mainLocLine = mainLocDocument?.lineAt(line)?.text ?? ''; + + diagnosticMetadataStore.set(diagnostic, {symbolName, mainLocLine}); // Related Information const relatedInfos: vscode.DiagnosticRelatedInformation[] = []; diff --git a/src/util/codeActions.ts b/src/util/codeActions.ts index 989f16f..5f91f39 100644 --- a/src/util/codeActions.ts +++ b/src/util/codeActions.ts @@ -21,6 +21,15 @@ export class CodeActionProvider implements vscode.CodeActionProvider { continue; } + const mainLocLineNumber = diagnostic.range.start.line; + const lineText = document.lineAt(mainLocLineNumber).text; + const expectedLineText = this.metadataStore.get(diagnostic)?.mainLocLine; + + // If document has been edited so that diagnostic no longer refers to the correct line we don't provide code actions + if (lineText !== expectedLineText) { + continue; + } + var diagnosticCode = diagnostic.code; if (typeof(diagnosticCode) === "object" && typeof(diagnosticCode) !== null) { diagnosticCode = diagnosticCode.value; @@ -33,7 +42,6 @@ export class CodeActionProvider implements vscode.CodeActionProvider { ); // Copy indentation from line affected by diagnostic - const lineText = document.lineAt(diagnostic.range.start.line).text; const indent = lineText.match(/^\s*/)?.[0] ?? ""; // Insert suppression comment above affected line diff --git a/src/util/diagnostics.ts b/src/util/diagnostics.ts index f28fcb2..c48bf6c 100644 --- a/src/util/diagnostics.ts +++ b/src/util/diagnostics.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; interface DiagnosticMetadata { symbolName?: string; + mainLocLine?: string; } export class DiagnosticMetadataStore {