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

Commit

Permalink
Code coverage removal fix for single line comments (#1996)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikraobr authored and ramya-rao-a committed Jan 15, 2019
1 parent 43a7d68 commit a7b0fb7
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/goCover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,18 @@ export function applyCodeCoverage(editor: vscode.TextEditor) {
* @param e TextDocumentChangeEvent
*/
export function removeCodeCoverageOnFileChange(e: vscode.TextDocumentChangeEvent) {
if (e.document.languageId !== 'go') {
if (e.document.languageId !== 'go' || !e.contentChanges.length || !isCoverageApplied) {
return;
}

if (vscode.window.visibleTextEditors.every(editor => editor.document !== e.document)) {
return;
}

if (isPartOfComment(e)) {
return;
}

clearCoverage();
}

Expand Down Expand Up @@ -305,3 +311,17 @@ export function toggleCoverageCurrentPackage() {
});
});
}

export function isPartOfComment(e: vscode.TextDocumentChangeEvent): boolean {
return e.contentChanges.every(change => {
// We cannot be sure with using just regex on individual lines whether a multi line change is part of a comment or not
// So play it safe and treat it as not a comment
if (!change.range.isSingleLine || change.text.includes('\n')) {
return false;
}

const text = e.document.lineAt(change.range.start).text;
const idx = text.search('//');
return (idx > -1 && idx <= change.range.start.character);
});
}

0 comments on commit a7b0fb7

Please sign in to comment.