Shaokang's Blog

To help people verify the correctness of their code in two of my research projects, I built a VSCode extension. Here is the script for adding a simple code lens to VSCode, making it clickable for people to check the correctness of their code. The result will be put in the terminal area. Participants were also able to click a fixed button or use a command prompt to test correctness by using this extension.

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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { ExtensionContext, languages, commands, Disposable, workspace, window } from 'vscode';
import * as vscode from 'vscode';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

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; }
}

// Define what command we want to trigger when activating the CodeLens
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);
});

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
// console.log('Congratulations, your extension "survey-helper" is now active in the web extension host!');
//Create output channel
let survey = vscode.window.createOutputChannel("survey-helper");
//Write to output.
survey.appendLine("Start services");
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('survey-helper.test', async () => {
// The code you place here will be executed every time your command is executed
const editor = vscode.window.activeTextEditor;
if (editor) {
let document = editor.document;
// Get the document text
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();
// current hours
let hours = dateOb.getHours();
// current minutes
let minutes = dateOb.getMinutes();
// current seconds
let seconds = dateOb.getSeconds();
// prints date & time in YYYY-MM-DD HH:MM:SS format
survey.appendLine("[" + hours + ":" + minutes + ":" + seconds + "] Test output:\nTime elapsed: " + testResult["time_elapsed"] + "\nResult:\n" + testResult["msg"]);
survey.show();
} else {
// Display a message box to the user
vscode.window.showInformationMessage('Please configure endpoint before run');
}
}
});
context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand('survey-helper.submit', async () => {
// The code you place here will be executed every time your command is executed
if (vscode.workspace.getConfiguration('survey-helper').get("endpoint") !== "null") {
const editor = vscode.window.activeTextEditor;
if (editor) {
let document = editor.document;
// Get the document text
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();
// current hours
let hours = dateOb.getHours();
// current minutes
let minutes = dateOb.getMinutes();
// current seconds
let seconds = dateOb.getSeconds();
// prints date & time in YYYY-MM-DD HH:MM:SS format
survey.appendLine("[" + hours + ":" + minutes + ":" + seconds + "] Submission Result:\nTime elapsed: " + testResult["time_elapsed"] + "\nResult:\n" + testResult["msg"]);
survey.show();
}
} else {
// Display a message box to the user
vscode.window.showInformationMessage('Please configure endpoint before run');
}
});
context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand('survey-helper.hello', () => {
// The code you place here will be executed every time your command is executed

// Display a message box to the user

vscode.window.showInformationMessage('Hello World from survey_helper in a web extension host!');
});

context.subscriptions.push(disposable);

}

// this method is called when your extension is deactivated
export function deactivate() {
if (disposables) {
disposables.forEach(item => item.dispose());
}
disposables = [];
}

 Comments