vite-uni-dev-tool 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/README.md +8 -0
  2. package/dist/const.cjs +1 -1
  3. package/dist/const.d.ts +1 -0
  4. package/dist/const.d.ts.map +1 -1
  5. package/dist/const.js +1 -1
  6. package/dist/core.d.ts.map +1 -1
  7. package/dist/core.js +2 -2
  8. package/dist/i18n/locales/en.cjs +1 -1
  9. package/dist/i18n/locales/en.d.ts +5 -0
  10. package/dist/i18n/locales/en.d.ts.map +1 -1
  11. package/dist/i18n/locales/en.js +1 -1
  12. package/dist/i18n/locales/zh-Hans.cjs +1 -1
  13. package/dist/i18n/locales/zh-Hans.d.ts +5 -0
  14. package/dist/i18n/locales/zh-Hans.d.ts.map +1 -1
  15. package/dist/i18n/locales/zh-Hans.js +1 -1
  16. package/dist/modules/devIntercept/index.cjs +9 -9
  17. package/dist/modules/devIntercept/index.d.ts +1 -1
  18. package/dist/modules/devIntercept/index.d.ts.map +1 -1
  19. package/dist/modules/devIntercept/index.js +9 -9
  20. package/dist/modules/devStore/index.cjs +1 -1
  21. package/dist/modules/devStore/index.d.ts.map +1 -1
  22. package/dist/modules/devStore/index.js +1 -1
  23. package/dist/plugins/uniDevTool/transform/transformMain.cjs +3 -3
  24. package/dist/plugins/uniDevTool/transform/transformMain.d.ts +2 -1
  25. package/dist/plugins/uniDevTool/transform/transformMain.d.ts.map +1 -1
  26. package/dist/plugins/uniDevTool/transform/transformMain.js +3 -3
  27. package/dist/plugins/uniDevTool/transform/transformVue.cjs +31 -25
  28. package/dist/plugins/uniDevTool/transform/transformVue.d.ts +2 -1
  29. package/dist/plugins/uniDevTool/transform/transformVue.d.ts.map +1 -1
  30. package/dist/plugins/uniDevTool/transform/transformVue.js +30 -24
  31. package/dist/plugins/uniDevTool/uniDevTool.cjs +3 -3
  32. package/dist/plugins/uniDevTool/uniDevTool.d.ts +3 -1
  33. package/dist/plugins/uniDevTool/uniDevTool.d.ts.map +1 -1
  34. package/dist/plugins/uniDevTool/uniDevTool.js +3 -3
  35. package/dist/type.d.ts +3 -0
  36. package/dist/type.d.ts.map +1 -1
  37. package/dist/v3/DevTool/components/BluetoothList/BluetoothItem.vue +199 -199
  38. package/dist/v3/DevTool/components/BluetoothList/BluetoothTool.vue +730 -730
  39. package/dist/v3/DevTool/components/BluetoothList/index.vue +167 -167
  40. package/dist/v3/DevTool/components/CaptureScreen/index.vue +109 -109
  41. package/dist/v3/DevTool/components/ConsoleList/ConsoleItem.vue +230 -225
  42. package/dist/v3/DevTool/components/ConsoleList/RunJSInput.vue +247 -247
  43. package/dist/v3/DevTool/components/ConsoleList/index.vue +171 -171
  44. package/dist/v3/DevTool/components/ConsoleList/staticTips.ts +1145 -1145
  45. package/dist/v3/DevTool/components/DevToolTitle/index.vue +24 -24
  46. package/dist/v3/DevTool/components/DevToolWindow/DevToolOverlay.vue +197 -197
  47. package/dist/v3/DevTool/components/DevToolWindow/hooks/dataUtils.ts +48 -48
  48. package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolData.ts +387 -387
  49. package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolHandlers.ts +629 -629
  50. package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolOverlay.ts +201 -197
  51. package/dist/v3/DevTool/components/ElEvent/ElEventItem.vue +105 -105
  52. package/dist/v3/DevTool/components/ElEvent/index.vue +106 -106
  53. package/dist/v3/DevTool/components/Instance/components/InstanceTreeNode.vue +265 -265
  54. package/dist/v3/DevTool/components/Instance/flatten.ts +226 -226
  55. package/dist/v3/DevTool/components/Instance/index.vue +94 -94
  56. package/dist/v3/DevTool/components/Instance/registry.ts +49 -49
  57. package/dist/v3/DevTool/components/Instance/transformTree.ts +375 -375
  58. package/dist/v3/DevTool/components/Instance/transformTreeCtx.ts +268 -268
  59. package/dist/v3/DevTool/components/Instance/typing.d.ts +43 -43
  60. package/dist/v3/DevTool/components/InstanceDetail/index.vue +485 -485
  61. package/dist/v3/DevTool/components/JsonDetail/index.vue +70 -70
  62. package/dist/v3/DevTool/components/NFCList/NFCItem.vue +112 -112
  63. package/dist/v3/DevTool/components/NFCList/NFCTool.vue +454 -454
  64. package/dist/v3/DevTool/components/NFCList/const.ts +56 -56
  65. package/dist/v3/DevTool/components/NFCList/index.vue +94 -94
  66. package/dist/v3/DevTool/components/NetworkList/InterceptConfig.vue +624 -624
  67. package/dist/v3/DevTool/components/NetworkList/InterceptItem.vue +140 -140
  68. package/dist/v3/DevTool/components/NetworkList/NetworkDetail.vue +287 -287
  69. package/dist/v3/DevTool/components/NetworkList/NetworkIntercept.vue +88 -88
  70. package/dist/v3/DevTool/components/NetworkList/NetworkItem.vue +163 -163
  71. package/dist/v3/DevTool/components/NetworkList/NetworkSend.vue +589 -589
  72. package/dist/v3/DevTool/components/NetworkList/const.ts +4 -4
  73. package/dist/v3/DevTool/components/NetworkList/hooks/useNetworkForm.ts +86 -86
  74. package/dist/v3/DevTool/components/NetworkList/index.vue +160 -160
  75. package/dist/v3/DevTool/components/NetworkList/utils.ts +101 -101
  76. package/dist/v3/DevTool/components/Performance/index.vue +498 -498
  77. package/dist/v3/DevTool/components/Performance/modules/PerformanceMetrics.vue +153 -153
  78. package/dist/v3/DevTool/components/Performance/modules/usePerformanceChart.ts +460 -460
  79. package/dist/v3/DevTool/components/Performance/modules/usePerformanceData.ts +258 -258
  80. package/dist/v3/DevTool/components/PiniaList/index.vue +93 -93
  81. package/dist/v3/DevTool/components/RunJS/index.vue +148 -148
  82. package/dist/v3/DevTool/components/ScanCodeList/ScanCodeItem.vue +97 -97
  83. package/dist/v3/DevTool/components/ScanCodeList/index.vue +100 -100
  84. package/dist/v3/DevTool/components/SettingButton/index.vue +45 -45
  85. package/dist/v3/DevTool/components/SettingList/index.vue +218 -218
  86. package/dist/v3/DevTool/components/SettingList/modules/SettingBarrage.vue +304 -304
  87. package/dist/v3/DevTool/components/SettingList/modules/SettingDevTool.vue +212 -212
  88. package/dist/v3/DevTool/components/SettingList/modules/SettingInfo.vue +157 -157
  89. package/dist/v3/DevTool/components/SettingList/modules/SettingLanguage.vue +74 -74
  90. package/dist/v3/DevTool/components/SettingList/modules/SettingLog.vue +230 -230
  91. package/dist/v3/DevTool/components/SettingList/typing.d.ts +2 -2
  92. package/dist/v3/DevTool/components/SourceCode/Line.vue +127 -127
  93. package/dist/v3/DevTool/components/SourceCode/parseCode.ts +609 -609
  94. package/dist/v3/DevTool/components/StorageList/index.vue +174 -174
  95. package/dist/v3/DevTool/components/TransferList/TransferDetail.vue +268 -268
  96. package/dist/v3/DevTool/components/VuexList/index.vue +84 -84
  97. package/dist/v3/DevTool/index.vue +1 -0
  98. package/dist/v3/components/AppTransition/index.vue +176 -176
  99. package/dist/v3/components/AutoSizer/index.vue +192 -192
  100. package/dist/v3/components/AutoSizer/index1.vue +184 -184
  101. package/dist/v3/components/AutoSizer/utils.ts +49 -49
  102. package/dist/v3/components/Barrage/BarrageItem.vue +137 -137
  103. package/dist/v3/components/Barrage/index.vue +202 -202
  104. package/dist/v3/components/CircularButton/index.vue +84 -84
  105. package/dist/v3/components/CustomSwiper/CustomSwiperItem.vue +49 -49
  106. package/dist/v3/components/CustomSwiper/index.vue +104 -104
  107. package/dist/v3/components/DevErrorBoundary/index.vue +380 -0
  108. package/dist/v3/components/Empty/index.vue +29 -29
  109. package/dist/v3/components/FilterSelect/index.vue +179 -179
  110. package/dist/v3/components/JsonPretty/components/Brackets/index.vue +27 -27
  111. package/dist/v3/components/JsonPretty/components/Carets/index.vue +59 -59
  112. package/dist/v3/components/JsonPretty/components/CheckController/index.vue +136 -136
  113. package/dist/v3/components/JsonPretty/components/TreeNode/index.vue +387 -387
  114. package/dist/v3/components/JsonPretty/hooks/useClipboard.ts +21 -21
  115. package/dist/v3/components/JsonPretty/hooks/useError.ts +21 -21
  116. package/dist/v3/components/JsonPretty/type.ts +127 -127
  117. package/dist/v3/components/JsonPretty/utils/index.ts +169 -169
  118. package/dist/v3/components/MovableContainer/index.vue +8 -4
  119. package/dist/v3/components/Pick/index.vue +322 -322
  120. package/dist/v3/components/Tag/index.vue +113 -113
  121. package/dist/v3/components/VirtualList/AutoSize.vue +40 -40
  122. package/dist/v3/components/VirtualList/index.vue +416 -416
  123. package/dist/v3/hooks/useBluetooth/index.ts +561 -561
  124. package/dist/v3/hooks/useContainerStyle.ts +153 -153
  125. package/dist/v3/hooks/useNFC/index.ts +107 -107
  126. package/dist/v3/hooks/useNFC/typing.d.ts +396 -396
  127. package/dist/v3/hooks/useNFC/useNFCAndroid.ts +966 -966
  128. package/dist/v3/hooks/useNFC/useNFCMpWeiXin.ts +812 -812
  129. package/dist/v3/hooks/useNFC/utils.ts +754 -754
  130. package/dist/v3/hooks/useRequest/index.ts +586 -586
  131. package/dist/v3/hooks/useRequest/utils.ts +267 -267
  132. package/dist/v3/hooks/useScanCode/index.ts +206 -206
  133. package/dist/v3/hooks/useWebsocket/index.ts +253 -253
  134. package/dist/v3/styles/theme.ts +12 -12
  135. package/package.json +9 -1
@@ -1,609 +1,609 @@
1
- /** 操作符 */
2
- const operatorList = ['+', '-', '*', '/', '%', '=', '!', '&'];
3
-
4
- /** 关键字 */
5
- const keywordList = [
6
- // JavaScript 关键字
7
- 'var',
8
- 'let',
9
- 'const',
10
- 'function',
11
- 'class',
12
- 'import',
13
- 'export',
14
- 'from',
15
- 'as',
16
- 'this',
17
- 'super',
18
- 'console',
19
- 'window',
20
- 'document',
21
- 'if',
22
- 'else',
23
- 'for',
24
- 'while',
25
- 'do',
26
- 'switch',
27
- 'case',
28
- 'default',
29
- 'break',
30
- 'continue',
31
- 'return',
32
- 'try',
33
- 'catch',
34
- 'finally',
35
- 'throw',
36
- 'new',
37
- 'delete',
38
- 'typeof',
39
- 'instanceof',
40
- 'in',
41
- 'of',
42
- 'void',
43
- 'yield',
44
- 'await',
45
- 'async',
46
- 'extends',
47
- 'implements',
48
- 'interface',
49
- 'package',
50
- 'private',
51
- 'protected',
52
- 'public',
53
- 'static',
54
- 'type',
55
- 'enum',
56
- 'declare',
57
- 'readonly',
58
- 'any',
59
- 'number',
60
- 'string',
61
- 'boolean',
62
- 'null',
63
- 'undefined',
64
- 'true',
65
- 'false',
66
- 'get',
67
- 'set',
68
-
69
- // Vue 特定关键字
70
- 'ref',
71
- 'reactive',
72
- 'computed',
73
- 'watch',
74
- 'watchEffect',
75
- 'provide',
76
- 'inject',
77
- 'setup',
78
- 'onMounted',
79
- 'onUnmounted',
80
- 'onUpdated',
81
- 'onBeforeMount',
82
- 'onBeforeUpdate',
83
- 'onBeforeUnmount',
84
- 'nextTick',
85
- 'defineComponent',
86
- 'defineProps',
87
- 'defineEmits',
88
- 'defineExpose',
89
- 'withDefaults',
90
-
91
- // UniApp 特定关键字
92
- 'uni',
93
- 'getCurrentInstance',
94
- 'onLoad',
95
- 'onShow',
96
- 'onReady',
97
- 'onHide',
98
- 'onUnload',
99
- 'onPullDownRefresh',
100
- 'onReachBottom',
101
- 'onShareAppMessage',
102
- 'onShareTimeline',
103
- 'onAddToFavorites',
104
- 'onPageScroll',
105
- 'onResize',
106
- 'onTabItemTap',
107
- ];
108
-
109
- /** 内置全局对象/类型 */
110
- const builtInList = [
111
- 'Object',
112
- 'Array',
113
- 'String',
114
- 'Number',
115
- 'Boolean',
116
- 'Promise',
117
- 'Math',
118
- 'JSON',
119
- 'Date',
120
- 'RegExp',
121
- 'Set',
122
- 'Map',
123
- 'Error',
124
- 'Symbol',
125
- ];
126
-
127
- type Word = {
128
- /** 字符 */
129
- word: string;
130
- /** css 样式 */
131
- class: string;
132
- /** 分类
133
- * - space: 空格
134
- * - bracket: 括号
135
- * - quotation: 引号
136
- * - string: 字符串
137
- * - function: 函数
138
- * - attribute: 属性
139
- * - operator: 操作符
140
- * - symbol: 符号
141
- * - keyword: 关键字
142
- * - number: 数字
143
- * - builtin: 内置对象
144
- * - other: 其他
145
- */
146
- type:
147
- | 'space'
148
- | 'bracket'
149
- | 'quotation'
150
- | 'string'
151
- | 'function'
152
- | 'attribute'
153
- | 'operator'
154
- | 'symbol'
155
- | 'keyword'
156
- | 'number'
157
- | 'builtin'
158
- | 'other';
159
- };
160
-
161
- export type LineInfo = {
162
- type: 'empty' | 'tag' | 'script' | 'style' | 'note';
163
- class: string;
164
- words: Word[];
165
- };
166
-
167
- const scriptStartReg = /<script(\s[^>]*)?>/i;
168
- const scriptEndReg = /<\/script>/i;
169
-
170
- const styleStartReg = /<style(\s[^>]*)?>/i;
171
- const styleEndReg = /<\/style>/i;
172
-
173
- const preClass = 'source-code-pre';
174
-
175
- /**
176
- * 获取代码中所有 <script> 块的起止行号(不包含标签本身)
177
- * @param lines 源码按行分割后的字符串数组
178
- * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
179
- */
180
- function getScriptStartAndEndRow(lines: string[]): number[][] {
181
- // 如果没有找到任何<script>标签,则认为整段代码都是script代码
182
- let hasScriptTags = false;
183
- const groups: number[][] = [];
184
- let currentStart: number | null = null; // 记录当前 <script> 开始行号
185
-
186
- lines.forEach((line, index) => {
187
- if (scriptStartReg.test(line)) {
188
- hasScriptTags = true;
189
- // 遇到开始标签,记录下一行作为内容开始位置
190
- currentStart = index + 1;
191
- } else if (scriptEndReg.test(line) && currentStart !== null) {
192
- // 遇到结束标签且已有开始位置,则记录为一个有效块
193
- // 如果开始行大于等于结束行(即<script></script>在同一行或相邻行),则不构成有效内容区间
194
- if (currentStart <= index - 1) {
195
- groups.push([currentStart, index - 1]); // 结束行为上一行(排除 </script>)
196
- }
197
- currentStart = null; // 重置等待下一个 <script>
198
- }
199
- });
200
-
201
- // 如果完全没有<script>标签,则整段代码都算作script内容
202
- if (!hasScriptTags && lines.length > 0) {
203
- // 整个文件都是script代码
204
- groups.push([0, lines.length - 1]);
205
- }
206
-
207
- return groups;
208
- }
209
-
210
- /**
211
- * 获取代码中所有 <style> 块的起止行号(不包含标签本身)
212
- * @param lines 源码按行分割后的字符串数组
213
- * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
214
- */
215
-
216
- function getStyleStartAndEndRow(lines: string[]): number[][] {
217
- const groups: number[][] = [];
218
- let currentStart: number | null = null; // 记录当前 <style> 开始行号
219
-
220
- lines.forEach((line, index) => {
221
- if (styleStartReg.test(line)) {
222
- // 遇到开始标签,记录下一行作为内容开始位置
223
- currentStart = index + 1;
224
- } else if (styleEndReg.test(line) && currentStart !== null) {
225
- // 遇到结束标签且已有开始位置,则记录为一个有效块
226
- // 如果开始行大于等于结束行(即<style></style>在同一行或相邻行),则不构成有效内容区间
227
- if (currentStart <= index - 1) {
228
- groups.push([currentStart, index - 1]); // 结束行为上一行(排除 </style>)
229
- }
230
- currentStart = null; // 重置等待下一个 <style>
231
- }
232
- });
233
-
234
- return groups;
235
- }
236
-
237
- /**
238
- * 获取代码中所有注释块的起止行号
239
- * @param lines 源码按行分割后的字符串数组
240
- * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
241
- */
242
- function getNoteStartAndEndRow(lines: string[]): number[][] {
243
- const groups: number[][] = [];
244
- let currentStart: number | null = null; // 记录当前注释块开始行号
245
- let inMultiLineComment = false; // 是否在多行注释 /* */ 中
246
- let inHtmlComment = false; // 是否在HTML注释 <!-- --> 中
247
-
248
- lines.forEach((line, index) => {
249
- const trimLine = line.trim();
250
-
251
- // 处理 /* */ 多行注释
252
- if (!inMultiLineComment && !inHtmlComment && trimLine.startsWith('/*')) {
253
- inMultiLineComment = true;
254
- currentStart = index;
255
-
256
- // 检查是否在同一行内结束多行注释
257
- if (
258
- trimLine.includes('*/') &&
259
- trimLine.indexOf('*/') > trimLine.indexOf('/*')
260
- ) {
261
- groups.push([currentStart, index]);
262
- inMultiLineComment = false;
263
- currentStart = null;
264
- }
265
- } else if (inMultiLineComment && trimLine.includes('*/')) {
266
- // 结束多行注释
267
- groups.push([currentStart!, index]);
268
- inMultiLineComment = false;
269
- currentStart = null;
270
- }
271
- // 处理 <!-- --> HTML注释
272
- else if (
273
- !inMultiLineComment &&
274
- !inHtmlComment &&
275
- trimLine.startsWith('<!--')
276
- ) {
277
- inHtmlComment = true;
278
- currentStart = index;
279
-
280
- // 检查是否在同一行内结束HTML注释
281
- if (
282
- trimLine.includes('-->') &&
283
- trimLine.indexOf('-->') > trimLine.indexOf('<!--')
284
- ) {
285
- groups.push([currentStart, index]);
286
- inHtmlComment = false;
287
- currentStart = null;
288
- }
289
- } else if (inHtmlComment && trimLine.includes('-->')) {
290
- // 结束HTML注释
291
- groups.push([currentStart!, index]);
292
- inHtmlComment = false;
293
- currentStart = null;
294
- }
295
- // 处理单行注释 // 或 /** */
296
- else if (
297
- !inMultiLineComment &&
298
- !inHtmlComment &&
299
- trimLine.startsWith('//')
300
- ) {
301
- groups.push([index, index]);
302
- }
303
- });
304
-
305
- return groups;
306
- }
307
-
308
- /**
309
- * 获取代码中的 标签起始行,不包含script 中的内容,不包含style中的内容
310
- * @param lines 源码按行分割后的字符串数组
311
- * @returns 包含标签、脚本、样式和注释范围的对象
312
- */
313
- function getTagStartAndEndRow(lines: string[]): {
314
- tagRanges: number[][];
315
- scriptRanges: number[][];
316
- styleRanges: number[][];
317
- noteRanges: number[][];
318
- } {
319
- // 先找出 script 和 style 的内容区域
320
- const scriptRanges = getScriptStartAndEndRow(lines);
321
- const styleRanges = getStyleStartAndEndRow(lines);
322
- const noteRanges = getNoteStartAndEndRow(lines);
323
-
324
- // 合并并排序所有需要排除的区间
325
- const excludeRanges = [...scriptRanges, ...styleRanges, ...noteRanges].sort(
326
- (a, b) => a[0] - b[0],
327
- );
328
-
329
- const tagRanges: number[][] = [];
330
-
331
- if (excludeRanges.length === 0) {
332
- // 如果没有 script 或 style 块,则整个文件都是 html 区域
333
- if (lines.length > 0) {
334
- tagRanges.push([0, lines.length - 1]);
335
- }
336
- } else {
337
- let currentStart = 0;
338
-
339
- for (let i = 0; i < excludeRanges.length; i++) {
340
- const [start, end] = excludeRanges[i];
341
-
342
- // 检查当前起始点到这个排除区间的开始是否有内容
343
- if (currentStart <= start - 1) {
344
- tagRanges.push([currentStart, start - 1]);
345
- }
346
-
347
- // 更新下一个可能的起始点为排除区间之后
348
- currentStart = end + 1;
349
- }
350
-
351
- // 检查最后一个排除区间结束后是否还有剩余内容
352
- if (currentStart < lines.length) {
353
- tagRanges.push([currentStart, lines.length - 1]);
354
- }
355
- }
356
-
357
- return {
358
- tagRanges,
359
- scriptRanges,
360
- styleRanges,
361
- noteRanges,
362
- };
363
- }
364
-
365
- /**
366
- * 获取指定行号对应的代码块类型
367
- *
368
- * @param {number} index
369
- * @param {{
370
- * tagRanges: number[][];
371
- * scriptRanges: number[][];
372
- * styleRanges: number[][];
373
- * }} {
374
- * tagRanges,
375
- * scriptRanges,
376
- * styleRanges,
377
- * }
378
- * @return {*}
379
- */
380
- function getLineIndexType(
381
- index: number,
382
- {
383
- tagRanges,
384
- scriptRanges,
385
- styleRanges,
386
- noteRanges,
387
- }: {
388
- tagRanges: number[][];
389
- scriptRanges: number[][];
390
- styleRanges: number[][];
391
- noteRanges: number[][];
392
- },
393
- ) {
394
- // 检查是否在注释区间内
395
- for (const [start, end] of noteRanges) {
396
- if (index >= start && index <= end) {
397
- return 'note';
398
- }
399
- }
400
-
401
- // 检查是否在 script 区间内
402
- for (const [start, end] of scriptRanges) {
403
- if (index >= start && index <= end) {
404
- return 'script';
405
- }
406
- }
407
-
408
- // 检查是否在 style 区间内
409
- for (const [start, end] of styleRanges) {
410
- if (index >= start && index <= end) {
411
- return 'style';
412
- }
413
- }
414
-
415
- // 检查是否在 tag 区间内
416
- for (const [start, end] of tagRanges) {
417
- if (index >= start && index <= end) {
418
- return 'tag';
419
- }
420
- }
421
-
422
- return 'empty';
423
- }
424
-
425
- /**
426
- * 将整行代码字符串分词,带有更精细的词法分析,大大减少DOM元素数量
427
- */
428
- function tokenizeLine(lineStr: string): Word[] {
429
- let remain = lineStr;
430
- const words: Word[] = [];
431
- let prevWord = '';
432
-
433
- while (remain.length > 0) {
434
- let match;
435
-
436
- // Space
437
- match = remain.match(/^(\s+)/);
438
- if (match) {
439
- // Use different class for inline spaces to prevent indent lines appearing in middle of code
440
- words.push({
441
- word: match[0],
442
- type: 'space',
443
- class: `${preClass}-space-inline`,
444
- });
445
- remain = remain.slice(match[0].length);
446
- continue;
447
- }
448
-
449
- // Comment (inline)
450
- match = remain.match(/^(\/\/.*)/);
451
- if (match) {
452
- words.push({ word: match[0], type: 'symbol', class: `${preClass}-note` });
453
- remain = remain.slice(match[0].length);
454
- continue;
455
- }
456
-
457
- // HTML Comment
458
- match = remain.match(/^(<!--.*?-->|<!--.*|.*-->)/);
459
- if (match) {
460
- words.push({ word: match[0], type: 'symbol', class: `${preClass}-note` });
461
- remain = remain.slice(match[0].length);
462
- continue;
463
- }
464
-
465
- // Strings (with escapes)
466
- match = remain.match(
467
- /^("[^"\\]*(\\.[^"\\]*)*"|'[^'\\]*(\\.[^'\\]*)*'|`[^`\\]*(\\.[^`\\]*)*`)/,
468
- );
469
- if (match) {
470
- words.push({
471
- word: match[0],
472
- type: 'string',
473
- class: `${preClass}-string`,
474
- });
475
- remain = remain.slice(match[0].length);
476
- prevWord = '""';
477
- continue;
478
- }
479
-
480
- // Identifiers & Keywords
481
- match = remain.match(/^[a-zA-Z_$@][a-zA-Z0-9_$-]*/);
482
- if (match) {
483
- const word = match[0];
484
- let type: Word['type'] = 'other';
485
- let cls = `${preClass}-other`;
486
-
487
- const nextCharMatch = remain.slice(word.length).match(/^\s*(.)/);
488
- const nextChar = nextCharMatch ? nextCharMatch[1] : '';
489
-
490
- if (keywordList.includes(word)) {
491
- type = 'keyword';
492
- cls = `${preClass}-keyword`;
493
- } else if (builtInList.includes(word)) {
494
- type = 'builtin';
495
- cls = `${preClass}-builtin`;
496
- } else if (prevWord === '.') {
497
- type = 'attribute';
498
- cls = `${preClass}-attribute`;
499
- } else if (prevWord === '<' || prevWord === '</') {
500
- type = 'keyword';
501
- cls = `${preClass}-keyword`; // Tag name
502
- } else if (nextChar === '(') {
503
- type = 'function';
504
- cls = `${preClass}-function`;
505
- } else if (word.startsWith('v-') || word.startsWith('@')) {
506
- type = 'keyword';
507
- cls = `${preClass}-keyword`; // Vue directives
508
- }
509
-
510
- words.push({ word, type, class: cls });
511
- prevWord = word;
512
- remain = remain.slice(word.length);
513
- continue;
514
- }
515
-
516
- // Numbers
517
- match = remain.match(
518
- /^(0[xX][0-9a-fA-F]+|\d*\.\d+([eE][+-]?\d+)?|\d+([eE][+-]?\d+)?|\d+n)/,
519
- );
520
- if (match) {
521
- words.push({
522
- word: match[0],
523
- type: 'number',
524
- class: `${preClass}-number`,
525
- });
526
- remain = remain.slice(match[0].length);
527
- prevWord = '0';
528
- continue;
529
- }
530
-
531
- // Multi-char symbols and Operators
532
- match = remain.match(
533
- /^(===|!==|==|!=|>=|<=|=>|\+\+|--|\+=|-=|\*=|\/=|\|\||&&|\?\.|<\/?|\/>|[{}()[\].,;:?+*/%&|^!=<>~#\\@])/,
534
- );
535
- if (match) {
536
- const word = match[0];
537
- let cls = `${preClass}-symbol`;
538
- if (['<', '>', '(', ')', '{', '}', '[', ']'].includes(word[0])) {
539
- cls = `${preClass}-bracket`;
540
- } else if (operatorList.includes(word[0]) || word.length > 1) {
541
- cls = `${preClass}-operator`;
542
- }
543
-
544
- words.push({ word, type: 'symbol', class: cls });
545
- prevWord = word;
546
- remain = remain.slice(word.length);
547
- continue;
548
- }
549
-
550
- // Fallback one char
551
- const word = remain[0];
552
- words.push({ word, type: 'other', class: `${preClass}-other` });
553
- prevWord = word;
554
- remain = remain.slice(1);
555
- }
556
- return words;
557
- }
558
-
559
- export function parseCode(code: string): LineInfo[] {
560
- // 按行进行重新解析
561
- const lines = code.split('\n');
562
-
563
- const { tagRanges, scriptRanges, styleRanges, noteRanges } =
564
- getTagStartAndEndRow(lines);
565
-
566
- const parseLine = lines.map((line, index) => {
567
- // 获取行类型
568
- const type = getLineIndexType(index, {
569
- tagRanges,
570
- scriptRanges,
571
- styleRanges,
572
- noteRanges,
573
- });
574
-
575
- // 解析行之前的空格,为了保证准确的缩进格式
576
- const spaceMatch = line.match(/^(\s*)/);
577
- const spacePrefix = spaceMatch ? spaceMatch[0] : '';
578
- const lineTrim = line.slice(spacePrefix.length);
579
-
580
- const words: Word[] = [];
581
- if (spacePrefix) {
582
- // Chunk spaces by 2 to add indentation guide lines
583
- for (let i = 0; i < spacePrefix.length; i += 2) {
584
- words.push({
585
- word: spacePrefix.slice(i, i + 2),
586
- type: 'space',
587
- class: `${preClass}-space`,
588
- });
589
- }
590
- }
591
-
592
- if (type === 'note') {
593
- if (lineTrim) {
594
- words.push({
595
- word: lineTrim,
596
- type: 'symbol',
597
- class: `${preClass}-note`,
598
- });
599
- }
600
- } else {
601
- words.push(...tokenizeLine(lineTrim));
602
- }
603
-
604
- const lineInfo: LineInfo = { type, words, class: `${preClass}-${type}` };
605
- return lineInfo;
606
- });
607
-
608
- return parseLine;
609
- }
1
+ /** 操作符 */
2
+ const operatorList = ['+', '-', '*', '/', '%', '=', '!', '&'];
3
+
4
+ /** 关键字 */
5
+ const keywordList = [
6
+ // JavaScript 关键字
7
+ 'var',
8
+ 'let',
9
+ 'const',
10
+ 'function',
11
+ 'class',
12
+ 'import',
13
+ 'export',
14
+ 'from',
15
+ 'as',
16
+ 'this',
17
+ 'super',
18
+ 'console',
19
+ 'window',
20
+ 'document',
21
+ 'if',
22
+ 'else',
23
+ 'for',
24
+ 'while',
25
+ 'do',
26
+ 'switch',
27
+ 'case',
28
+ 'default',
29
+ 'break',
30
+ 'continue',
31
+ 'return',
32
+ 'try',
33
+ 'catch',
34
+ 'finally',
35
+ 'throw',
36
+ 'new',
37
+ 'delete',
38
+ 'typeof',
39
+ 'instanceof',
40
+ 'in',
41
+ 'of',
42
+ 'void',
43
+ 'yield',
44
+ 'await',
45
+ 'async',
46
+ 'extends',
47
+ 'implements',
48
+ 'interface',
49
+ 'package',
50
+ 'private',
51
+ 'protected',
52
+ 'public',
53
+ 'static',
54
+ 'type',
55
+ 'enum',
56
+ 'declare',
57
+ 'readonly',
58
+ 'any',
59
+ 'number',
60
+ 'string',
61
+ 'boolean',
62
+ 'null',
63
+ 'undefined',
64
+ 'true',
65
+ 'false',
66
+ 'get',
67
+ 'set',
68
+
69
+ // Vue 特定关键字
70
+ 'ref',
71
+ 'reactive',
72
+ 'computed',
73
+ 'watch',
74
+ 'watchEffect',
75
+ 'provide',
76
+ 'inject',
77
+ 'setup',
78
+ 'onMounted',
79
+ 'onUnmounted',
80
+ 'onUpdated',
81
+ 'onBeforeMount',
82
+ 'onBeforeUpdate',
83
+ 'onBeforeUnmount',
84
+ 'nextTick',
85
+ 'defineComponent',
86
+ 'defineProps',
87
+ 'defineEmits',
88
+ 'defineExpose',
89
+ 'withDefaults',
90
+
91
+ // UniApp 特定关键字
92
+ 'uni',
93
+ 'getCurrentInstance',
94
+ 'onLoad',
95
+ 'onShow',
96
+ 'onReady',
97
+ 'onHide',
98
+ 'onUnload',
99
+ 'onPullDownRefresh',
100
+ 'onReachBottom',
101
+ 'onShareAppMessage',
102
+ 'onShareTimeline',
103
+ 'onAddToFavorites',
104
+ 'onPageScroll',
105
+ 'onResize',
106
+ 'onTabItemTap',
107
+ ];
108
+
109
+ /** 内置全局对象/类型 */
110
+ const builtInList = [
111
+ 'Object',
112
+ 'Array',
113
+ 'String',
114
+ 'Number',
115
+ 'Boolean',
116
+ 'Promise',
117
+ 'Math',
118
+ 'JSON',
119
+ 'Date',
120
+ 'RegExp',
121
+ 'Set',
122
+ 'Map',
123
+ 'Error',
124
+ 'Symbol',
125
+ ];
126
+
127
+ type Word = {
128
+ /** 字符 */
129
+ word: string;
130
+ /** css 样式 */
131
+ class: string;
132
+ /** 分类
133
+ * - space: 空格
134
+ * - bracket: 括号
135
+ * - quotation: 引号
136
+ * - string: 字符串
137
+ * - function: 函数
138
+ * - attribute: 属性
139
+ * - operator: 操作符
140
+ * - symbol: 符号
141
+ * - keyword: 关键字
142
+ * - number: 数字
143
+ * - builtin: 内置对象
144
+ * - other: 其他
145
+ */
146
+ type:
147
+ | 'space'
148
+ | 'bracket'
149
+ | 'quotation'
150
+ | 'string'
151
+ | 'function'
152
+ | 'attribute'
153
+ | 'operator'
154
+ | 'symbol'
155
+ | 'keyword'
156
+ | 'number'
157
+ | 'builtin'
158
+ | 'other';
159
+ };
160
+
161
+ export type LineInfo = {
162
+ type: 'empty' | 'tag' | 'script' | 'style' | 'note';
163
+ class: string;
164
+ words: Word[];
165
+ };
166
+
167
+ const scriptStartReg = /<script(\s[^>]*)?>/i;
168
+ const scriptEndReg = /<\/script>/i;
169
+
170
+ const styleStartReg = /<style(\s[^>]*)?>/i;
171
+ const styleEndReg = /<\/style>/i;
172
+
173
+ const preClass = 'source-code-pre';
174
+
175
+ /**
176
+ * 获取代码中所有 <script> 块的起止行号(不包含标签本身)
177
+ * @param lines 源码按行分割后的字符串数组
178
+ * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
179
+ */
180
+ function getScriptStartAndEndRow(lines: string[]): number[][] {
181
+ // 如果没有找到任何<script>标签,则认为整段代码都是script代码
182
+ let hasScriptTags = false;
183
+ const groups: number[][] = [];
184
+ let currentStart: number | null = null; // 记录当前 <script> 开始行号
185
+
186
+ lines.forEach((line, index) => {
187
+ if (scriptStartReg.test(line)) {
188
+ hasScriptTags = true;
189
+ // 遇到开始标签,记录下一行作为内容开始位置
190
+ currentStart = index + 1;
191
+ } else if (scriptEndReg.test(line) && currentStart !== null) {
192
+ // 遇到结束标签且已有开始位置,则记录为一个有效块
193
+ // 如果开始行大于等于结束行(即<script></script>在同一行或相邻行),则不构成有效内容区间
194
+ if (currentStart <= index - 1) {
195
+ groups.push([currentStart, index - 1]); // 结束行为上一行(排除 </script>)
196
+ }
197
+ currentStart = null; // 重置等待下一个 <script>
198
+ }
199
+ });
200
+
201
+ // 如果完全没有<script>标签,则整段代码都算作script内容
202
+ if (!hasScriptTags && lines.length > 0) {
203
+ // 整个文件都是script代码
204
+ groups.push([0, lines.length - 1]);
205
+ }
206
+
207
+ return groups;
208
+ }
209
+
210
+ /**
211
+ * 获取代码中所有 <style> 块的起止行号(不包含标签本身)
212
+ * @param lines 源码按行分割后的字符串数组
213
+ * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
214
+ */
215
+
216
+ function getStyleStartAndEndRow(lines: string[]): number[][] {
217
+ const groups: number[][] = [];
218
+ let currentStart: number | null = null; // 记录当前 <style> 开始行号
219
+
220
+ lines.forEach((line, index) => {
221
+ if (styleStartReg.test(line)) {
222
+ // 遇到开始标签,记录下一行作为内容开始位置
223
+ currentStart = index + 1;
224
+ } else if (styleEndReg.test(line) && currentStart !== null) {
225
+ // 遇到结束标签且已有开始位置,则记录为一个有效块
226
+ // 如果开始行大于等于结束行(即<style></style>在同一行或相邻行),则不构成有效内容区间
227
+ if (currentStart <= index - 1) {
228
+ groups.push([currentStart, index - 1]); // 结束行为上一行(排除 </style>)
229
+ }
230
+ currentStart = null; // 重置等待下一个 <style>
231
+ }
232
+ });
233
+
234
+ return groups;
235
+ }
236
+
237
+ /**
238
+ * 获取代码中所有注释块的起止行号
239
+ * @param lines 源码按行分割后的字符串数组
240
+ * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
241
+ */
242
+ function getNoteStartAndEndRow(lines: string[]): number[][] {
243
+ const groups: number[][] = [];
244
+ let currentStart: number | null = null; // 记录当前注释块开始行号
245
+ let inMultiLineComment = false; // 是否在多行注释 /* */ 中
246
+ let inHtmlComment = false; // 是否在HTML注释 <!-- --> 中
247
+
248
+ lines.forEach((line, index) => {
249
+ const trimLine = line.trim();
250
+
251
+ // 处理 /* */ 多行注释
252
+ if (!inMultiLineComment && !inHtmlComment && trimLine.startsWith('/*')) {
253
+ inMultiLineComment = true;
254
+ currentStart = index;
255
+
256
+ // 检查是否在同一行内结束多行注释
257
+ if (
258
+ trimLine.includes('*/') &&
259
+ trimLine.indexOf('*/') > trimLine.indexOf('/*')
260
+ ) {
261
+ groups.push([currentStart, index]);
262
+ inMultiLineComment = false;
263
+ currentStart = null;
264
+ }
265
+ } else if (inMultiLineComment && trimLine.includes('*/')) {
266
+ // 结束多行注释
267
+ groups.push([currentStart!, index]);
268
+ inMultiLineComment = false;
269
+ currentStart = null;
270
+ }
271
+ // 处理 <!-- --> HTML注释
272
+ else if (
273
+ !inMultiLineComment &&
274
+ !inHtmlComment &&
275
+ trimLine.startsWith('<!--')
276
+ ) {
277
+ inHtmlComment = true;
278
+ currentStart = index;
279
+
280
+ // 检查是否在同一行内结束HTML注释
281
+ if (
282
+ trimLine.includes('-->') &&
283
+ trimLine.indexOf('-->') > trimLine.indexOf('<!--')
284
+ ) {
285
+ groups.push([currentStart, index]);
286
+ inHtmlComment = false;
287
+ currentStart = null;
288
+ }
289
+ } else if (inHtmlComment && trimLine.includes('-->')) {
290
+ // 结束HTML注释
291
+ groups.push([currentStart!, index]);
292
+ inHtmlComment = false;
293
+ currentStart = null;
294
+ }
295
+ // 处理单行注释 // 或 /** */
296
+ else if (
297
+ !inMultiLineComment &&
298
+ !inHtmlComment &&
299
+ trimLine.startsWith('//')
300
+ ) {
301
+ groups.push([index, index]);
302
+ }
303
+ });
304
+
305
+ return groups;
306
+ }
307
+
308
+ /**
309
+ * 获取代码中的 标签起始行,不包含script 中的内容,不包含style中的内容
310
+ * @param lines 源码按行分割后的字符串数组
311
+ * @returns 包含标签、脚本、样式和注释范围的对象
312
+ */
313
+ function getTagStartAndEndRow(lines: string[]): {
314
+ tagRanges: number[][];
315
+ scriptRanges: number[][];
316
+ styleRanges: number[][];
317
+ noteRanges: number[][];
318
+ } {
319
+ // 先找出 script 和 style 的内容区域
320
+ const scriptRanges = getScriptStartAndEndRow(lines);
321
+ const styleRanges = getStyleStartAndEndRow(lines);
322
+ const noteRanges = getNoteStartAndEndRow(lines);
323
+
324
+ // 合并并排序所有需要排除的区间
325
+ const excludeRanges = [...scriptRanges, ...styleRanges, ...noteRanges].sort(
326
+ (a, b) => a[0] - b[0],
327
+ );
328
+
329
+ const tagRanges: number[][] = [];
330
+
331
+ if (excludeRanges.length === 0) {
332
+ // 如果没有 script 或 style 块,则整个文件都是 html 区域
333
+ if (lines.length > 0) {
334
+ tagRanges.push([0, lines.length - 1]);
335
+ }
336
+ } else {
337
+ let currentStart = 0;
338
+
339
+ for (let i = 0; i < excludeRanges.length; i++) {
340
+ const [start, end] = excludeRanges[i];
341
+
342
+ // 检查当前起始点到这个排除区间的开始是否有内容
343
+ if (currentStart <= start - 1) {
344
+ tagRanges.push([currentStart, start - 1]);
345
+ }
346
+
347
+ // 更新下一个可能的起始点为排除区间之后
348
+ currentStart = end + 1;
349
+ }
350
+
351
+ // 检查最后一个排除区间结束后是否还有剩余内容
352
+ if (currentStart < lines.length) {
353
+ tagRanges.push([currentStart, lines.length - 1]);
354
+ }
355
+ }
356
+
357
+ return {
358
+ tagRanges,
359
+ scriptRanges,
360
+ styleRanges,
361
+ noteRanges,
362
+ };
363
+ }
364
+
365
+ /**
366
+ * 获取指定行号对应的代码块类型
367
+ *
368
+ * @param {number} index
369
+ * @param {{
370
+ * tagRanges: number[][];
371
+ * scriptRanges: number[][];
372
+ * styleRanges: number[][];
373
+ * }} {
374
+ * tagRanges,
375
+ * scriptRanges,
376
+ * styleRanges,
377
+ * }
378
+ * @return {*}
379
+ */
380
+ function getLineIndexType(
381
+ index: number,
382
+ {
383
+ tagRanges,
384
+ scriptRanges,
385
+ styleRanges,
386
+ noteRanges,
387
+ }: {
388
+ tagRanges: number[][];
389
+ scriptRanges: number[][];
390
+ styleRanges: number[][];
391
+ noteRanges: number[][];
392
+ },
393
+ ) {
394
+ // 检查是否在注释区间内
395
+ for (const [start, end] of noteRanges) {
396
+ if (index >= start && index <= end) {
397
+ return 'note';
398
+ }
399
+ }
400
+
401
+ // 检查是否在 script 区间内
402
+ for (const [start, end] of scriptRanges) {
403
+ if (index >= start && index <= end) {
404
+ return 'script';
405
+ }
406
+ }
407
+
408
+ // 检查是否在 style 区间内
409
+ for (const [start, end] of styleRanges) {
410
+ if (index >= start && index <= end) {
411
+ return 'style';
412
+ }
413
+ }
414
+
415
+ // 检查是否在 tag 区间内
416
+ for (const [start, end] of tagRanges) {
417
+ if (index >= start && index <= end) {
418
+ return 'tag';
419
+ }
420
+ }
421
+
422
+ return 'empty';
423
+ }
424
+
425
+ /**
426
+ * 将整行代码字符串分词,带有更精细的词法分析,大大减少DOM元素数量
427
+ */
428
+ function tokenizeLine(lineStr: string): Word[] {
429
+ let remain = lineStr;
430
+ const words: Word[] = [];
431
+ let prevWord = '';
432
+
433
+ while (remain.length > 0) {
434
+ let match;
435
+
436
+ // Space
437
+ match = remain.match(/^(\s+)/);
438
+ if (match) {
439
+ // Use different class for inline spaces to prevent indent lines appearing in middle of code
440
+ words.push({
441
+ word: match[0],
442
+ type: 'space',
443
+ class: `${preClass}-space-inline`,
444
+ });
445
+ remain = remain.slice(match[0].length);
446
+ continue;
447
+ }
448
+
449
+ // Comment (inline)
450
+ match = remain.match(/^(\/\/.*)/);
451
+ if (match) {
452
+ words.push({ word: match[0], type: 'symbol', class: `${preClass}-note` });
453
+ remain = remain.slice(match[0].length);
454
+ continue;
455
+ }
456
+
457
+ // HTML Comment
458
+ match = remain.match(/^(<!--.*?-->|<!--.*|.*-->)/);
459
+ if (match) {
460
+ words.push({ word: match[0], type: 'symbol', class: `${preClass}-note` });
461
+ remain = remain.slice(match[0].length);
462
+ continue;
463
+ }
464
+
465
+ // Strings (with escapes)
466
+ match = remain.match(
467
+ /^("[^"\\]*(\\.[^"\\]*)*"|'[^'\\]*(\\.[^'\\]*)*'|`[^`\\]*(\\.[^`\\]*)*`)/,
468
+ );
469
+ if (match) {
470
+ words.push({
471
+ word: match[0],
472
+ type: 'string',
473
+ class: `${preClass}-string`,
474
+ });
475
+ remain = remain.slice(match[0].length);
476
+ prevWord = '""';
477
+ continue;
478
+ }
479
+
480
+ // Identifiers & Keywords
481
+ match = remain.match(/^[a-zA-Z_$@][a-zA-Z0-9_$-]*/);
482
+ if (match) {
483
+ const word = match[0];
484
+ let type: Word['type'] = 'other';
485
+ let cls = `${preClass}-other`;
486
+
487
+ const nextCharMatch = remain.slice(word.length).match(/^\s*(.)/);
488
+ const nextChar = nextCharMatch ? nextCharMatch[1] : '';
489
+
490
+ if (keywordList.includes(word)) {
491
+ type = 'keyword';
492
+ cls = `${preClass}-keyword`;
493
+ } else if (builtInList.includes(word)) {
494
+ type = 'builtin';
495
+ cls = `${preClass}-builtin`;
496
+ } else if (prevWord === '.') {
497
+ type = 'attribute';
498
+ cls = `${preClass}-attribute`;
499
+ } else if (prevWord === '<' || prevWord === '</') {
500
+ type = 'keyword';
501
+ cls = `${preClass}-keyword`; // Tag name
502
+ } else if (nextChar === '(') {
503
+ type = 'function';
504
+ cls = `${preClass}-function`;
505
+ } else if (word.startsWith('v-') || word.startsWith('@')) {
506
+ type = 'keyword';
507
+ cls = `${preClass}-keyword`; // Vue directives
508
+ }
509
+
510
+ words.push({ word, type, class: cls });
511
+ prevWord = word;
512
+ remain = remain.slice(word.length);
513
+ continue;
514
+ }
515
+
516
+ // Numbers
517
+ match = remain.match(
518
+ /^(0[xX][0-9a-fA-F]+|\d*\.\d+([eE][+-]?\d+)?|\d+([eE][+-]?\d+)?|\d+n)/,
519
+ );
520
+ if (match) {
521
+ words.push({
522
+ word: match[0],
523
+ type: 'number',
524
+ class: `${preClass}-number`,
525
+ });
526
+ remain = remain.slice(match[0].length);
527
+ prevWord = '0';
528
+ continue;
529
+ }
530
+
531
+ // Multi-char symbols and Operators
532
+ match = remain.match(
533
+ /^(===|!==|==|!=|>=|<=|=>|\+\+|--|\+=|-=|\*=|\/=|\|\||&&|\?\.|<\/?|\/>|[{}()[\].,;:?+*/%&|^!=<>~#\\@])/,
534
+ );
535
+ if (match) {
536
+ const word = match[0];
537
+ let cls = `${preClass}-symbol`;
538
+ if (['<', '>', '(', ')', '{', '}', '[', ']'].includes(word[0])) {
539
+ cls = `${preClass}-bracket`;
540
+ } else if (operatorList.includes(word[0]) || word.length > 1) {
541
+ cls = `${preClass}-operator`;
542
+ }
543
+
544
+ words.push({ word, type: 'symbol', class: cls });
545
+ prevWord = word;
546
+ remain = remain.slice(word.length);
547
+ continue;
548
+ }
549
+
550
+ // Fallback one char
551
+ const word = remain[0];
552
+ words.push({ word, type: 'other', class: `${preClass}-other` });
553
+ prevWord = word;
554
+ remain = remain.slice(1);
555
+ }
556
+ return words;
557
+ }
558
+
559
+ export function parseCode(code: string): LineInfo[] {
560
+ // 按行进行重新解析
561
+ const lines = code.split('\n');
562
+
563
+ const { tagRanges, scriptRanges, styleRanges, noteRanges } =
564
+ getTagStartAndEndRow(lines);
565
+
566
+ const parseLine = lines.map((line, index) => {
567
+ // 获取行类型
568
+ const type = getLineIndexType(index, {
569
+ tagRanges,
570
+ scriptRanges,
571
+ styleRanges,
572
+ noteRanges,
573
+ });
574
+
575
+ // 解析行之前的空格,为了保证准确的缩进格式
576
+ const spaceMatch = line.match(/^(\s*)/);
577
+ const spacePrefix = spaceMatch ? spaceMatch[0] : '';
578
+ const lineTrim = line.slice(spacePrefix.length);
579
+
580
+ const words: Word[] = [];
581
+ if (spacePrefix) {
582
+ // Chunk spaces by 2 to add indentation guide lines
583
+ for (let i = 0; i < spacePrefix.length; i += 2) {
584
+ words.push({
585
+ word: spacePrefix.slice(i, i + 2),
586
+ type: 'space',
587
+ class: `${preClass}-space`,
588
+ });
589
+ }
590
+ }
591
+
592
+ if (type === 'note') {
593
+ if (lineTrim) {
594
+ words.push({
595
+ word: lineTrim,
596
+ type: 'symbol',
597
+ class: `${preClass}-note`,
598
+ });
599
+ }
600
+ } else {
601
+ words.push(...tokenizeLine(lineTrim));
602
+ }
603
+
604
+ const lineInfo: LineInfo = { type, words, class: `${preClass}-${type}` };
605
+ return lineInfo;
606
+ });
607
+
608
+ return parseLine;
609
+ }