diff --git a/src/goCover.ts b/src/goCover.ts index 9e1b22015..0ed7a985c 100644 --- a/src/goCover.ts +++ b/src/goCover.ts @@ -29,6 +29,10 @@ let decoratorConfig: { coveredGutterStyle: string; uncoveredGutterStyle: string; }; +// a list of modified, unsaved go files with actual code edits (rather than comment edits) +let modifiedFiles: { + [key: string]: boolean; +}; /** * Initializes the decorators used for Code coverage. @@ -255,12 +259,33 @@ export function applyCodeCoverage(editor: vscode.TextEditor) { } } +/** + * Listener for file save + * A save in a Go file means the coverage data is stale, if it is dirty and the diff + * is in code and not comments. Therefore it should be cleared. + * @param e TextDocument + */ +export function removeCodeCoverageOnFileSave(e: vscode.TextDocument) { + if (e.languageId !== 'go' || !isCoverageApplied || !e.isDirty) { + return; + } + + if (vscode.window.visibleTextEditors.every(editor => editor.document !== e)) { + return; + } + + if (modifiedFiles[e.fileName]) { + clearCoverage(); + modifiedFiles = {}; // reset the list of modified files + } +} + /** * Listener for change in the editor. - * A change in a Go file means the coverage data is stale. Therefore it should be cleared. + * A change in a Go file means the coverage data is stale. Therefore it should be cleared on save. * @param e TextDocumentChangeEvent */ -export function removeCodeCoverageOnFileChange(e: vscode.TextDocumentChangeEvent) { +export function trackCodeCoverageRemovalOnFileChange(e: vscode.TextDocumentChangeEvent) { if (e.document.languageId !== 'go' || !e.contentChanges.length || !isCoverageApplied) { return; } @@ -273,7 +298,7 @@ export function removeCodeCoverageOnFileChange(e: vscode.TextDocumentChangeEvent return; } - clearCoverage(); + modifiedFiles[e.document.fileName] = true; } /** diff --git a/src/goMain.ts b/src/goMain.ts index aca2da210..06c01a4e7 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -10,7 +10,7 @@ import { browsePackages } from './goBrowsePackage'; import { buildCode } from './goBuild'; import { check, notifyIfGeneratedFile, removeTestStatus } from './goCheck'; import { GoCodeActionProvider } from './goCodeAction'; -import { applyCodeCoverage, initCoverageDecorators, removeCodeCoverageOnFileChange, toggleCoverageCurrentPackage, updateCodeCoverageDecorators } from './goCover'; +import { applyCodeCoverage, initCoverageDecorators, trackCodeCoverageRemovalOnFileChange, removeCodeCoverageOnFileSave, toggleCoverageCurrentPackage, updateCodeCoverageDecorators } from './goCover'; import { GoDebugConfigurationProvider } from './goDebugConfiguration'; import { extractFunction, extractVariable } from './goDoctor'; import { runFillStruct } from './goFillStruct'; @@ -385,6 +385,7 @@ function runBuilds(document: vscode.TextDocument, goConfig: vscode.WorkspaceConf } function addOnSaveTextDocumentListeners(ctx: vscode.ExtensionContext) { + vscode.workspace.onDidSaveTextDocument(removeCodeCoverageOnFileSave, null, ctx.subscriptions); vscode.workspace.onDidSaveTextDocument(document => { if (document.languageId !== 'go') { return; @@ -400,7 +401,7 @@ function addOnSaveTextDocumentListeners(ctx: vscode.ExtensionContext) { } function addOnChangeTextDocumentListeners(ctx: vscode.ExtensionContext) { - vscode.workspace.onDidChangeTextDocument(removeCodeCoverageOnFileChange, null, ctx.subscriptions); + vscode.workspace.onDidChangeTextDocument(trackCodeCoverageRemovalOnFileChange, null, ctx.subscriptions); vscode.workspace.onDidChangeTextDocument(removeTestStatus, null, ctx.subscriptions); vscode.workspace.onDidChangeTextDocument(notifyIfGeneratedFile, ctx, ctx.subscriptions); }