Skip to content

Case area #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## version 2.19.1

- 区域调试参数,等到使用的时候才生成
- 部分题目配置调试区后还是无法调试问题

## version 2.18.1

- 增加周赛场次信息展示
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
// @lcpr-div-debug-arg-start
// funName= alternateDigitSum
// paramTypes= ["number"]
// returnType= number
// @lcpr-div-debug-arg-end
```
2. diy 参数说明
Expand Down Expand Up @@ -120,9 +119,6 @@
- 类型说明:数组
- "TreeNode"
- 类型说明:树节点
- returnType:函数的返回值

- 可填入内容同 paramTypes

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

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-leetcode-problem-rating",
"displayName": "LeetCode",
"description": "%main.description%",
"version": "2.18.1",
"version": "2.19.1",
"author": "ccagml",
"publisher": "ccagml",
"license": "MIT",
Expand Down Expand Up @@ -1018,7 +1018,8 @@
"case",
"allcase",
"test",
"solution"
"solution",
"debug"
],
"scope": "application",
"items": {
Expand All @@ -1030,7 +1031,8 @@
"solution",
"description",
"case",
"allcase"
"allcase",
"debug"
],
"enumDescriptions": [
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.0%",
Expand All @@ -1039,7 +1041,8 @@
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.3%",
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.4%",
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.5%",
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.6%"
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.6%",
"%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions.7%"
]
},
"description": "%main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.description%"
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"Show the top voted solution for the current problem.",
"Show the problem description page.",
"Test default case",
"Test all default case"
"Test all default case",
"Debug case"
],
"main.contributes.configuration.properties.leetcode-problem-rating.enableSideMode.description": "Determine whether to group all webview pages into the second editor column when solving problems.",
"main.contributes.configuration.properties.leetcode-problem-rating.nodePath.description": "The Node.js executable path. for example, C:\\Program Files\\nodejs\\node.exe",
Expand Down
15 changes: 8 additions & 7 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@
"main.contributes.configuration.properties.leetcode-problem-rating.enableStatusBar.description": "是否显示状态栏。",
"main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.description": "自定义编辑器中的快捷方式。",
"main.contributes.configuration.properties.leetcode-problem-rating.editor.shortcuts.items.enumDescriptions": [
"Submit your answer to LeetCode.",
"Test your answer with customized test cases.",
"Star or unstar the current problem.",
"Show the top voted solution for the current problem.",
"Show the problem description page.",
"Test default case",
"Test all default case"
"提交题解",
"提交测试",
"题目标心",
"获取题解",
"显示题目简介",
"提交默认测试",
"提交所有测试",
"本地调试"
],
"main.contributes.configuration.properties.leetcode-problem-rating.enableSideMode.description": "解决问题时确定是否将所有webview页面分组到第二个编辑器列中。",
"main.contributes.configuration.properties.leetcode-problem-rating.nodePath.description": "Node.js 可执行文件路径。 例如:C:\\Program Files\\nodejs\\node.exe",
Expand Down
5 changes: 0 additions & 5 deletions resources/templates/codeonly.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
<%=code%>
<%=comment.singleLine%> @lc code=end

<%=comment.singleLine%> @lcpr-div-debug-arg-start
<%=comment.singleLine%> funName=
<%=comment.singleLine%> paramTypes= []
<%=comment.singleLine%> returnType=
<%=comment.singleLine%> @lcpr-div-debug-arg-end

<% if(allCaseList && allCaseList.length > 0){ %>
<%=comment.start%><% allCaseList.forEach(function(acase) { %>
Expand Down
5 changes: 0 additions & 5 deletions resources/templates/detailed.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
<%=code%>
<%=comment.singleLine%> @lc code=end

<%=comment.singleLine%> @lcpr-div-debug-arg-start
<%=comment.singleLine%> funName=
<%=comment.singleLine%> paramTypes= []
<%=comment.singleLine%> returnType=
<%=comment.singleLine%> @lcpr-div-debug-arg-end

<% if(allCaseList && allCaseList.length > 0){ %>
<%=comment.start%><% allCaseList.forEach(function(acase) { %>
Expand Down
197 changes: 184 additions & 13 deletions src/controller/DebugController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Copyright (c) 2023 ccagml . All rights reserved
*/

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

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

const singleLineFlag = {
bash: "#",
c: "//",
cpp: "//",
csharp: "//",
golang: "//",
java: "//",
javascript: "//",
kotlin: "//",
mysql: "--",
php: "//",
python: "#",
python3: "#",
ruby: "#",
rust: "//",
scala: "//",
swift: "//",
typescript: "//",
};

// 做杂活
class DebugContorller {
constructor() {}
Expand All @@ -31,6 +51,149 @@ class DebugContorller {
return true;
}

public async check_create_debug_area(meta: ProblemMeta | null, document: TextDocument) {
if (supportDebugLanguages.indexOf(meta?.lang || "") != -1) {
// 分析uri代码块
if (document != null) {
const fileContent: string = document.getText();
const debug_div_arg: RegExp = /@lcpr-div-debug-arg-start/;
if (!debug_div_arg.test(fileContent.toString())) {
// 弹一个生成配置区的选项
await this.create_diy_debug_arg(meta, document);
}
}
}
}

public try_get_array_type(obj) {
if (Array.isArray(obj)) {
if (Array.isArray(obj[0])) {
if (typeof obj[0][0] == "number") {
return "number[][]";
} else if (typeof obj[0][0] == "string") {
return "string[][]";
}
} else if (typeof obj[0] == "number") {
return "number[]";
} else if (typeof obj[0] == "string") {
return "string[]";
}
}
return "try_arg_error";
}

// 尝试获取diy的参数
public try_get_diy_param(ts: string) {
let debug_param: Array<any> = [];

ts = (ts || "").replace(/\r?\n/g, "\\n");
ts += "\\n";

let case_array: Array<string> = ts.split("\\n");

case_array.forEach((element) => {
if (element.length > 0) {
try {
let cur_param = JSON.parse(element);
if (typeof cur_param == "number") {
debug_param.push("number");
} else if (Array.isArray(cur_param)) {
debug_param.push(this.try_get_array_type(cur_param));
} else if (typeof cur_param == "string") {
debug_param.push(element.length == 1 ? "character" : "string");
} else {
debug_param = [];
return;
}
} catch (error) {
// 这里是字符串
debug_param.push(element.length == 1 ? "character" : "string");
}
}
});

// console.log("结果", debug_param);
return debug_param;
}

// 去除测试用例前的注释符号, 测试用例 可能有某些语言的注释符号, 例如 844题的#
public fix_lineContent(lineContent) {
let cut_pos = 0;
for (let left = 0; left < lineContent.length; left++) {
if (lineContent[left] == "#") {
continue;
}
if (lineContent[left] == "/" && lineContent[left + 1] == "/") {
left++;
continue;
}
if (lineContent[left] == "-" && lineContent[left + 1] == "-") {
left++;
continue;
}
if (lineContent[left] == " ") {
continue;
}
cut_pos = left;
break;
}
return lineContent.substring(cut_pos);
}

public get_one_case(document: TextDocument) {
let caseFlag = false;
let curCase = "";
for (let i: number = 0; i < document.lineCount; i++) {
const lineContent: string = document.lineAt(i).text;

if (caseFlag && lineContent.indexOf("@lcpr case=end") < 0) {
curCase += this.fix_lineContent(lineContent).replace(/\s+/g, "");
}
// 收集所有用例
if (lineContent.indexOf("@lcpr case=start") >= 0) {
caseFlag = true;
}

if (caseFlag && lineContent.indexOf("@lcpr case=end") >= 0) {
return curCase;
}
}
return curCase;
}

public async create_diy_debug_arg(meta: ProblemMeta | null, document: TextDocument) {
const name: string | undefined = await window.showInputBox({
prompt: "输入函数名",
title: "尝试生成区域调试参数",
ignoreFocusOut: true,
});

if (!(name && name.trim())) {
return;
}

let singleLine = singleLineFlag[meta?.lang || ""];
let div_debug_arg: any = [
`\n`,
`${singleLine} @lcpr-div-debug-arg-start`,
`${singleLine} funName=${name}`,
`${singleLine} paramTypes= ${JSON.stringify(this.try_get_diy_param(this.get_one_case(document)))}`,
`${singleLine} @lcpr-div-debug-arg-end`,
`\n`,
];

for (let i: number = 0; i < document.lineCount; i++) {
const lineContent: string = document.lineAt(i).text;

if (lineContent.indexOf("@lc code=end") >= 0) {
const editor = window.activeTextEditor;
editor?.edit((edit) => {
edit.insert(new Position(i + 1, i + 1), div_debug_arg.join("\n"));
});
}
}
}

public async startDebug(document: TextDocument, testcase?: string): Promise<void> {
try {
const filePath: string | undefined = await getTextEditorFilePathByUri(document.uri);
Expand All @@ -41,7 +204,9 @@ class DebugContorller {
const meta: ProblemMeta | null = fileMeta(fileContent.toString());

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

const content: string = document.getText();
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=(.*)/);
if (!matchResult || !matchResult[2]) {
const matchResult: RegExpMatchArray | null = content.match(
/@lc app=(.*) id=(.*|\w*|\W*|[\\u4e00-\\u9fa5]*) lang=(.*)/
);
if (!matchResult || !matchResult[3]) {
return undefined;
}
// 搜集所有debug
Expand Down Expand Up @@ -132,11 +299,12 @@ class DebugContorller {

edit.replace(new Range(i, equal_index + 1, i, last_index), JSON.stringify(cur_param_array));
});
} else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
window.activeTextEditor?.edit((edit) => {
edit.replace(new Range(i, equal_index + 1, i, last_index), choice.value);
});
}
// else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
// window.activeTextEditor?.edit((edit) => {
// edit.replace(new Range(i, equal_index + 1, i, last_index), choice.value);
// });
// }
}

// 收集所有用例
Expand All @@ -148,7 +316,9 @@ class DebugContorller {
}
public async resetDebugType(document: TextDocument, addType) {
const content: string = document.getText();
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=(.*)/);
const matchResult: RegExpMatchArray | null = content.match(
/@lc app=(.*) id=(.*|\w*|\W*|[\\u4e00-\\u9fa5]*) lang=(.*)/
);
if (!matchResult || !matchResult[2]) {
return undefined;
}
Expand All @@ -170,11 +340,12 @@ class DebugContorller {
let cur_param_array: any = [];
edit.replace(new Range(i, equal_index + 1, i, last_index), JSON.stringify(cur_param_array));
});
} else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
window.activeTextEditor?.edit((edit) => {
edit.replace(new Range(i, equal_index + 1, i, last_index), "");
});
}
// else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
// window.activeTextEditor?.edit((edit) => {
// edit.replace(new Range(i, equal_index + 1, i, last_index), "");
// });
// }
}

// 收集所有用例
Expand Down
Loading