Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit 56ff329

Browse files
Tao Wangwangtao0101
Tao Wang
authored andcommitted
add debug framework
1 parent bedd693 commit 56ff329

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

.vscodeignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
out/test/**
44
test/**
55
src/**
6+
!src/debug/entry/**
67
**/*.map
78
.gitignore
89
.travis.yml

src/commands/test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33

44
import * as fse from "fs-extra";
55
import * as vscode from "vscode";
6+
import { debugExecutor } from "../debug/debugExecutor";
67
import { leetCodeExecutor } from "../leetCodeExecutor";
78
import { leetCodeManager } from "../leetCodeManager";
89
import { IQuickItemEx, UserStatus } from "../shared";
910
import { isWindows, usingCmd } from "../utils/osUtils";
11+
import { genFileLanguage } from "../utils/problemUtils";
1012
import { DialogType, promptForOpenOutputChannel, showFileSelectDialog } from "../utils/uiUtils";
1113
import { getActiveFilePath } from "../utils/workspaceUtils";
1214
import * as wsl from "../utils/wslUtils";
1315
import { leetCodeSubmissionProvider } from "../webview/leetCodeSubmissionProvider";
1416

17+
const supportDebugLanguages: string[] = ["javascript"];
18+
1519
export async function testSolution(uri?: vscode.Uri): Promise<void> {
1620
try {
1721
if (leetCodeManager.getStatus() === UserStatus.SignedOut) {
@@ -43,6 +47,17 @@ export async function testSolution(uri?: vscode.Uri): Promise<void> {
4347
value: ":file",
4448
},
4549
);
50+
51+
const language: string = genFileLanguage(filePath);
52+
if (supportDebugLanguages.indexOf(language) !== -1) {
53+
picks.push({
54+
label: "$(pencil) Debug by writing directly...",
55+
description: "",
56+
detail: "Debug by writing test cases in input box",
57+
value: ":debug-direct",
58+
});
59+
}
60+
4661
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
4762
if (!choice) {
4863
return;
@@ -75,6 +90,17 @@ export async function testSolution(uri?: vscode.Uri): Promise<void> {
7590
}
7691
}
7792
break;
93+
case ":debug-direct":
94+
const ts: string | undefined = await vscode.window.showInputBox({
95+
prompt: "Enter the test cases.",
96+
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "Test case must not be empty.",
97+
placeHolder: "Example: [1,2,3]\\n4",
98+
ignoreFocusOut: true,
99+
});
100+
if (ts) {
101+
result = await debugExecutor.execute(filePath, parseTestString(ts), language);
102+
}
103+
break;
78104
default:
79105
break;
80106
}

src/debug/debugExecutor.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as path from "path";
2+
import * as vscode from "vscode";
3+
4+
interface IDebugConfig {
5+
type: string;
6+
program: string;
7+
}
8+
9+
const debugConfigMap: Map<string, IDebugConfig> = new Map([
10+
["javascript", {
11+
type: "node",
12+
program: "src/debug/entry/javascript.js",
13+
}],
14+
]);
15+
16+
class DebugExecutor {
17+
public async execute(filePath: string, testString: string, language: string): Promise<string> {
18+
return new Promise((_resolve: (res: string) => void, _reject: (e: Error) => void): void => {
19+
const debugConfig: undefined | IDebugConfig = debugConfigMap.get(language);
20+
if (debugConfig == null) {
21+
// tslint:disable-next-line: no-console
22+
console.error("Debug config map does not contain the support language.");
23+
throw new Error("Debug config map does not contain the support language.");
24+
}
25+
26+
const args: string[] = [filePath, testString];
27+
const extDir: string = vscode.extensions.getExtension("shengchen.vscode-leetcode")!.extensionPath;
28+
29+
vscode.debug.startDebugging(undefined, Object.assign({}, debugConfig, {
30+
request: "launch",
31+
name: "Launch Program",
32+
program: path.join(extDir, debugConfig.program),
33+
args,
34+
}));
35+
});
36+
}
37+
}
38+
39+
export const debugExecutor: DebugExecutor = new DebugExecutor();

src/debug/entry/javascript.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// tslint:disable-next-line: no-var-requires
2+
const boot = require(process.argv[2]);
3+
boot([1, 2, 3], 4);

src/utils/problemUtils.ts

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import * as _ from "lodash";
66
import * as path from "path";
77
import { IProblem, langExt } from "../shared";
88

9+
const extLang: Map<string, string> = new Map();
10+
for (const le of langExt) {
11+
extLang.set(`.${le[1]}`, le[0]);
12+
}
13+
914
export function genFileExt(language: string): string {
1015
const ext: string | undefined = langExt.get(language);
1116
if (!ext) {
@@ -14,6 +19,15 @@ export function genFileExt(language: string): string {
1419
return ext;
1520
}
1621

22+
export function genFileLanguage(filePath: string): string {
23+
const ext: string = path.parse(filePath).ext;
24+
const lang: string | undefined = extLang.get(ext);
25+
if (!lang) {
26+
throw new Error(`The language ext "${ext}" is not supported.`);
27+
}
28+
return lang;
29+
}
30+
1731
export function genFileName(node: IProblem, language: string): string {
1832
const slug: string = _.kebabCase(node.name);
1933
const ext: string = genFileExt(language);

0 commit comments

Comments
 (0)