Skip to content

Commit 24443e3

Browse files
committed
暂代debug
1 parent a5621ff commit 24443e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+12078
-163
lines changed

package-lock.json

Lines changed: 102 additions & 100 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@
247247
"command": "lcpr.includeTemplates",
248248
"title": "插入头文件模板",
249249
"icon": "$(remove)"
250+
},
251+
{
252+
"command": "lcpr.simpleDebug",
253+
"title": "简单调试",
254+
"icon": "$(remove)"
250255
}
251256
],
252257
"viewsContainers": {

src/controller/DebugController.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Filename: /home/cc/leetcode-vscode/src/controller/DebugController.ts
3+
* Path: /home/cc/leetcode-vscode
4+
* Created Date: Wednesday, February 1st 2023, 11:11:23 am
5+
* Author: ccagml
6+
*
7+
* Copyright (c) 2023 ccagml . All rights reserved
8+
*/
9+
10+
import { Uri, window } from "vscode";
11+
import { getTextEditorFilePathByUri } from "../utils/SystemUtils";
12+
import * as fs from "fs";
13+
import { fileMeta, ProblemMeta, canDebug } from "../utils/problemUtils";
14+
import problemTypes from "../utils/problemTypes";
15+
import { debugExecutor } from "../debug/debugExecutor";
16+
17+
// 做杂活
18+
class DebugContorller {
19+
constructor() {}
20+
21+
public async debugSolution(uri: Uri, testcase?: string): Promise<void> {
22+
try {
23+
const filePath: string | undefined = await getTextEditorFilePathByUri(uri);
24+
if (!filePath) {
25+
return;
26+
}
27+
const fileContent: Buffer = fs.readFileSync(filePath);
28+
const meta: ProblemMeta | null = fileMeta(fileContent.toString());
29+
if (!canDebug(meta)) {
30+
return;
31+
}
32+
let result: any;
33+
34+
if (!testcase) {
35+
result = await debugExecutor.execute(
36+
filePath,
37+
problemTypes[meta!.id]!.testCase.replace(/"/g, '\\"'),
38+
meta!.lang
39+
);
40+
} else {
41+
const ts: string | undefined = await window.showInputBox({
42+
prompt: "Enter the test cases.",
43+
validateInput: (s: string): string | undefined =>
44+
s && s.trim() ? undefined : "Test case must not be empty.",
45+
placeHolder: "Example: [1,2,3]\\n4",
46+
ignoreFocusOut: true,
47+
});
48+
if (ts) {
49+
result = await debugExecutor.execute(filePath, ts.replace(/"/g, '\\"'), meta!.lang);
50+
}
51+
}
52+
53+
if (!result) {
54+
return;
55+
}
56+
} catch (error) {
57+
//
58+
}
59+
}
60+
}
61+
62+
export const debugContorller: DebugContorller = new DebugContorller();

src/controller/MainController.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { executeService } from "../service/ExecuteService";
1212
import { ExtensionContext } from "vscode";
1313
import { treeDataService } from "../service/TreeDataService";
1414
import { logOutput } from "../utils/OutputUtils";
15+
import { extensionState } from "../utils/problemUtils";
1516

17+
import * as fse from "fs-extra";
1618
// 做杂活
1719
class MainContorller {
1820
constructor() {}
@@ -30,6 +32,9 @@ class MainContorller {
3032
throw new Error("The environment doesn't meet requirements.");
3133
}
3234
}
35+
if (!(await fse.pathExists(extensionState.cachePath))) {
36+
await fse.mkdirs(extensionState.cachePath);
37+
}
3338
}
3439

3540
/**
@@ -64,6 +69,8 @@ class MainContorller {
6469
public initialize(context: ExtensionContext) {
6570
this.setGlobal(context);
6671
treeDataService.initialize(context);
72+
extensionState.context = context;
73+
extensionState.cachePath = context.globalStoragePath;
6774
}
6875

6976
// 删除缓存

src/controller/TreeViewController.ts

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ import { executeService } from "../service/ExecuteService";
5757
import { getNodeIdFromFile } from "../utils/SystemUtils";
5858
import { logOutput, promptForOpenOutputChannel, promptForSignIn, promptHintMessage } from "../utils/OutputUtils";
5959
import { treeDataService } from "../service/TreeDataService";
60-
import { genFileExt, genFileName, getyyyymmdd, getDayNowStr } from "../utils/SystemUtils";
60+
import {
61+
genFileExt,
62+
genFileName,
63+
getyyyymmdd,
64+
getDayNowStr,
65+
getTextEditorFilePathByUri,
66+
usingCmd,
67+
} from "../utils/SystemUtils";
6168
import { IDescriptionConfiguration, isStarShortcut } from "../utils/ConfigUtils";
6269
import * as systemUtils from "../utils/SystemUtils";
6370
import { solutionService } from "../service/SolutionService";
@@ -88,33 +95,6 @@ class TreeViewController implements Disposable {
8895
}
8996
}, this);
9097
}
91-
// 获取当前文件的路径
92-
/**
93-
* It returns the path of the currently active file, or undefined if there is no active file
94-
* @param [uri] - The file path to open.
95-
* @returns A promise that resolves to a string or undefined.
96-
*/
97-
public async getActiveFilePath(uri?: vscode.Uri): Promise<string | undefined> {
98-
let textEditor: vscode.TextEditor | undefined;
99-
if (uri) {
100-
textEditor = await vscode.window.showTextDocument(uri, {
101-
preview: false,
102-
});
103-
} else {
104-
textEditor = vscode.window.activeTextEditor;
105-
}
106-
107-
if (!textEditor) {
108-
return undefined;
109-
}
110-
if (textEditor.document.isDirty && !(await textEditor.document.save())) {
111-
vscode.window.showWarningMessage("请先保存当前文件");
112-
return undefined;
113-
}
114-
return systemUtils.useWsl()
115-
? systemUtils.toWslPath(textEditor.document.uri.fsPath)
116-
: textEditor.document.uri.fsPath;
117-
}
11898

11999
// 提交问题
120100
/**
@@ -130,7 +110,7 @@ class TreeViewController implements Disposable {
130110
return;
131111
}
132112

133-
const filePath: string | undefined = await this.getActiveFilePath(uri);
113+
const filePath: string | undefined = await getTextEditorFilePathByUri(uri);
134114
if (!filePath) {
135115
return;
136116
}
@@ -160,7 +140,7 @@ class TreeViewController implements Disposable {
160140
return;
161141
}
162142

163-
const filePath: string | undefined = await this.getActiveFilePath(uri);
143+
const filePath: string | undefined = await getTextEditorFilePathByUri(uri);
164144
if (!filePath) {
165145
return;
166146
}
@@ -277,7 +257,7 @@ class TreeViewController implements Disposable {
277257
return;
278258
}
279259

280-
const filePath: string | undefined = await this.getActiveFilePath(uri);
260+
const filePath: string | undefined = await getTextEditorFilePathByUri(uri);
281261
if (!filePath) {
282262
return;
283263
}
@@ -308,7 +288,7 @@ class TreeViewController implements Disposable {
308288
return;
309289
}
310290

311-
const filePath: string | undefined = await this.getActiveFilePath(uri);
291+
const filePath: string | undefined = await getTextEditorFilePathByUri(uri);
312292
if (!filePath) {
313293
return;
314294
}
@@ -324,26 +304,6 @@ class TreeViewController implements Disposable {
324304
}
325305
}
326306

327-
/**
328-
* "If the ComSpec environment variable is not set, or if it is set to cmd.exe, then return true."
329-
*
330-
* The ComSpec environment variable is set to the path of the command processor. On Windows, this is
331-
* usually cmd.exe. On Linux, it is usually bash
332-
* @returns A boolean value.
333-
*/
334-
public usingCmd(): boolean {
335-
const comSpec: string | undefined = process.env.ComSpec;
336-
// 'cmd.exe' is used as a fallback if process.env.ComSpec is unavailable.
337-
if (!comSpec) {
338-
return true;
339-
}
340-
341-
if (comSpec.indexOf("cmd.exe") > -1) {
342-
return true;
343-
}
344-
return false;
345-
}
346-
347307
/**
348308
* If you're on Windows, and you're using cmd.exe, then you need to escape double quotes with
349309
* backslashes. Otherwise, you don't
@@ -358,7 +318,7 @@ class TreeViewController implements Disposable {
358318
return `'${test}'`;
359319
}
360320

361-
if (this.usingCmd()) {
321+
if (usingCmd()) {
362322
// 一般需要走进这里, 除非改了 环境变量ComSpec的值
363323
if (systemUtils.useVscodeNode()) {
364324
//eslint-disable-next-line
@@ -628,7 +588,7 @@ class TreeViewController implements Disposable {
628588
}
629589
} else if (!input) {
630590
// Triggerred from command
631-
problemInput = await this.getActiveFilePath();
591+
problemInput = await getTextEditorFilePathByUri();
632592
}
633593

634594
if (!problemInput) {
@@ -682,7 +642,7 @@ class TreeViewController implements Disposable {
682642
return;
683643
}
684644
try {
685-
let problemInput = await this.getActiveFilePath();
645+
let problemInput = await getTextEditorFilePathByUri();
686646
// vscode.window.showErrorMessage(twoFactor || "输入错误");
687647
const solution: string = await executeService.getTestApi(problemInput || "");
688648
solutionService.show(solution);

0 commit comments

Comments
 (0)