From 273645e95ec22a07c1d5d86834591671946bcb53 Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 1 Dec 2015 17:01:22 -0500 Subject: [PATCH 01/10] Adding support for go test --coverprofile on save and via command --- package.json | 15 +++++++++++++-- src/goCheck.ts | 10 +++++++--- src/goMain.ts | 9 +++++++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index b1fe01ff7..6bf902899 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "onCommand:go.gopath", "onCommand:go.test.cursor", "onCommand:go.test.package", - "onCommand:go.test.file" + "onCommand:go.test.file", + "onCommand:go.test.coverage" ], "main": "./out/src/goMain", "contributes": { @@ -94,6 +95,11 @@ "command": "go.test.file", "title": "Go: Run tests in file", "description": "Runs all unit tests in the current file." + }, + { + "command": "go.test.coverage", + "title": "Go: Test coverage in file", + "description": "Displays test coverage in the current file." } ], "debuggers": [ @@ -178,7 +184,7 @@ "type": "boolean", "default": true, "description": "Run 'go build'/'go test' on save." - }, + }, "go.lintOnSave": { "type": "boolean", "default": true, @@ -209,6 +215,11 @@ "default": false, "description": "[EXPERIMENTAL] Run formatting tool on save." }, + "go.coverOnSave": { + "type": "boolean", + "default": false, + "description": "Run 'go test -coverprofile' on save" + }, "go.testTimeout": { "type": "string", "default": "30s", diff --git a/src/goCheck.ts b/src/goCheck.ts index e3f55eef3..88a55bd3b 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -9,7 +9,9 @@ import cp = require('child_process'); import path = require('path'); import os = require('os'); import fs = require('fs'); -import { getBinPath, getGoRuntimePath } from './goPath' +import { getBinPath, getGoRuntimePath } from './goPath'; +import rl = require('readline'); +import { getCoverage, ICoverResult } from './goCover'; if (!getGoRuntimePath()) { vscode.window.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT."); @@ -22,7 +24,7 @@ export interface ICheckResult { severity: string; } -export function check(filename: string, buildOnSave = true, lintOnSave = true, vetOnSave = true): Promise { +export function check(filename: string, buildOnSave = true, lintOnSave = true, vetOnSave = true, coverOnSave = false): Promise { var gobuild = !buildOnSave ? Promise.resolve([]) : new Promise((resolve, reject) => { var tmppath = path.normalize(path.join(os.tmpdir(), "go-code-check")) var cwd = path.dirname(filename) @@ -104,6 +106,8 @@ export function check(filename: string, buildOnSave = true, lintOnSave = true, v } }); }); + + var gocover = !coverOnSave ? Promise.resolve([]) : getCoverage(filename); - return Promise.all([gobuild, golint, govet]).then(resultSets => [].concat.apply([], resultSets)); + return Promise.all([gobuild, golint, govet, getCoverage(filename)]).then(resultSets => [].concat.apply([], resultSets)); } \ No newline at end of file diff --git a/src/goMain.ts b/src/goMain.ts index d8ce9fbfe..9628a522d 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -19,6 +19,7 @@ import { check, ICheckResult } from './goCheck'; import { setupGoPathAndOfferToInstallTools } from './goInstallTools' import { GO_MODE } from './goMode' import { showHideStatus } from './goStatus' +import { coverageCurrentFile } from './goCover'; import { testAtCursor, testCurrentPackage, testCurrentFile } from './goTest' let diagnosticCollection: vscode.DiagnosticCollection; @@ -59,6 +60,10 @@ export function activate(ctx: vscode.ExtensionContext): void { let goConfig = vscode.workspace.getConfiguration('go'); testCurrentFile(goConfig['testTimeout']); })); + + ctx.subscriptions.push(vscode.commands.registerCommand("go.test.coverage", () => { + coverageCurrentFile(); + })); vscode.languages.setLanguageConfiguration(GO_MODE.language, { indentationRules: { @@ -119,9 +124,9 @@ function runBuilds(document: vscode.TextDocument, goConfig: vscode.WorkspaceConf if (document.languageId != "go") { return; } - + var uri = document.uri; - check(uri.fsPath, goConfig['buildOnSave'], goConfig['lintOnSave'], goConfig['vetOnSave']).then(errors => { + check(uri.fsPath, goConfig['buildOnSave'], goConfig['lintOnSave'], goConfig['vetOnSave'], goConfig['coverOnSave']).then(errors => { diagnosticCollection.clear(); let diagnosticMap: Map = new Map();; From a42aecc86258b31f6f8fc10cf42c120d8fcd64d5 Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 1 Dec 2015 17:01:33 -0500 Subject: [PATCH 02/10] Adding support for go test --coverprofile on save and via command --- ...debug.log.0cec55be3f3e55cdcfacfca33bfc420e | 0 ...debug.log.1a5c5ec94407f0ccb6fc35090b747a13 | 0 ...debug.log.6f33c11c551818be558a640005c0948e | 0 ...debug.log.782ed0215d42f6b7a84af58be215a68a | 0 ...debug.log.a0db8a32b6163bd34b2a904d8a83955b | 0 ...debug.log.e5e2027a71385459dbfff6678d9e330a | 0 src/goCover.ts | 100 ++++++++++++++++++ 7 files changed, 100 insertions(+) create mode 100644 npm-debug.log.0cec55be3f3e55cdcfacfca33bfc420e create mode 100644 npm-debug.log.1a5c5ec94407f0ccb6fc35090b747a13 create mode 100644 npm-debug.log.6f33c11c551818be558a640005c0948e create mode 100644 npm-debug.log.782ed0215d42f6b7a84af58be215a68a create mode 100644 npm-debug.log.a0db8a32b6163bd34b2a904d8a83955b create mode 100644 npm-debug.log.e5e2027a71385459dbfff6678d9e330a create mode 100644 src/goCover.ts diff --git a/npm-debug.log.0cec55be3f3e55cdcfacfca33bfc420e b/npm-debug.log.0cec55be3f3e55cdcfacfca33bfc420e new file mode 100644 index 000000000..e69de29bb diff --git a/npm-debug.log.1a5c5ec94407f0ccb6fc35090b747a13 b/npm-debug.log.1a5c5ec94407f0ccb6fc35090b747a13 new file mode 100644 index 000000000..e69de29bb diff --git a/npm-debug.log.6f33c11c551818be558a640005c0948e b/npm-debug.log.6f33c11c551818be558a640005c0948e new file mode 100644 index 000000000..e69de29bb diff --git a/npm-debug.log.782ed0215d42f6b7a84af58be215a68a b/npm-debug.log.782ed0215d42f6b7a84af58be215a68a new file mode 100644 index 000000000..e69de29bb diff --git a/npm-debug.log.a0db8a32b6163bd34b2a904d8a83955b b/npm-debug.log.a0db8a32b6163bd34b2a904d8a83955b new file mode 100644 index 000000000..e69de29bb diff --git a/npm-debug.log.e5e2027a71385459dbfff6678d9e330a b/npm-debug.log.e5e2027a71385459dbfff6678d9e330a new file mode 100644 index 000000000..e69de29bb diff --git a/src/goCover.ts b/src/goCover.ts new file mode 100644 index 000000000..31fa6fa69 --- /dev/null +++ b/src/goCover.ts @@ -0,0 +1,100 @@ +/*--------------------------------------------------------- + * Copyright (C) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------*/ + +'use strict'; + +import vscode = require('vscode'); +import cp = require('child_process'); +import path = require('path'); +import os = require('os'); +import fs = require('fs'); +import { getBinPath, getGoRuntimePath } from './goPath'; +import rl = require('readline'); + +if (!getGoRuntimePath()) { + vscode.window.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT."); +} + +export function coverageCurrentFile() { + var editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage("No editor is active."); + return; + } + getCoverage(editor.document.uri.fsPath); +} + +export function getCoverage(filename: string): Promise { + return new Promise((resolve, reject) => { + var tmppath = path.normalize(path.join(os.tmpdir(), "go-code-cover")) + var cwd = path.dirname(filename) + var args = ["test", "-coverprofile=" + tmppath]; + cp.execFile(getGoRuntimePath(), args, { cwd: cwd }, (err, stdout, stderr) => { + try { + if (err && (err).code == "ENOENT") { + vscode.window.showInformationMessage("Could not generate coverage report. Install Go from http://golang.org/dl/."); + return resolve([]); + } + var ret = []; + + var lines = rl.createInterface({ + input: fs.createReadStream(tmppath), + output: undefined + }); + + var coveredRange = [], + uncoveredRange =[], + uncoveredHighLight = vscode.window.createTextEditorDecorationType({ + // Red + backgroundColor: 'rgba(128,64,64,0.5)', + isWholeLine: false + }), coveredHighLight = vscode.window.createTextEditorDecorationType({ + // Green + backgroundColor: 'rgba(64,128,64,0.5)', + isWholeLine: false + }); + + lines.on('line', function(data: string) { + // go test coverageprofile generates output: + // filename:StartLine.StartColumn,EndLine.EndColumn Hits IsCovered + var fileRange = data.match(/([^:]+)\:([\d]+)\.([\d]+)\,([\d]+)\.([\d]+)\s([\d]+)\s([\d]+)/); + if (fileRange) { + // If line matches active file + if (filename.endsWith(fileRange[1])) { + var range = { + range: new vscode.Range( + // Start Line converted to zero based + parseInt(fileRange[2]) - 1, + // Start Column converted to zero based + parseInt(fileRange[3]) - 1, + // End Line converted to zero based + parseInt(fileRange[4]) - 1, + // End Column converted to zero based + parseInt(fileRange[5]) - 1 + ) + }; + // If is Covered + if (parseInt(fileRange[7]) === 1) { + coveredRange.push(range); + } + // Not Covered + else { + uncoveredRange.push(range); + } + } + } + }); + lines.on('close', function(data) { + // Highlight lines in current editor. + vscode.window.activeTextEditor.setDecorations(uncoveredHighLight, uncoveredRange); + vscode.window.activeTextEditor.setDecorations(coveredHighLight, coveredRange); + resolve(ret); + }); + } catch (e) { + vscode.window.showInformationMessage(e.msg); + reject(e); + } + }); + }); +} \ No newline at end of file From becad32cec16ffafa4222ab4625b2367a1bbea1e Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 1 Dec 2015 17:01:51 -0500 Subject: [PATCH 03/10] removing debug logs --- npm-debug.log.0cec55be3f3e55cdcfacfca33bfc420e | 0 npm-debug.log.1a5c5ec94407f0ccb6fc35090b747a13 | 0 npm-debug.log.6f33c11c551818be558a640005c0948e | 0 npm-debug.log.782ed0215d42f6b7a84af58be215a68a | 0 npm-debug.log.a0db8a32b6163bd34b2a904d8a83955b | 0 npm-debug.log.e5e2027a71385459dbfff6678d9e330a | 0 6 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 npm-debug.log.0cec55be3f3e55cdcfacfca33bfc420e delete mode 100644 npm-debug.log.1a5c5ec94407f0ccb6fc35090b747a13 delete mode 100644 npm-debug.log.6f33c11c551818be558a640005c0948e delete mode 100644 npm-debug.log.782ed0215d42f6b7a84af58be215a68a delete mode 100644 npm-debug.log.a0db8a32b6163bd34b2a904d8a83955b delete mode 100644 npm-debug.log.e5e2027a71385459dbfff6678d9e330a diff --git a/npm-debug.log.0cec55be3f3e55cdcfacfca33bfc420e b/npm-debug.log.0cec55be3f3e55cdcfacfca33bfc420e deleted file mode 100644 index e69de29bb..000000000 diff --git a/npm-debug.log.1a5c5ec94407f0ccb6fc35090b747a13 b/npm-debug.log.1a5c5ec94407f0ccb6fc35090b747a13 deleted file mode 100644 index e69de29bb..000000000 diff --git a/npm-debug.log.6f33c11c551818be558a640005c0948e b/npm-debug.log.6f33c11c551818be558a640005c0948e deleted file mode 100644 index e69de29bb..000000000 diff --git a/npm-debug.log.782ed0215d42f6b7a84af58be215a68a b/npm-debug.log.782ed0215d42f6b7a84af58be215a68a deleted file mode 100644 index e69de29bb..000000000 diff --git a/npm-debug.log.a0db8a32b6163bd34b2a904d8a83955b b/npm-debug.log.a0db8a32b6163bd34b2a904d8a83955b deleted file mode 100644 index e69de29bb..000000000 diff --git a/npm-debug.log.e5e2027a71385459dbfff6678d9e330a b/npm-debug.log.e5e2027a71385459dbfff6678d9e330a deleted file mode 100644 index e69de29bb..000000000 From be08a56aca34a7c69c9a9e5fd4606cd543723709 Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 1 Dec 2015 17:37:21 -0500 Subject: [PATCH 04/10] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6bf902899..fed0341a3 100644 --- a/package.json +++ b/package.json @@ -184,7 +184,7 @@ "type": "boolean", "default": true, "description": "Run 'go build'/'go test' on save." - }, + }, "go.lintOnSave": { "type": "boolean", "default": true, From c62083d3ead8e881c9598273a4c8b5cd123f00b1 Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 1 Dec 2015 17:37:59 -0500 Subject: [PATCH 05/10] removing unnecessary whitespace --- src/goMain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/goMain.ts b/src/goMain.ts index 9628a522d..e22f64220 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -124,7 +124,7 @@ function runBuilds(document: vscode.TextDocument, goConfig: vscode.WorkspaceConf if (document.languageId != "go") { return; } - + var uri = document.uri; check(uri.fsPath, goConfig['buildOnSave'], goConfig['lintOnSave'], goConfig['vetOnSave'], goConfig['coverOnSave']).then(errors => { diagnosticCollection.clear(); From 334357fec3dc5a6e36c6b018fcfe899e9719746b Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 1 Dec 2015 17:40:36 -0500 Subject: [PATCH 06/10] removing unused imports --- src/goCheck.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/goCheck.ts b/src/goCheck.ts index 88a55bd3b..716145503 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -10,8 +10,7 @@ import path = require('path'); import os = require('os'); import fs = require('fs'); import { getBinPath, getGoRuntimePath } from './goPath'; -import rl = require('readline'); -import { getCoverage, ICoverResult } from './goCover'; +import { getCoverage } from './goCover'; if (!getGoRuntimePath()) { vscode.window.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT."); From 18310a9cfe368836c040c41ba45c0eab2f58ce8a Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 1 Dec 2015 17:44:43 -0500 Subject: [PATCH 07/10] fixed go check promise chain to use gocover --- src/goCheck.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/goCheck.ts b/src/goCheck.ts index 716145503..702da810f 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -108,5 +108,5 @@ export function check(filename: string, buildOnSave = true, lintOnSave = true, v var gocover = !coverOnSave ? Promise.resolve([]) : getCoverage(filename); - return Promise.all([gobuild, golint, govet, getCoverage(filename)]).then(resultSets => [].concat.apply([], resultSets)); + return Promise.all([gobuild, golint, govet, gocover]).then(resultSets => [].concat.apply([], resultSets)); } \ No newline at end of file From 23e06e19d6cc357209b1efa78b212f51d3f6d8b4 Mon Sep 17 00:00:00 2001 From: John Adams Date: Wed, 2 Dec 2015 15:00:35 -0500 Subject: [PATCH 08/10] Apply coverage to files onDocumentChange --- package.json | 4 +- src/goCover.ts | 117 ++++++++++++++++++++++++++++++++----------------- src/goMain.ts | 6 ++- 3 files changed, 84 insertions(+), 43 deletions(-) diff --git a/package.json b/package.json index fed0341a3..1b0cb4f35 100644 --- a/package.json +++ b/package.json @@ -98,8 +98,8 @@ }, { "command": "go.test.coverage", - "title": "Go: Test coverage in file", - "description": "Displays test coverage in the current file." + "title": "Go: Test coverage in current package", + "description": "Displays test coverage in the current package." } ], "debuggers": [ diff --git a/src/goCover.ts b/src/goCover.ts index 31fa6fa69..5a46ea451 100644 --- a/src/goCover.ts +++ b/src/goCover.ts @@ -16,7 +16,7 @@ if (!getGoRuntimePath()) { vscode.window.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT."); } -export function coverageCurrentFile() { +export function coverageCurrentPackage() { var editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showInformationMessage("No editor is active."); @@ -25,6 +25,56 @@ export function coverageCurrentFile() { getCoverage(editor.document.uri.fsPath); } +var coveredHighLight = vscode.window.createTextEditorDecorationType({ + // Green + backgroundColor: 'rgba(64,128,64,0.5)', + isWholeLine: false +}), +uncoveredHighLight = vscode.window.createTextEditorDecorationType({ + // Red + backgroundColor: 'rgba(128,64,64,0.5)', + isWholeLine: false +}), +coverageFiles = {}; + +interface coverageFile { + filename: string; + uncoveredRange: vscode.Range[]; + coveredRange: vscode.Range[]; +} + +export function getCodeCoverage(editor: vscode.TextEditor) { + for(var filename in coverageFiles) { + if (editor.document.uri.fsPath.endsWith(filename)) { + highlightCoverage(editor, coverageFiles[filename], false); + } + } +} + +function clearCoverage() { + applyCoverage(true); + coverageFiles = {}; +} + +function applyCoverage(remove: boolean = false) { + for(var filename in coverageFiles) { + var file = coverageFiles[filename]; + // Highlight lines in current editor. + var editor = vscode.window.visibleTextEditors.find((value, index, obj) => { + return value.document.fileName.endsWith(filename); + }); + if (editor) { + highlightCoverage(editor, file, remove); + } + } +} + +function highlightCoverage(editor: vscode.TextEditor, file: coverageFile, remove: boolean) { + editor.setDecorations(uncoveredHighLight, remove ? [] : file.uncoveredRange); + editor.setDecorations(coveredHighLight, remove ? [] : file.coveredRange); + +} + export function getCoverage(filename: string): Promise { return new Promise((resolve, reject) => { var tmppath = path.normalize(path.join(os.tmpdir(), "go-code-cover")) @@ -32,63 +82,52 @@ export function getCoverage(filename: string): Promise { var args = ["test", "-coverprofile=" + tmppath]; cp.execFile(getGoRuntimePath(), args, { cwd: cwd }, (err, stdout, stderr) => { try { + // Clear existing coverage files + clearCoverage(); if (err && (err).code == "ENOENT") { vscode.window.showInformationMessage("Could not generate coverage report. Install Go from http://golang.org/dl/."); return resolve([]); } var ret = []; + var lines = rl.createInterface({ input: fs.createReadStream(tmppath), output: undefined }); - - var coveredRange = [], - uncoveredRange =[], - uncoveredHighLight = vscode.window.createTextEditorDecorationType({ - // Red - backgroundColor: 'rgba(128,64,64,0.5)', - isWholeLine: false - }), coveredHighLight = vscode.window.createTextEditorDecorationType({ - // Green - backgroundColor: 'rgba(64,128,64,0.5)', - isWholeLine: false - }); lines.on('line', function(data: string) { // go test coverageprofile generates output: // filename:StartLine.StartColumn,EndLine.EndColumn Hits IsCovered var fileRange = data.match(/([^:]+)\:([\d]+)\.([\d]+)\,([\d]+)\.([\d]+)\s([\d]+)\s([\d]+)/); if (fileRange) { - // If line matches active file - if (filename.endsWith(fileRange[1])) { - var range = { - range: new vscode.Range( - // Start Line converted to zero based - parseInt(fileRange[2]) - 1, - // Start Column converted to zero based - parseInt(fileRange[3]) - 1, - // End Line converted to zero based - parseInt(fileRange[4]) - 1, - // End Column converted to zero based - parseInt(fileRange[5]) - 1 - ) - }; - // If is Covered - if (parseInt(fileRange[7]) === 1) { - coveredRange.push(range); - } - // Not Covered - else { - uncoveredRange.push(range); - } - } + var coverage = coverageFiles[fileRange[1]] || { coveredRange:[], uncoveredRange:[]}; + + var range = { + range: new vscode.Range( + // Start Line converted to zero based + parseInt(fileRange[2]) - 1, + // Start Column converted to zero based + parseInt(fileRange[3]) - 1, + // End Line converted to zero based + parseInt(fileRange[4]) - 1, + // End Column converted to zero based + parseInt(fileRange[5]) - 1 + ) + }; + // If is Covered + if (parseInt(fileRange[7]) === 1) { + coverage.coveredRange.push(range); + } + // Not Covered + else { + coverage.uncoveredRange.push(range); + } + coverageFiles[fileRange[1]] = coverage; } }); lines.on('close', function(data) { - // Highlight lines in current editor. - vscode.window.activeTextEditor.setDecorations(uncoveredHighLight, uncoveredRange); - vscode.window.activeTextEditor.setDecorations(coveredHighLight, coveredRange); + applyCoverage(); resolve(ret); }); } catch (e) { diff --git a/src/goMain.ts b/src/goMain.ts index e22f64220..bfe78f8f0 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -19,7 +19,7 @@ import { check, ICheckResult } from './goCheck'; import { setupGoPathAndOfferToInstallTools } from './goInstallTools' import { GO_MODE } from './goMode' import { showHideStatus } from './goStatus' -import { coverageCurrentFile } from './goCover'; +import { coverageCurrentPackage, getCodeCoverage } from './goCover'; import { testAtCursor, testCurrentPackage, testCurrentFile } from './goTest' let diagnosticCollection: vscode.DiagnosticCollection; @@ -38,6 +38,8 @@ export function activate(ctx: vscode.ExtensionContext): void { ctx.subscriptions.push(diagnosticCollection); vscode.window.onDidChangeActiveTextEditor(showHideStatus, null, ctx.subscriptions); + vscode.window.onDidChangeActiveTextEditor(getCodeCoverage, null, ctx.subscriptions); + setupGoPathAndOfferToInstallTools(); startBuildOnSaveWatcher(ctx.subscriptions); @@ -62,7 +64,7 @@ export function activate(ctx: vscode.ExtensionContext): void { })); ctx.subscriptions.push(vscode.commands.registerCommand("go.test.coverage", () => { - coverageCurrentFile(); + coverageCurrentPackage(); })); vscode.languages.setLanguageConfiguration(GO_MODE.language, { From d7f9d179715c598bd09e017c88d417155029e5bb Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 8 Dec 2015 11:42:40 -0500 Subject: [PATCH 09/10] removing code coverage for changed files --- src/goCover.ts | 45 +++++++++++++++++++++++++++++++-------------- src/goMain.ts | 5 +++-- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/goCover.ts b/src/goCover.ts index 5a46ea451..48e305a58 100644 --- a/src/goCover.ts +++ b/src/goCover.ts @@ -16,15 +16,6 @@ if (!getGoRuntimePath()) { vscode.window.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT."); } -export function coverageCurrentPackage() { - var editor = vscode.window.activeTextEditor; - if (!editor) { - vscode.window.showInformationMessage("No editor is active."); - return; - } - getCoverage(editor.document.uri.fsPath); -} - var coveredHighLight = vscode.window.createTextEditorDecorationType({ // Green backgroundColor: 'rgba(64,128,64,0.5)', @@ -43,26 +34,52 @@ interface coverageFile { coveredRange: vscode.Range[]; } -export function getCodeCoverage(editor: vscode.TextEditor) { +function clearCoverage() { + applyCoverage(true); + coverageFiles = {}; +} + +export function removeCodeCoverage(e: vscode.TextDocumentChangeEvent) { + var editor = vscode.window.visibleTextEditors.find((value, index, obj) => { + return value.document == e.document; + }); + if (!editor) { + return; + } for(var filename in coverageFiles) { if (editor.document.uri.fsPath.endsWith(filename)) { - highlightCoverage(editor, coverageFiles[filename], false); + highlightCoverage(vscode.window.activeTextEditor, coverageFiles[filename], true); + delete coverageFiles[filename]; } } } -function clearCoverage() { - applyCoverage(true); - coverageFiles = {}; +export function coverageCurrentPackage() { + var editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage("No editor is active."); + return; + } + getCoverage(editor.document.uri.fsPath); +} + +export function getCodeCoverage(editor: vscode.TextEditor) { + for(var filename in coverageFiles) { + if (editor.document.uri.fsPath.endsWith(filename)) { + highlightCoverage(editor, coverageFiles[filename], false); + } + } } function applyCoverage(remove: boolean = false) { + console.log(coverageFiles) for(var filename in coverageFiles) { var file = coverageFiles[filename]; // Highlight lines in current editor. var editor = vscode.window.visibleTextEditors.find((value, index, obj) => { return value.document.fileName.endsWith(filename); }); + console.log(editor); if (editor) { highlightCoverage(editor, file, remove); } diff --git a/src/goMain.ts b/src/goMain.ts index bfe78f8f0..222a99280 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -19,7 +19,7 @@ import { check, ICheckResult } from './goCheck'; import { setupGoPathAndOfferToInstallTools } from './goInstallTools' import { GO_MODE } from './goMode' import { showHideStatus } from './goStatus' -import { coverageCurrentPackage, getCodeCoverage } from './goCover'; +import { coverageCurrentPackage, getCodeCoverage, removeCodeCoverage } from './goCover'; import { testAtCursor, testCurrentPackage, testCurrentFile } from './goTest' let diagnosticCollection: vscode.DiagnosticCollection; @@ -33,10 +33,11 @@ export function activate(ctx: vscode.ExtensionContext): void { ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GO_MODE, new GoDocumentFormattingEditProvider())); ctx.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(GO_MODE, new GoDocumentSymbolProvider())); ctx.subscriptions.push(vscode.languages.registerRenameProvider(GO_MODE, new GoRenameProvider())); - + diagnosticCollection = vscode.languages.createDiagnosticCollection('go'); ctx.subscriptions.push(diagnosticCollection); + vscode.workspace.onDidChangeTextDocument(removeCodeCoverage, null, ctx.subscriptions); vscode.window.onDidChangeActiveTextEditor(showHideStatus, null, ctx.subscriptions); vscode.window.onDidChangeActiveTextEditor(getCodeCoverage, null, ctx.subscriptions); From b7113b37eb12a1faf766cb7332af69225cac0888 Mon Sep 17 00:00:00 2001 From: John Adams Date: Tue, 8 Dec 2015 11:47:27 -0500 Subject: [PATCH 10/10] making sure i'm using the matched editor --- src/goCover.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/goCover.ts b/src/goCover.ts index 48e305a58..76ad8ba8c 100644 --- a/src/goCover.ts +++ b/src/goCover.ts @@ -48,7 +48,7 @@ export function removeCodeCoverage(e: vscode.TextDocumentChangeEvent) { } for(var filename in coverageFiles) { if (editor.document.uri.fsPath.endsWith(filename)) { - highlightCoverage(vscode.window.activeTextEditor, coverageFiles[filename], true); + highlightCoverage(editor, coverageFiles[filename], true); delete coverageFiles[filename]; } }