Working example
This commit is contained in:
+27
-15
@@ -1,15 +1,20 @@
|
||||
import * as vscode from 'vscode';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import testCode from './tester';
|
||||
|
||||
let resetTimer = () => {}
|
||||
|
||||
const speedCodePath = path.join(os.homedir(), ".speed-code/");
|
||||
|
||||
if (!fs.existsSync(speedCodePath)) {
|
||||
fs.mkdirSync(speedCodePath);
|
||||
}
|
||||
|
||||
export async function openAndFocusFile(filePath: string) {
|
||||
const document = await vscode.workspace.openTextDocument(filePath);
|
||||
await vscode.window.showTextDocument(document, { preview: false, preserveFocus: false });
|
||||
const document = await vscode.workspace.openTextDocument(filePath);
|
||||
await vscode.window.showTextDocument(document, { preview: false, preserveFocus: false });
|
||||
}
|
||||
|
||||
const userCode = `
|
||||
@@ -19,19 +24,6 @@ function isPalindrome(s) {
|
||||
`;
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
const [isValid, res] = testCode(userCode, [{
|
||||
input: ["racecar"],
|
||||
expected: true
|
||||
},{
|
||||
input: ["hello"],
|
||||
expected: false
|
||||
},{
|
||||
input: ["madam"],
|
||||
expected: true
|
||||
},]);
|
||||
|
||||
vscode.window.showInformationMessage('Speedrun info: ' + JSON.stringify(res));
|
||||
|
||||
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
|
||||
statusBarItem.tooltip = 'Speedrun timer';
|
||||
statusBarItem.text = "No active Speedrun";
|
||||
@@ -49,6 +41,26 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
resetTimer();
|
||||
|
||||
context.subscriptions.push(vscode.commands.registerCommand('speed-code.startSpeedRun', () => {
|
||||
const runFilePath = path.join(speedCodePath, 'isPalindrome.js');
|
||||
fs.writeFileSync(runFilePath, '');
|
||||
|
||||
openAndFocusFile(runFilePath);
|
||||
|
||||
setTimeout(() => {
|
||||
const [isValid, res] = testCode(fs.readFileSync(runFilePath).toString(), [{
|
||||
input: ["racecar"],
|
||||
expected: true
|
||||
},{
|
||||
input: ["hello"],
|
||||
expected: false
|
||||
},{
|
||||
input: ["madam"],
|
||||
expected: true
|
||||
},]);
|
||||
|
||||
vscode.window.showInformationMessage('Speedrun info: ' + JSON.stringify(res) + 'Summary: ' + isValid);
|
||||
}, 2000);
|
||||
|
||||
const startTime = Date.now();
|
||||
timer = setInterval(() => {
|
||||
const currentTime = Date.now();
|
||||
|
||||
+1
-1
@@ -38,5 +38,5 @@ export default function testCode(code: string, testCases: TestCase[]): [boolean,
|
||||
}
|
||||
});
|
||||
|
||||
return [tests.every(t => t), tests];
|
||||
return [tests.every(t => t.valid), tests];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user