浏览代码

feat(task/launch): Add select testcase.

Marcelo Fornet 5 年之前
父节点
当前提交
b1650eeb11
共有 4 个文件被更改,包括 54 次插入10 次删除
  1. 3 4
      .vscode/launch.json
  2. 20 1
      package.json
  3. 0 1
      src/conn.ts
  4. 31 4
      src/extension.ts

+ 3 - 4
.vscode/launch.json

@@ -3,9 +3,8 @@
 // Hover to view descriptions of existing attributes.
 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
 {
-	"version": "0.2.0",
-    "configurations": [
-        {
+    "version": "0.2.0",
+    "configurations": [{
             "name": "Extension",
             "type": "extensionHost",
             "request": "launch",
@@ -33,4 +32,4 @@
             "preLaunchTask": "npm: watch"
         }
     ]
-}
+}

+ 20 - 1
package.json

@@ -127,6 +127,10 @@
             {
                 "command": "acmx.setChecker",
                 "title": "ACMX: Set Checker"
+            },
+            {
+                "command": "acmx.debugTestCase",
+                "title": "Select Test Case"
             }
         ],
         "keybindings": [
@@ -150,7 +154,22 @@
                 "command": "acmx.upgrade",
                 "key": "ctrl+alt+u"
             }
-        ]
+        ],
+        "menus": {
+            "explorer/context": [
+                {
+                    "command": "acmx.debugTestCase",
+                    "group": "acmx@1",
+                    "when": "resourceExtname == .in"
+                }
+            ],
+            "commandPalette": [
+                {
+                    "command": "acmx.debugTestCase",
+                    "when": "false"
+                }
+            ]
+        }
     },
     "scripts": {
         "vscode:prepublish": "npm run compile",

+ 0 - 1
src/conn.ts

@@ -65,7 +65,6 @@ const EMPTY = new SiteDescription(
  */
 export const SITES: SiteDescription[] = [
     EMPTY,
-    // CODEFORCES,
 ];
 
 export function getSite(site: string): SiteDescription {

+ 31 - 4
src/extension.ts

@@ -1,6 +1,6 @@
 'use strict';
-import { copyFileSync, existsSync, readdirSync, writeFileSync } from 'fs';
-import { extname, join } from 'path';
+import { closeSync, copyFileSync, existsSync, openSync, readdirSync, readFileSync, writeFileSync, writeSync } from 'fs';
+import { basename, dirname, extname, join } from 'path';
 import * as vscode from 'vscode';
 import { startCompetitiveCompanionService } from './companion';
 import { getSite, SITES } from './conn';
@@ -318,6 +318,31 @@ async function setChecker() {
     copyFileSync(checker_path, checker_dest);
 }
 
+async function debugTestCase(uriPath: vscode.Uri) {
+    let testCaseName = basename(uriPath.path);
+    let path = dirname(uriPath.path);
+
+    const MAX_DEPTH = 2;
+
+    for (let i = 0; i < MAX_DEPTH && !existsSync(join(path, '.vscode')); i++) {
+        path = dirname(path);
+        console.log(path);
+    }
+
+    let launchTaskPath = join(path, '.vscode', 'launch.json');
+
+    if (!existsSync(launchTaskPath)) {
+        return undefined;
+    }
+
+    let launchTaskData = readFileSync(launchTaskPath, "utf8");
+    // TODO(#41): Don't use regular expression to replace this. Use JSON parser instead.
+    let newTaskData = launchTaskData.replace(/\"stdio\"\:.+/, `"stdio": ["\${fileDirname}/testcases/${testCaseName}"],`);
+    let launchTaskFdW = openSync(launchTaskPath, 'w');
+    writeSync(launchTaskFdW, newTaskData);
+    closeSync(launchTaskFdW);
+}
+
 async function debugTest() {
     console.log("no bugs :O");
 }
@@ -338,8 +363,9 @@ export function activate(context: vscode.ExtensionContext) {
     let upgradeCommand = vscode.commands.registerCommand('acmx.upgrade', upgrade);
     let compileCommand = vscode.commands.registerCommand('acmx.compile', compile);
     let setCheckerCommand = vscode.commands.registerCommand('acmx.setChecker', setChecker);
+    let debugTestCaseCommand = vscode.commands.registerCommand('acmx.debugTestCase', debugTestCase);
 
-    let debugTestCommand = vscode.commands.registerCommand('acmx.debugTest', debugTest);
+    // let debugTestCommand = vscode.commands.registerCommand('acmx.debugTest', debugTest);
 
     context.subscriptions.push(addProblemCommand);
     context.subscriptions.push(addContestCommand);
@@ -351,8 +377,9 @@ export function activate(context: vscode.ExtensionContext) {
     context.subscriptions.push(upgradeCommand);
     context.subscriptions.push(compileCommand);
     context.subscriptions.push(setCheckerCommand);
+    context.subscriptions.push(debugTestCaseCommand);
 
-    context.subscriptions.push(debugTestCommand);
+    // context.subscriptions.push(debugTestCommand);
 }
 
 // this method is called when your extension is deactivated