Skip to content

Commit 62c1ae7

Browse files
committed
快速选择调试需要的参数
1 parent e7616c2 commit 62c1ae7

File tree

5 files changed

+191
-2
lines changed

5 files changed

+191
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## version 2.17.1
2+
3+
- 增加调试需要参数快速选择按钮
4+
15
## version 2.16.1
26

37
- 增加中英文多语言的配置

package.json

Lines changed: 1 addition & 1 deletion
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.16.2",
5+
"version": "2.17.1",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",

src/controller/DebugController.ts

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
* Copyright (c) 2023 ccagml . All rights reserved
88
*/
99

10-
import { TextDocument, window } from "vscode";
10+
import { TextDocument, window, Range } from "vscode";
1111
import { getTextEditorFilePathByUri } from "../utils/SystemUtils";
1212
import * as fs from "fs";
1313
import { fileMeta, ProblemMeta, supportDebugLanguages } from "../utils/problemUtils";
1414

1515
import { debugService } from "../service/DebugService";
1616
import { debugArgDao } from "../dao/debugArgDao";
1717

18+
import { IQuickItemEx } from "../model/Model";
19+
1820
// 做杂活
1921
class DebugContorller {
2022
constructor() {}
@@ -66,6 +68,122 @@ class DebugContorller {
6668
//
6769
}
6870
}
71+
72+
public async addDebugType(document: TextDocument, addType) {
73+
const picks: Array<IQuickItemEx<string>> = [
74+
{ label: "number", detail: "类型说明:数字", value: "number" },
75+
{ label: "number[]", detail: "类型说明:数字数组", value: "number[]" },
76+
{ label: "number[][]", detail: "类型说明:数字二维数组", value: "number[][]" },
77+
{ label: "string", detail: "类型说明:字符串", value: "string" },
78+
{ label: "string[]", detail: "类型说明:字符串数组", value: "string[]" },
79+
{ label: "string[][]", detail: "类型说明:字符串二维数组", value: "string[][]" },
80+
{ label: "ListNode", detail: "类型说明:链表", value: "ListNode" },
81+
{ label: "ListNode[]", detail: "类型说明:链表数组", value: "ListNode[]" },
82+
{ label: "character", detail: "类型说明:字节", value: "character" },
83+
{ label: "character[]", detail: "类型说明:字节数组", value: "character[]" },
84+
{ label: "character[][]", detail: "类型说明:字节二维数组", value: "character[][]" },
85+
{ label: "NestedInteger[]", detail: "类型说明:数组", value: "NestedInteger[]" },
86+
{ label: "MountainArray", detail: "类型说明:数组", value: "MountainArray" },
87+
{ label: "TreeNode", detail: "类型说明:树节点", value: "TreeNode" },
88+
];
89+
90+
const choice: IQuickItemEx<string> | undefined = await window.showQuickPick(picks, {
91+
title: "添加调试需要的参数",
92+
matchOnDescription: false,
93+
matchOnDetail: false,
94+
placeHolder: "选择要添加的分类",
95+
canPickMany: false,
96+
});
97+
if (!choice) {
98+
return;
99+
}
100+
101+
const content: string = document.getText();
102+
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=(.*)/);
103+
if (!matchResult || !matchResult[2]) {
104+
return undefined;
105+
}
106+
// 搜集所有debug
107+
let debugFlag: boolean = false;
108+
for (let i: number = 0; i < document.lineCount; i++) {
109+
const lineContent: string = document.lineAt(i).text;
110+
111+
// 收集所有用例
112+
if (lineContent.indexOf("@lcpr-div-debug-arg-end") >= 0) {
113+
debugFlag = false;
114+
}
115+
116+
if (debugFlag) {
117+
let equal_index = lineContent.indexOf("=");
118+
const last_index = document.lineAt(i).range.end.character;
119+
if (addType == "paramTypes" && lineContent.indexOf("paramTypes=") >= 0) {
120+
window.activeTextEditor?.edit((edit) => {
121+
// 参数是个数组;
122+
// edit.replace(new Position(i, equal_index + 1), choice.value);
123+
let cur_param_str = lineContent.substring(equal_index + 1);
124+
let cur_param_array: any = [];
125+
try {
126+
cur_param_array = JSON.parse(cur_param_str);
127+
} catch (error) {
128+
cur_param_array = [];
129+
}
130+
131+
cur_param_array.push(choice.value);
132+
133+
edit.replace(new Range(i, equal_index + 1, i, last_index), JSON.stringify(cur_param_array));
134+
});
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+
});
139+
}
140+
}
141+
142+
// 收集所有用例
143+
if (lineContent.indexOf("@lcpr-div-debug-arg-start") >= 0) {
144+
debugFlag = true;
145+
}
146+
}
147+
return;
148+
}
149+
public async resetDebugType(document: TextDocument, addType) {
150+
const content: string = document.getText();
151+
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=(.*)/);
152+
if (!matchResult || !matchResult[2]) {
153+
return undefined;
154+
}
155+
// 搜集所有debug
156+
let debugFlag: boolean = false;
157+
for (let i: number = 0; i < document.lineCount; i++) {
158+
const lineContent: string = document.lineAt(i).text;
159+
160+
// 收集所有用例
161+
if (lineContent.indexOf("@lcpr-div-debug-arg-end") >= 0) {
162+
debugFlag = false;
163+
}
164+
165+
if (debugFlag) {
166+
let equal_index = lineContent.indexOf("=");
167+
const last_index = document.lineAt(i).range.end.character;
168+
if (addType == "paramTypes" && lineContent.indexOf("paramTypes=") >= 0) {
169+
window.activeTextEditor?.edit((edit) => {
170+
let cur_param_array: any = [];
171+
edit.replace(new Range(i, equal_index + 1, i, last_index), JSON.stringify(cur_param_array));
172+
});
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+
});
177+
}
178+
}
179+
180+
// 收集所有用例
181+
if (lineContent.indexOf("@lcpr-div-debug-arg-start") >= 0) {
182+
debugFlag = true;
183+
}
184+
}
185+
return;
186+
}
69187
}
70188

71189
export const debugContorller: DebugContorller = new DebugContorller();

src/extension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ export async function activate(context: ExtensionContext): Promise<void> {
141141
}),
142142
commands.registerCommand("lcpr.simpleDebug", (document: TextDocument, testCase?) =>
143143
debugContorller.startDebug(document, testCase)
144+
),
145+
commands.registerCommand("lcpr.addDebugType", (document: TextDocument, addType) =>
146+
debugContorller.addDebugType(document, addType)
147+
),
148+
commands.registerCommand("lcpr.resetDebugType", (document: TextDocument, addType) =>
149+
debugContorller.resetDebugType(document, addType)
144150
)
145151
);
146152

src/service/FileButtonService.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,51 @@ export class FileButtonService implements vscode.CodeLensProvider {
209209
return lineContent.substring(cut_pos);
210210
}
211211

212+
public createDebugButton(codeLensLine, document, lineContent): vscode.CodeLens[] {
213+
// const last_index = document.lineAt(codeLensLine).range.end.character;
214+
215+
const range: vscode.Range = new vscode.Range(codeLensLine + 1, 0, codeLensLine + 1, 0);
216+
const temp_result: vscode.CodeLens[] = [];
217+
218+
// paramTypes= []
219+
// returnType=
220+
221+
if (lineContent.indexOf("paramTypes=") >= 0) {
222+
temp_result.push(
223+
new vscode.CodeLens(range, {
224+
title: "addParam",
225+
command: "lcpr.addDebugType",
226+
arguments: [document, "paramTypes"],
227+
})
228+
);
229+
temp_result.push(
230+
new vscode.CodeLens(range, {
231+
title: "resetParam",
232+
command: "lcpr.resetDebugType",
233+
arguments: [document, "paramTypes"],
234+
})
235+
);
236+
}
237+
238+
if (lineContent.indexOf("returnType=") >= 0) {
239+
temp_result.push(
240+
new vscode.CodeLens(range, {
241+
title: "addReturn",
242+
command: "lcpr.addDebugType",
243+
arguments: [document, "returnType"],
244+
})
245+
);
246+
temp_result.push(
247+
new vscode.CodeLens(range, {
248+
title: "resetReturn",
249+
command: "lcpr.resetDebugType",
250+
arguments: [document, "returnType"],
251+
})
252+
);
253+
}
254+
return temp_result;
255+
}
256+
212257
public provideCodeLenses(document: vscode.TextDocument): vscode.ProviderResult<vscode.CodeLens[]> {
213258
const content: string = document.getText();
214259
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=(.*)/);
@@ -225,6 +270,8 @@ export class FileButtonService implements vscode.CodeLensProvider {
225270
const codeLens: vscode.CodeLens[] = [];
226271
let caseFlag: boolean = false;
227272
let curCase = "";
273+
// 搜集所有debug
274+
let debugFlag: boolean = false;
228275
for (let i: number = 0; i < document.lineCount; i++) {
229276
const lineContent: string = document.lineAt(i).text;
230277
if (lineContent.indexOf("@lc code=end") >= 0) {
@@ -248,6 +295,20 @@ export class FileButtonService implements vscode.CodeLensProvider {
248295
curCase = "";
249296
caseFlag = false;
250297
}
298+
299+
// 收集所有用例
300+
if (lineContent.indexOf("@lcpr-div-debug-arg-end") >= 0) {
301+
debugFlag = false;
302+
}
303+
304+
if (debugFlag) {
305+
this.createDebugButton(i, document, lineContent).forEach((x) => codeLens.push(x));
306+
}
307+
308+
// 收集所有用例
309+
if (lineContent.indexOf("@lcpr-div-debug-arg-start") >= 0) {
310+
debugFlag = true;
311+
}
251312
}
252313

253314
return codeLens;

0 commit comments

Comments
 (0)