7
7
* Copyright (c) 2023 ccagml . All rights reserved
8
8
*/
9
9
10
- import { TextDocument , window , Range } from "vscode" ;
10
+ import { TextDocument , window , Range , Position } from "vscode" ;
11
11
import { getTextEditorFilePathByUri } from "../utils/SystemUtils" ;
12
12
import * as fs from "fs" ;
13
13
import { fileMeta , ProblemMeta , supportDebugLanguages } from "../utils/problemUtils" ;
@@ -17,6 +17,26 @@ import { debugArgDao } from "../dao/debugArgDao";
17
17
18
18
import { IQuickItemEx } from "../model/Model" ;
19
19
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
+
20
40
// 做杂活
21
41
class DebugContorller {
22
42
constructor ( ) { }
@@ -31,6 +51,137 @@ class DebugContorller {
31
51
return true ;
32
52
}
33
53
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 = / @ l c p r - d i v - d e b u g - a r g - s t a r t / ;
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 async try_get_diy_param ( ) {
87
+ let debug_param : Array < any > = [ ] ;
88
+
89
+ let ts : string | undefined = await window . showInputBox ( {
90
+ prompt : "测试" ,
91
+ placeHolder : "Example: [1,2,3]\\n4" ,
92
+ ignoreFocusOut : true ,
93
+ } ) ;
94
+
95
+ ts = ( ts || "" ) . replace ( / \r ? \n / g, "\\n" ) ;
96
+ ts += "\\n" ;
97
+
98
+ let case_array : Array < string > = ts . split ( "\\n" ) ;
99
+
100
+ case_array . forEach ( ( element ) => {
101
+ try {
102
+ let cur_param = JSON . parse ( element ) ;
103
+ if ( typeof cur_param == "number" ) {
104
+ debug_param . push ( "number" ) ;
105
+ } else if ( Array . isArray ( cur_param ) ) {
106
+ debug_param . push ( this . try_get_array_type ( cur_param ) ) ;
107
+ } else {
108
+ debug_param = [ ] ;
109
+ return ;
110
+ }
111
+ } catch ( error ) {
112
+ // 这里是字符串
113
+ debug_param . push ( element . length == 1 ? "character" : "string" ) ;
114
+ }
115
+ } ) ;
116
+
117
+ console . log ( "结果" , debug_param ) ;
118
+
119
+ // const picks: Array<IQuickItemEx<string>> = [
120
+ // { label: "number", detail: "类型说明:数字", value: "number" },
121
+ // { label: "number[]", detail: "类型说明:数字数组", value: "number[]" },
122
+ // { label: "number[][]", detail: "类型说明:数字二维数组", value: "number[][]" },
123
+ // { label: "string", detail: "类型说明:字符串", value: "string" },
124
+ // { label: "string[]", detail: "类型说明:字符串数组", value: "string[]" },
125
+ // { label: "string[][]", detail: "类型说明:字符串二维数组", value: "string[][]" },
126
+ // { label: "character", detail: "类型说明:字节", value: "character" },
127
+ // { label: "character[]", detail: "类型说明:字节数组", value: "character[]" },
128
+ // { label: "character[][]", detail: "类型说明:字节二维数组", value: "character[][]" },
129
+ // ];
130
+
131
+ // let equal_index = lineContent.indexOf("=");
132
+ // const last_index = document.lineAt(i).range.end.character;
133
+ // if (addType == "paramTypes" && lineContent.indexOf("paramTypes=") >= 0) {
134
+ // window.activeTextEditor?.edit((edit) => {
135
+ // // 参数是个数组;
136
+ // // edit.replace(new Position(i, equal_index + 1), choice.value);
137
+ // let cur_param_str = lineContent.substring(equal_index + 1);
138
+ // let cur_param_array: any = [];
139
+ // try {
140
+ // cur_param_array = JSON.parse(cur_param_str);
141
+ // } catch (error) {
142
+ // cur_param_array = [];
143
+ // }
144
+
145
+ // cur_param_array.push(choice.value);
146
+
147
+ // edit.replace(new Range(i, equal_index + 1, i, last_index), JSON.stringify(cur_param_array));
148
+ // });
149
+ // }
150
+ }
151
+
152
+ public async create_diy_debug_arg ( meta : ProblemMeta | null , document : TextDocument ) {
153
+ const name : string | undefined = await window . showInputBox ( {
154
+ prompt : "输入函数名" ,
155
+ title : "尝试生成区域调试参数" ,
156
+ ignoreFocusOut : true ,
157
+ } ) ;
158
+
159
+ if ( ! ( name && name . trim ( ) ) ) {
160
+ return ;
161
+ }
162
+
163
+ let singleLine = singleLineFlag [ meta ?. lang || "" ] ;
164
+ let div_debug_arg : any = [
165
+ `\n` ,
166
+ `${ singleLine } @lcpr-div-debug-arg-start` ,
167
+ `${ singleLine } funName=${ name } ` ,
168
+ `${ singleLine } paramTypes= []` ,
169
+ `${ singleLine } @lcpr-div-debug-arg-end` ,
170
+ `\n` ,
171
+ ] ;
172
+
173
+ for ( let i : number = 0 ; i < document . lineCount ; i ++ ) {
174
+ const lineContent : string = document . lineAt ( i ) . text ;
175
+
176
+ if ( lineContent . indexOf ( "@lc code=end" ) >= 0 ) {
177
+ const editor = window . activeTextEditor ;
178
+ editor ?. edit ( ( edit ) => {
179
+ edit . insert ( new Position ( i + 1 , i + 1 ) , div_debug_arg . join ( "\n" ) ) ;
180
+ } ) ;
181
+ }
182
+ }
183
+ }
184
+
34
185
public async startDebug ( document : TextDocument , testcase ?: string ) : Promise < void > {
35
186
try {
36
187
const filePath : string | undefined = await getTextEditorFilePathByUri ( document . uri ) ;
@@ -41,7 +192,9 @@ class DebugContorller {
41
192
const meta : ProblemMeta | null = fileMeta ( fileContent . toString ( ) ) ;
42
193
43
194
if ( ! this . canDebug ( meta , document ) ) {
44
- window . showErrorMessage ( "这题还不能debug,请尝试配置区域调试参数,麻烦提issuse" ) ;
195
+ // window.showErrorMessage("这题还不能debug,请尝试配置区域调试参数,麻烦提issuse");
196
+ // 判断生成测试区块
197
+ await this . check_create_debug_area ( meta , document ) ;
45
198
return ;
46
199
}
47
200
let result : any ;
@@ -132,11 +285,12 @@ class DebugContorller {
132
285
133
286
edit . replace ( new Range ( i , equal_index + 1 , i , last_index ) , JSON . stringify ( cur_param_array ) ) ;
134
287
} ) ;
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
288
}
289
+ // else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
290
+ // window.activeTextEditor?.edit((edit) => {
291
+ // edit.replace(new Range(i, equal_index + 1, i, last_index), choice.value);
292
+ // });
293
+ // }
140
294
}
141
295
142
296
// 收集所有用例
@@ -170,11 +324,12 @@ class DebugContorller {
170
324
let cur_param_array : any = [ ] ;
171
325
edit . replace ( new Range ( i , equal_index + 1 , i , last_index ) , JSON . stringify ( cur_param_array ) ) ;
172
326
} ) ;
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
327
}
328
+ // else if (addType == "returnType" && lineContent.indexOf("returnType=") >= 0) {
329
+ // window.activeTextEditor?.edit((edit) => {
330
+ // edit.replace(new Range(i, equal_index + 1, i, last_index), "");
331
+ // });
332
+ // }
178
333
}
179
334
180
335
// 收集所有用例
0 commit comments