Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Fix #2551 : Code coverage disappears when typing a character #2853

Merged
merged 6 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
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
33 changes: 29 additions & 4 deletions src/goCover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -288,11 +292,32 @@ export function applyCodeCoverage(editor: vscode.TextEditor) {
}

/**
* Listener for change in the editor.
* A change in a Go file means the coverage data is stale. Therefore it should be cleared.
* Listener for file save that clears potential stale coverage data.
* Local cache tracks files with changes outside of comments to determine
* files for which the save event can cause stale coverage data.
* @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 file change that tracks files with changes outside of comments
* to determine files for which an eventual save can cause stale coverage data.
* @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;
}
Expand All @@ -305,7 +330,7 @@ export function removeCodeCoverageOnFileChange(e: vscode.TextDocumentChangeEvent
return;
}

clearCoverage();
modifiedFiles[e.document.fileName] = true;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
applyCodeCoverage,
applyCodeCoverageToAllEditors,
initCoverageDecorators,
removeCodeCoverageOnFileChange,
removeCodeCoverageOnFileSave,
toggleCoverageCurrentPackage,
trackCodeCoverageRemovalOnFileChange,
updateCodeCoverageDecorators
} from './goCover';
import { GoDebugConfigurationProvider } from './goDebugConfiguration';
Expand Down Expand Up @@ -575,6 +576,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') {
Expand Down Expand Up @@ -607,7 +609,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);
}
Expand Down