From 77909c5684e7fca51504230f2cf646bb3e1ac8f4 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Wed, 6 Aug 2025 23:45:41 +0200 Subject: [PATCH] Online repo for problems --- README.md | 3 ++ src/extension.ts | 82 ++++++++++++++++++++++++++++++++---------------- src/utils.ts | 2 ++ 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 0e18b24..d401948 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # extension + The VSCode extension for SpeedCode + +All the problems and their test cases [here](https://github.com/SpeedCodeHQ/problems) diff --git a/src/extension.ts b/src/extension.ts index 0e18a5a..32017bf 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,7 +2,7 @@ import * as vscode from "vscode"; import os from "os"; import path from "path"; import fs from "fs"; -import testCode, { TestResult } from "./tester"; +import testCode, { TestCase } from "./tester"; import * as utils from "./utils"; import { MyTreeDataProvider } from "./resultViewer"; @@ -47,6 +47,8 @@ export function activate(context: vscode.ExtensionContext) { | undefined | { path: string; + name: string; + testCases: TestCase[]; elapsed: number; timer: NodeJS.Timeout; }; @@ -68,11 +70,7 @@ export function activate(context: vscode.ExtensionContext) { try { const [isValid, res] = testCode( fs.readFileSync(run.path).toString(), - [ - { input: ["racecar"], expected: true }, - { input: ["hello"], expected: false }, - { input: ["madam"], expected: true }, - ] + run.testCases ); if (isValid) { @@ -96,29 +94,59 @@ export function activate(context: vscode.ExtensionContext) { ); context.subscriptions.push( - vscode.commands.registerCommand("speed-code.startSpeedRun", () => { - treeDataProvider.testSummary = undefined; - treeDataProvider.testResults = []; - treeDataProvider.blunders = []; - updateTreeMessage(); - const runFilePath = path.join(speedCodePath, "isPalindrome.js"); - fs.writeFileSync(runFilePath, ""); - openAndFocusFile(runFilePath); + vscode.commands.registerCommand("speed-code.startSpeedRun", async () => { + const quickPick = vscode.window.createQuickPick(); + quickPick.placeholder = "Type to search"; + quickPick.matchOnDescription = true; + quickPick.items = utils.problems.map((label) => ({ label })); + quickPick.onDidChangeValue((value) => { + quickPick.items = utils.problems + .filter((prob) => prob.toLowerCase().includes(value.toLowerCase())) + .map((label) => ({ label })); + }); + quickPick.onDidAccept(async () => { + const selection = quickPick.selectedItems[0]; + if (selection) { + const name = selection.label.toLowerCase(); + treeDataProvider.testSummary = undefined; + treeDataProvider.testResults = []; + treeDataProvider.blunders = []; + updateTreeMessage(); + const runFilePath = path.join(speedCodePath, name + ".js"); + fs.writeFileSync(runFilePath, ""); + openAndFocusFile(runFilePath); - const startTime = Date.now(); - run = { - path: runFilePath, - elapsed: 0, - timer: setInterval(() => { - if (!run) return; - const currentTime = Date.now(); - run.elapsed = (currentTime - startTime) / 1000; - statusBarItem.text = utils.format(run.elapsed); - statusBarItem.color = "#fff"; - }, 10), - }; + const response = await fetch( + `https://raw.githubusercontent.com/SpeedCodeHQ/problems/refs/heads/main/${name}.json` + ); - vscode.window.showInformationMessage("Speedrun Started!"); + if (!response.ok) { + vscode.window.showErrorMessage( + `Failed to fetch test cases for ${name}: ${response.statusText}` + ); + return; + } + + const startTime = Date.now(); + run = { + path: runFilePath, + elapsed: 0, + name, + testCases: (await response.json()) as TestCase[], + timer: setInterval(() => { + if (!run) return; + const currentTime = Date.now(); + run.elapsed = (currentTime - startTime) / 1000; + statusBarItem.text = utils.format(run.elapsed); + statusBarItem.color = "#fff"; + }, 10), + }; + + vscode.window.showInformationMessage("Speedrun Started!"); + } + quickPick.hide(); + }); + quickPick.show(); }), statusBarItem ); diff --git a/src/utils.ts b/src/utils.ts index 7e23aa8..f2ce675 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +export const problems = ["Palindrome"]; + export function format(timeSeconds: number): string { if (timeSeconds > 86400) { const days = Math.floor(timeSeconds / 86400);