Skip to content

Commit

Permalink
Fix microsoft#2551 : Code coverage disappears when typing a character
Browse files Browse the repository at this point in the history
This also preserves an earlier fix to keep coverage when the modifications
are only due to comment edits.

Supersedes microsoft#2586
  • Loading branch information
Kegan Dougal authored and Kegan Dougal committed Oct 22, 2019
1 parent de7296f commit e2ab08e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
31 changes: 28 additions & 3 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 @@ -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;
}
Expand All @@ -273,7 +298,7 @@ export function removeCodeCoverageOnFileChange(e: vscode.TextDocumentChangeEvent
return;
}

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

/**
Expand Down
5 changes: 3 additions & 2 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit e2ab08e

Please sign in to comment.