import axios from 'axios'; axios.defaults.timeout = 3000; import * as vscode from 'vscode'; import { spawnSync } from 'child_process'; import * as path from 'path'; import { promisify } from 'util'; const sleep = promisify(setTimeout); export class GocServer { private _serverUrl: string = ''; private timer = true; private highlightDecorationType = vscode.window.createTextEditorDecorationType({ backgroundColor: 'green', border: '2px solid white', color: 'white' });; private lastProfile = ''; construct() { } async startQueryLoop(packages: any[]) { this.timer = true; while ( true === this.timer ) { await sleep(2000); if ( true !== this.timer ) { this.clearHightlight(); return; } this.getConfigurations(); let profile = await this.getLatestProfile(); if (profile == this.lastProfile) { continue; } this.lastProfile = profile; this.renderFile(packages, profile); } } stopQueryLoop() { this.timer = false; this.clearHightlight() } clearHightlight() { vscode.window.visibleTextEditors.forEach(visibleEditor => { visibleEditor.setDecorations(this.highlightDecorationType, []); }); } getConfigurations() { this._serverUrl = vscode.workspace.getConfiguration().get('goc.serverUrl') || ''; } async getLatestProfile(): Promise { let profileApi = `${this._serverUrl}/v1/cover/profile?force=true`; try { let res = await axios.get(profileApi, ); let body: string = res.data.toString(); return body; } catch(err) { console.error(err) } return ""; } getGoList(): Array { let cwd = ""; let workspaces = vscode.workspace.workspaceFolders || []; if (workspaces.length == 0) { console.error("no workspace found"); return []; } else { cwd = workspaces[0].uri.path; } let opts = { 'cwd': cwd }; let output = spawnSync('go', ['list', '-json', './...'], opts); if (output.error != null) { console.error(output.stderr.toString()); return []; } let packages = JSON.parse('[' + output.stdout.toString().replace(/}\n{/g, '},\n{') + ']'); return packages; } renderFile(packages: Array, profile: string) { let activeTextEditor = vscode.window.activeTextEditor; let fileNeedsRender = activeTextEditor?.document.fileName; for (let i=0; i