const vscode = require('vscode') const cp = require('child_process') function activate(context) { let panel = null function refreshPanel(stdout, stderr) { const uri = vscode.window.activeTextEditor.document.uri const { pngPath, refPath } = getPaths(uri) if (panel && panel.visible) { console.log('Refreshing WebView') const pngSrc = panel.webview.asWebviewUri(pngPath) const refSrc = panel.webview.asWebviewUri(refPath) panel.webview.html = '' // Make refresh notable. setTimeout(() => { panel.webview.html = getWebviewContent(pngSrc, refSrc, stdout, stderr) }, 50) } } const openCmd = vscode.commands.registerCommand("ShortcutMenuBar.testOpen", () => { panel = vscode.window.createWebviewPanel( 'testOutput', 'Test output', vscode.ViewColumn.Beside, {} ) refreshPanel("", "") }) const refreshCmd = vscode.commands.registerCommand("ShortcutMenuBar.testRefresh", () => { refreshPanel("", "") }) const rerunCmd = vscode.commands.registerCommand("ShortcutMenuBar.testRerun", () => { const uri = vscode.window.activeTextEditor.document.uri const components = uri.fsPath.split(/tests[\/\\]/) const dir = components[0] const subPath = components[1] cp.exec( `cargo test --manifest-path ${dir}/Cargo.toml --all --test tests -- ${subPath}`, (err, stdout, stderr) => { console.log('Ran tests') refreshPanel(stdout, stderr) } ) }) const approveCmd = vscode.commands.registerCommand("ShortcutMenuBar.testApprove", () => { const uri = vscode.window.activeTextEditor.document.uri const { pngPath, refPath } = getPaths(uri) vscode.workspace.fs.copy(pngPath, refPath, { overwrite: true }).then(() => { console.log('Copied to reference file') cp.exec(`oxipng -o max -a ${refPath.fsPath}`, (err, stdout, stderr) => { refreshPanel(stdout, stderr) }) }) }) context.subscriptions.push(openCmd) context.subscriptions.push(refreshCmd) context.subscriptions.push(rerunCmd) context.subscriptions.push(approveCmd) } function getPaths(uri) { const pngPath = vscode.Uri.file(uri.path .replace("tests/typ", "tests/png") .replace(".typ", ".png")) const refPath = vscode.Uri.file(uri.path .replace("tests/typ", "tests/ref") .replace(".typ", ".png")) return { pngPath, refPath } } function getWebviewContent(pngSrc, refSrc, stdout, stderr) { return ` Test output

Output

Reference

Standard output

${escape(stdout)}

Standard error

${escape(stderr)}
` } function escape(text) { return text.replace(//g, ">"); } function deactivate() {} module.exports = { activate, deactivate }