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,149 @@ 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 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
+
34
197
public async startDebug ( document : TextDocument , testcase ?: string ) : Promise < void > {
35
198
try {
36
199
const filePath : string | undefined = await getTextEditorFilePathByUri ( document . uri ) ;
@@ -41,7 +204,9 @@ class DebugContorller {
41
204
const meta : ProblemMeta | null = fileMeta ( fileContent . toString ( ) ) ;
42
205
43
206
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 ) ;
45
210
return ;
46
211
}
47
212
let result : any ;
@@ -99,8 +264,10 @@ class DebugContorller {
99
264
}
100
265
101
266
const content : string = document . getText ( ) ;
102
- const matchResult : RegExpMatchArray | null = content . match ( / @ l c a p p = .* i d = ( .* ) l a n g = ( .* ) / ) ;
103
- if ( ! matchResult || ! matchResult [ 2 ] ) {
267
+ const matchResult : RegExpMatchArray | null = content . match (
268
+ / @ l c a p p = ( .* ) i d = ( .* | \w * | \W * | [ \\ u 4 e 0 0 -\\ u 9 f a 5 ] * ) l a n g = ( .* ) /
269
+ ) ;
270
+ if ( ! matchResult || ! matchResult [ 3 ] ) {
104
271
return undefined ;
105
272
}
106
273
// 搜集所有debug
@@ -132,11 +299,12 @@ class DebugContorller {
132
299
133
300
edit . replace ( new Range ( i , equal_index + 1 , i , last_index ) , JSON . stringify ( cur_param_array ) ) ;
134
301
} ) ;
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
302
}
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
+ // }
140
308
}
141
309
142
310
// 收集所有用例
@@ -148,7 +316,9 @@ class DebugContorller {
148
316
}
149
317
public async resetDebugType ( document : TextDocument , addType ) {
150
318
const content : string = document . getText ( ) ;
151
- const matchResult : RegExpMatchArray | null = content . match ( / @ l c a p p = .* i d = ( .* ) l a n g = ( .* ) / ) ;
319
+ const matchResult : RegExpMatchArray | null = content . match (
320
+ / @ l c a p p = ( .* ) i d = ( .* | \w * | \W * | [ \\ u 4 e 0 0 -\\ u 9 f a 5 ] * ) l a n g = ( .* ) /
321
+ ) ;
152
322
if ( ! matchResult || ! matchResult [ 2 ] ) {
153
323
return undefined ;
154
324
}
@@ -170,11 +340,12 @@ class DebugContorller {
170
340
let cur_param_array : any = [ ] ;
171
341
edit . replace ( new Range ( i , equal_index + 1 , i , last_index ) , JSON . stringify ( cur_param_array ) ) ;
172
342
} ) ;
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
343
}
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
+ // }
178
349
}
179
350
180
351
// 收集所有用例
0 commit comments