Skip to content

Commit 2b52321

Browse files
authored
Merge pull request #207 from ccagml/case_area
Case area
2 parents 7e5cca1 + 7cddb8e commit 2b52321

17 files changed

+311
-1907
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## version 2.19.1
2+
3+
- 区域调试参数,等到使用的时候才生成
4+
- 部分题目配置调试区后还是无法调试问题
5+
16
## version 2.18.1
27

38
- 增加周赛场次信息展示

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
// @lcpr-div-debug-arg-start
8585
// funName= alternateDigitSum
8686
// paramTypes= ["number"]
87-
// returnType= number
8887
// @lcpr-div-debug-arg-end
8988
```
9089
2. diy 参数说明
@@ -120,9 +119,6 @@
120119
- 类型说明:数组
121120
- "TreeNode"
122121
- 类型说明:树节点
123-
- returnType:函数的返回值
124-
125-
- 可填入内容同 paramTypes
126122

127123
<!-- ## TODO 看到一些可能会用得上的功能
128124

package-lock.json

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

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode-problem-rating",
33
"displayName": "LeetCode",
44
"description": "%main.description%",
5-
"version": "2.18.1",
5+
"version": "2.19.1",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",
@@ -1018,7 +1018,8 @@
10181018
"case",
10191019
"allcase",
10201020
"test",
1021-
"solution"
1021+
"solution",
1022+
"debug"
10221023
],
10231024
"scope": "application",
10241025
"items": {
@@ -1030,7 +1031,8 @@
10301031
"solution",
10311032
"description",
10321033
"case",
1033-
"allcase"
1034+
"allcase",
1035+
"debug"
10341036
],
10351037
"enumDescriptions": [
10361038
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.0%",
@@ -1039,7 +1041,8 @@
10391041
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.3%",
10401042
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.4%",
10411043
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.5%",
1042-
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.6%"
1044+
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.6%",
1045+
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.7%"
10431046
]
10441047
},
10451048
"description": "%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.description%"

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
"Show the top voted solution for the current problem.",
8080
"Show the problem description page.",
8181
"Test default case",
82-
"Test all default case"
82+
"Test all default case",
83+
"Debug case"
8384
],
8485
"main.contributes.configuration.properties.leetcode-problem-rating.enableSideMode.description": "Determine whether to group all webview pages into the second editor column when solving problems.",
8586
"main.contributes.configuration.properties.leetcode-problem-rating.nodePath.description": "The Node.js executable path. for example, C:\\Program Files\\nodejs\\node.exe",

package.nls.zh-cn.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@
7373
"main.contributes.configuration.properties.leetcode-problem-rating.enableStatusBar.description": "是否显示状态栏。",
7474
"main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.description": "自定义编辑器中的快捷方式。",
7575
"main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions": [
76-
"Submit your answer to LeetCode.",
77-
"Test your answer with customized test cases.",
78-
"Star or unstar the current problem.",
79-
"Show the top voted solution for the current problem.",
80-
"Show the problem description page.",
81-
"Test default case",
82-
"Test all default case"
76+
"提交题解",
77+
"提交测试",
78+
"题目标心",
79+
"获取题解",
80+
"显示题目简介",
81+
"提交默认测试",
82+
"提交所有测试",
83+
"本地调试"
8384
],
8485
"main.contributes.configuration.properties.leetcode-problem-rating.enableSideMode.description": "解决问题时确定是否将所有webview页面分组到第二个编辑器列中。",
8586
"main.contributes.configuration.properties.leetcode-problem-rating.nodePath.description": "Node.js 可执行文件路径。 例如:C:\\Program Files\\nodejs\\node.exe",

resources/templates/codeonly.tpl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
<%=code%>
1010
<%=comment.singleLine%> @lc code=end
1111

12-
<%=comment.singleLine%> @lcpr-div-debug-arg-start
13-
<%=comment.singleLine%> funName=
14-
<%=comment.singleLine%> paramTypes= []
15-
<%=comment.singleLine%> returnType=
16-
<%=comment.singleLine%> @lcpr-div-debug-arg-end
1712

1813
<% if(allCaseList && allCaseList.length > 0){ %>
1914
<%=comment.start%><% allCaseList.forEach(function(acase) { %>

resources/templates/detailed.tpl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
<%=code%>
2222
<%=comment.singleLine%> @lc code=end
2323

24-
<%=comment.singleLine%> @lcpr-div-debug-arg-start
25-
<%=comment.singleLine%> funName=
26-
<%=comment.singleLine%> paramTypes= []
27-
<%=comment.singleLine%> returnType=
28-
<%=comment.singleLine%> @lcpr-div-debug-arg-end
2924

3025
<% if(allCaseList && allCaseList.length > 0){ %>
3126
<%=comment.start%><% allCaseList.forEach(function(acase) { %>

src/controller/DebugController.ts

Lines changed: 184 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (c) 2023 ccagml . All rights reserved
88
*/
99

10-
import { TextDocument, window, Range } from "vscode";
10+
import { TextDocument, window, Range, Position } from "vscode";
1111
import { getTextEditorFilePathByUri } from "../utils/SystemUtils";
1212
import * as fs from "fs";
1313
import { fileMeta, ProblemMeta, supportDebugLanguages } from "../utils/problemUtils";
@@ -17,6 +17,26 @@ import { debugArgDao } from "../dao/debugArgDao";
1717

1818
import { IQuickItemEx } from "../model/Model";
1919

20+
const singleLineFlag = {
21+
bash: "#",
22+
c: "//",
23+
cpp: "//",
24+
csharp: "//",
25+
golang: "//",
26+
java: "//",
27+
javascript: "//",
28+
kotlin: "//",
29+
mysql: "--",
30+
php: "//",
31+
python: "#",
32+
python3: "#",
33+
ruby: "#",
34+
rust: "//",
35+
scala: "//",
36+
swift: "//",
37+
typescript: "//",
38+
};
39+
2040
// 做杂活
2141
class DebugContorller {
2242
constructor() {}
@@ -31,6 +51,149 @@ class DebugContorller {
3151
return true;
3252
}
3353

54+
public async check_create_debug_area(meta: ProblemMeta | null, document: TextDocument) {
55+
if (supportDebugLanguages.indexOf(meta?.lang || "") != -1) {
56+
// 分析uri代码块
57+
if (document != null) {
58+
const fileContent: string = document.getText();
59+
const debug_div_arg: RegExp = /@lcpr-div-debug-arg-start/;
60+
if (!debug_div_arg.test(fileContent.toString())) {
61+
// 弹一个生成配置区的选项
62+
await this.create_diy_debug_arg(meta, document);
63+
}
64+
}
65+
}
66+
}
67+
68+
public try_get_array_type(obj) {
69+
if (Array.isArray(obj)) {
70+
if (Array.isArray(obj[0])) {
71+
if (typeof obj[0][0] == "number") {
72+
return "number[][]";
73+
} else if (typeof obj[0][0] == "string") {
74+
return "string[][]";
75+
}
76+
} else if (typeof obj[0] == "number") {
77+
return "number[]";
78+
} else if (typeof obj[0] == "string") {
79+
return "string[]";
80+
}
81+
}
82+
return "try_arg_error";
83+
}
84+
85+
// 尝试获取diy的参数
86+
public try_get_diy_param(ts: string) {
87+
let debug_param: Array<any> = [];
88+
89+
ts = (ts || "").replace(/\r?\n/g, "\\n");
90+
ts += "\\n";
91+
92+
let case_array: Array<string> = ts.split("\\n");
93+
94+
case_array.forEach((element) => {
95+
if (element.length > 0) {
96+
try {
97+
let cur_param = JSON.parse(element);
98+
if (typeof cur_param == "number") {
99+
debug_param.push("number");
100+
} else if (Array.isArray(cur_param)) {
101+
debug_param.push(this.try_get_array_type(cur_param));
102+
} else if (typeof cur_param == "string") {
103+
debug_param.push(element.length == 1 ? "character" : "string");
104+
} else {
105+
debug_param = [];
106+
return;
107+
}
108+
} catch (error) {
109+
// 这里是字符串
110+
debug_param.push(element.length == 1 ? "character" : "string");
111+
}
112+
}
113+
});
114+
115+
// console.log("结果", debug_param);
116+
return debug_param;
117+
}
118+
119+
// 去除测试用例前的注释符号, 测试用例 可能有某些语言的注释符号, 例如 844题的#
120+
public fix_lineContent(lineContent) {
121+
let cut_pos = 0;
122+
for (let left = 0; left < lineContent.length; left++) {
123+
if (lineContent[left] == "#") {
124+
continue;
125+
}
126+
if (lineContent[left] == "/" && lineContent[left + 1] == "/") {
127+
left++;
128+
continue;
129+
}
130+
if (lineContent[left] == "-" && lineContent[left + 1] == "-") {
131+
left++;
132+
continue;
133+
}
134+
if (lineContent[left] == " ") {
135+
continue;
136+
}
137+
cut_pos = left;
138+
break;
139+
}
140+
return lineContent.substring(cut_pos);
141+
}
142+
143+
public get_one_case(document: TextDocument) {
144+
let caseFlag = false;
145+
let curCase = "";
146+
for (let i: number = 0; i < document.lineCount; i++) {
147+
const lineContent: string = document.lineAt(i).text;
148+
149+
if (caseFlag && lineContent.indexOf("@lcpr case=end") < 0) {
150+
curCase += this.fix_lineContent(lineContent).replace(/\s+/g, "");
151+
}
152+
// 收集所有用例
153+
if (lineContent.indexOf("@lcpr case=start") >= 0) {
154+
caseFlag = true;
155+
}
156+
157+
if (caseFlag && lineContent.indexOf("@lcpr case=end") >= 0) {
158+
return curCase;
159+
}
160+
}
161+
return curCase;
162+
}
163+
164+
public async create_diy_debug_arg(meta: ProblemMeta | null, document: TextDocument) {
165+
const name: string | undefined = await window.showInputBox({
166+
prompt: "输入函数名",
167+
title: "尝试生成区域调试参数",
168+
ignoreFocusOut: true,
169+
});
170+
171+
if (!(name && name.trim())) {
172+
return;
173+
}
174+
175+
let singleLine = singleLineFlag[meta?.lang || ""];
176+
let div_debug_arg: any = [
177+
`\n`,
178+
`${singleLine} @lcpr-div-debug-arg-start`,
179+
`${singleLine} funName=${name}`,
180+
`${singleLine} paramTypes= ${JSON.stringify(this.try_get_diy_param(this.get_one_case(document)))}`,
181+
`${singleLine} @lcpr-div-debug-arg-end`,
182+
`\n`,
183+
];
184+
185+
for (let i: number = 0; i < document.lineCount; i++) {
186+
const lineContent: string = document.lineAt(i).text;
187+
188+
if (lineContent.indexOf("@lc code=end") >= 0) {
189+
const editor = window.activeTextEditor;
190+
editor?.edit((edit) => {
191+
edit.insert(new Position(i + 1, i + 1), div_debug_arg.join("\n"));
192+
});
193+
}
194+
}
195+
}
196+
34197
public async startDebug(document: TextDocument, testcase?: string): Promise<void> {
35198
try {
36199
const filePath: string | undefined = await getTextEditorFilePathByUri(document.uri);
@@ -41,7 +204,9 @@ class DebugContorller {
41204
const meta: ProblemMeta | null = fileMeta(fileContent.toString());
42205

43206
if (!this.canDebug(meta, document)) {
44-
window.showErrorMessage("这题还不能debug,请尝试配置区域调试参数,麻烦提issuse");
207+
// window.showErrorMessage("这题还不能debug,请尝试配置区域调试参数,麻烦提issuse");
208+
// 判断生成测试区块
209+
await this.check_create_debug_area(meta, document);
45210
return;
46211
}
47212
let result: any;
@@ -99,8 +264,10 @@ class DebugContorller {
99264
}
100265

101266
const content: string = document.getText();
102-
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=(.*)/);
103-
if (!matchResult || !matchResult[2]) {
267+
const matchResult: RegExpMatchArray | null = content.match(
268+
/@lc app=(.*) id=(.*|\w*|\W*|[\\u4e00-\\u9fa5]*) lang=(.*)/
269+
);
270+
if (!matchResult || !matchResult[3]) {
104271
return undefined;
105272
}
106273
// 搜集所有debug
@@ -132,11 +299,12 @@ class DebugContorller {
132299

133300
edit.replace(new Range(i, equal_index + 1, i, last_index), JSON.stringify(cur_param_array));
134301
});
135-
} else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
136-
window.activeTextEditor?.edit((edit) => {
137-
edit.replace(new Range(i, equal_index + 1, i, last_index), choice.value);
138-
});
139302
}
303+
// else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
304+
// window.activeTextEditor?.edit((edit) => {
305+
// edit.replace(new Range(i, equal_index + 1, i, last_index), choice.value);
306+
// });
307+
// }
140308
}
141309

142310
// 收集所有用例
@@ -148,7 +316,9 @@ class DebugContorller {
148316
}
149317
public async resetDebugType(document: TextDocument, addType) {
150318
const content: string = document.getText();
151-
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=(.*)/);
319+
const matchResult: RegExpMatchArray | null = content.match(
320+
/@lc app=(.*) id=(.*|\w*|\W*|[\\u4e00-\\u9fa5]*) lang=(.*)/
321+
);
152322
if (!matchResult || !matchResult[2]) {
153323
return undefined;
154324
}
@@ -170,11 +340,12 @@ class DebugContorller {
170340
let cur_param_array: any = [];
171341
edit.replace(new Range(i, equal_index + 1, i, last_index), JSON.stringify(cur_param_array));
172342
});
173-
} else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
174-
window.activeTextEditor?.edit((edit) => {
175-
edit.replace(new Range(i, equal_index + 1, i, last_index), "");
176-
});
177343
}
344+
// else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
345+
// window.activeTextEditor?.edit((edit) => {
346+
// edit.replace(new Range(i, equal_index + 1, i, last_index), "");
347+
// });
348+
// }
178349
}
179350

180351
// 收集所有用例

0 commit comments

Comments
 (0)