1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
import { ExtensionContext, languages, commands, Disposable, workspace, window } from 'vscode'; import * as vscode from 'vscode';
let disposables: Disposable[] = [];
class CodelensProvider implements vscode.CodeLensProvider {
private _onDidChangeCodeLenses: vscode.EventEmitter<void> = new vscode.EventEmitter<void>(); public readonly onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
constructor() { vscode.workspace.onDidChangeConfiguration((_) => { this._onDidChangeCodeLenses.fire(); }); }
public provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.CodeLens[] | Thenable<vscode.CodeLens[]> {
if (vscode.workspace.getConfiguration("survey-helper").get("enableCodeLens", true)) { let lines = document.getText().split("\n");
let line = 0; for (let i = 0; i < lines.length; i++) { if (lines[i].startsWith("class Solution:")) { line = i; } }
let test: vscode.Command = { command: "survey-helper.test", title: "Test" };
let submit: vscode.Command = { command: "survey-helper.submit", title: "Submit" };
let codeLens = new vscode.CodeLens(new vscode.Range(new vscode.Position(line, 0), new vscode.Position(line, 5)), test); let codeLens1 = new vscode.CodeLens(new vscode.Range(new vscode.Position(line, 6), new vscode.Position(line, 11)), submit);
return [codeLens, codeLens1]; } return []; }
public resolveCodeLens(codeLens: vscode.CodeLens, token: vscode.CancellationToken) { if (vscode.workspace.getConfiguration("survey-helper").get("enableCodeLens", true)) { return codeLens; } return null; } }
export function activate(context: ExtensionContext) { const codelensProvider = new CodelensProvider();
languages.registerCodeLensProvider("*", codelensProvider);
commands.registerCommand("survey-helper.enableCodeLens", () => { workspace.getConfiguration("survey-helper").update("enableCodeLens", true, true); });
commands.registerCommand("survey-helper.disableCodeLens", () => { workspace.getConfiguration("survey-helper").update("enableCodeLens", false, true); });
let survey = vscode.window.createOutputChannel("survey-helper"); survey.appendLine("Start services"); let disposable = vscode.commands.registerCommand('survey-helper.test', async () => { const editor = vscode.window.activeTextEditor; if (editor) { let document = editor.document; let documentText = document.getText(); await document.save(); if (vscode.workspace.getConfiguration('survey-helper').get("endpoint") !== "null") { let response: Response = await fetch(String(vscode.workspace.getConfiguration('survey-helper').get("endpoint")) + "/test?problem_id=" + document.fileName.replace(/^.*[\\\/]/, '').charAt(0), { method: 'GET' }); let testResult = await response.json(); let dateOb = new Date(); let hours = dateOb.getHours(); let minutes = dateOb.getMinutes(); let seconds = dateOb.getSeconds(); survey.appendLine("[" + hours + ":" + minutes + ":" + seconds + "] Test output:\nTime elapsed: " + testResult["time_elapsed"] + "\nResult:\n" + testResult["msg"]); survey.show(); } else { vscode.window.showInformationMessage('Please configure endpoint before run'); } } }); context.subscriptions.push(disposable);
disposable = vscode.commands.registerCommand('survey-helper.submit', async () => { if (vscode.workspace.getConfiguration('survey-helper').get("endpoint") !== "null") { const editor = vscode.window.activeTextEditor; if (editor) { let document = editor.document; let documentText = document.getText(); await document.save(); let response: Response = await fetch(String(vscode.workspace.getConfiguration('survey-helper').get("endpoint")) + "/submit?problem_id=" + document.fileName.replace(/^.*[\\\/]/, '').charAt(0), { method: 'GET' }); let testResult = await response.json(); let dateOb = new Date(); let hours = dateOb.getHours(); let minutes = dateOb.getMinutes(); let seconds = dateOb.getSeconds(); survey.appendLine("[" + hours + ":" + minutes + ":" + seconds + "] Submission Result:\nTime elapsed: " + testResult["time_elapsed"] + "\nResult:\n" + testResult["msg"]); survey.show(); } } else { vscode.window.showInformationMessage('Please configure endpoint before run'); } }); context.subscriptions.push(disposable);
disposable = vscode.commands.registerCommand('survey-helper.hello', () => {
vscode.window.showInformationMessage('Hello World from survey_helper in a web extension host!'); });
context.subscriptions.push(disposable);
}
export function deactivate() { if (disposables) { disposables.forEach(item => item.dispose()); } disposables = []; }
|