vite-uni-dev-tool 1.0.0 → 1.1.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 (168) hide show
  1. package/README.md +46 -0
  2. package/dist/const.cjs +1 -1
  3. package/dist/const.d.ts +12 -0
  4. package/dist/const.d.ts.map +1 -1
  5. package/dist/const.js +1 -1
  6. package/dist/core-shared.d.ts +1 -1
  7. package/dist/core-shared.d.ts.map +1 -1
  8. package/dist/core-shared.js +1 -1
  9. package/dist/core.d.ts +10 -3
  10. package/dist/core.d.ts.map +1 -1
  11. package/dist/core.js +2 -2
  12. package/dist/i18n/locales/en.cjs +1 -1
  13. package/dist/i18n/locales/en.d.ts +81 -0
  14. package/dist/i18n/locales/en.d.ts.map +1 -1
  15. package/dist/i18n/locales/en.js +1 -1
  16. package/dist/i18n/locales/zh-Hans.cjs +1 -1
  17. package/dist/i18n/locales/zh-Hans.d.ts +82 -1
  18. package/dist/i18n/locales/zh-Hans.d.ts.map +1 -1
  19. package/dist/i18n/locales/zh-Hans.js +1 -1
  20. package/dist/modules/devConsole/index.cjs +1 -1
  21. package/dist/modules/devConsole/index.js +3 -3
  22. package/dist/modules/devEvent/index.cjs +3 -3
  23. package/dist/modules/devEvent/index.d.ts +1 -0
  24. package/dist/modules/devEvent/index.d.ts.map +1 -1
  25. package/dist/modules/devEvent/index.js +3 -3
  26. package/dist/modules/devIntercept/index.cjs +14 -13
  27. package/dist/modules/devIntercept/index.d.ts +19 -0
  28. package/dist/modules/devIntercept/index.d.ts.map +1 -1
  29. package/dist/modules/devIntercept/index.js +14 -13
  30. package/dist/modules/devStore/index.cjs +1 -1
  31. package/dist/modules/devStore/index.d.ts +21 -0
  32. package/dist/modules/devStore/index.d.ts.map +1 -1
  33. package/dist/modules/devStore/index.js +1 -1
  34. package/dist/plugins/uniDevTool/transform/transformMain.cjs +1 -1
  35. package/dist/plugins/uniDevTool/transform/transformMain.js +1 -1
  36. package/dist/type.d.ts +47 -2
  37. package/dist/type.d.ts.map +1 -1
  38. package/dist/utils/language.cjs +1 -1
  39. package/dist/utils/language.d.ts.map +1 -1
  40. package/dist/utils/language.js +1 -1
  41. package/dist/utils/object.cjs +1 -1
  42. package/dist/utils/object.d.ts.map +1 -1
  43. package/dist/utils/object.js +1 -1
  44. package/dist/v3/DevTool/components/BluetoothList/BluetoothItem.vue +199 -0
  45. package/dist/v3/DevTool/components/BluetoothList/BluetoothTool.vue +730 -0
  46. package/dist/v3/DevTool/components/BluetoothList/index.vue +167 -0
  47. package/dist/v3/{CaptureScreen → DevTool/components/CaptureScreen}/index.vue +109 -109
  48. package/dist/v3/{ConsoleList → DevTool/components/ConsoleList}/ConsoleItem.vue +225 -224
  49. package/dist/v3/{ConsoleList → DevTool/components/ConsoleList}/RunJSInput.vue +247 -249
  50. package/dist/v3/{ConsoleList → DevTool/components/ConsoleList}/index.vue +171 -160
  51. package/dist/v3/{ConsoleList → DevTool/components/ConsoleList}/staticTips.ts +1145 -1145
  52. package/dist/v3/{DevToolButton → DevTool/components/DevToolButton}/index.vue +7 -4
  53. package/dist/v3/{DevToolTitle → DevTool/components/DevToolTitle}/index.vue +24 -24
  54. package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/DevToolOverlay.vue +197 -182
  55. package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/const.ts +28 -5
  56. package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/hooks/dataUtils.ts +48 -48
  57. package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/hooks/useDevToolData.ts +387 -338
  58. package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/hooks/useDevToolHandlers.ts +629 -549
  59. package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/hooks/useDevToolOverlay.ts +197 -184
  60. package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/index.vue +67 -16
  61. package/dist/v3/{ElEvent → DevTool/components/ElEvent}/ElEventItem.vue +105 -105
  62. package/dist/v3/{ElEvent → DevTool/components/ElEvent}/index.vue +106 -109
  63. package/dist/v3/{Instance → DevTool/components/Instance}/components/InstanceTreeNode.vue +265 -265
  64. package/dist/v3/{Instance → DevTool/components/Instance}/flatten.ts +226 -226
  65. package/dist/v3/{Instance → DevTool/components/Instance}/index.vue +94 -94
  66. package/dist/v3/{Instance → DevTool/components/Instance}/registry.ts +49 -49
  67. package/dist/v3/{Instance → DevTool/components/Instance}/transformTree.ts +375 -375
  68. package/dist/v3/{Instance → DevTool/components/Instance}/transformTreeCtx.ts +268 -268
  69. package/dist/v3/{Instance → DevTool/components/Instance}/typing.d.ts +43 -43
  70. package/dist/v3/{InstanceDetail → DevTool/components/InstanceDetail}/index.vue +485 -485
  71. package/dist/v3/{JsonDetail → DevTool/components/JsonDetail}/index.vue +70 -70
  72. package/dist/v3/{NFCList → DevTool/components/NFCList}/NFCItem.vue +112 -113
  73. package/dist/v3/{NFCList → DevTool/components/NFCList}/NFCTool.vue +454 -478
  74. package/dist/v3/{NFCList → DevTool/components/NFCList}/const.ts +56 -56
  75. package/dist/v3/{NFCList → DevTool/components/NFCList}/index.vue +94 -98
  76. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/InterceptConfig.vue +624 -608
  77. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/InterceptItem.vue +140 -140
  78. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/NetworkDetail.vue +287 -296
  79. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/NetworkIntercept.vue +88 -93
  80. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/NetworkItem.vue +163 -167
  81. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/NetworkSend.vue +589 -556
  82. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/const.ts +4 -4
  83. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/hooks/useNetworkForm.ts +86 -86
  84. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/index.vue +160 -160
  85. package/dist/v3/{NetworkList → DevTool/components/NetworkList}/utils.ts +101 -101
  86. package/dist/v3/{Performance → DevTool/components/Performance}/index.vue +498 -495
  87. package/dist/v3/{Performance → DevTool/components/Performance}/modules/PerformanceMetrics.vue +153 -153
  88. package/dist/v3/{Performance → DevTool/components/Performance}/modules/PerformanceWidget.vue +12 -9
  89. package/dist/v3/{Performance → DevTool/components/Performance}/modules/usePerformanceChart.ts +460 -460
  90. package/dist/v3/{Performance → DevTool/components/Performance}/modules/usePerformanceData.ts +258 -258
  91. package/dist/v3/{PiniaList → DevTool/components/PiniaList}/index.vue +93 -94
  92. package/dist/v3/{RouteList → DevTool/components/RouteList}/index.vue +21 -24
  93. package/dist/v3/{RunJS → DevTool/components/RunJS}/index.vue +148 -148
  94. package/dist/v3/{ScanCodeList → DevTool/components/ScanCodeList}/ScanCodeItem.vue +97 -98
  95. package/dist/v3/{ScanCodeList → DevTool/components/ScanCodeList}/index.vue +100 -104
  96. package/dist/v3/{SettingButton → DevTool/components/SettingButton}/index.vue +45 -45
  97. package/dist/v3/{SettingList → DevTool/components/SettingList}/index.vue +218 -150
  98. package/dist/v3/DevTool/components/SettingList/modules/SettingBarrage.vue +304 -0
  99. package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingDevTool.vue +212 -208
  100. package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingInfo.vue +157 -119
  101. package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingLanguage.vue +74 -74
  102. package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingLog.vue +230 -230
  103. package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingNetwork.vue +3 -3
  104. package/dist/v3/{SettingList → DevTool/components/SettingList}/modules/SettingTheme.vue +37 -7
  105. package/dist/v3/{SettingList → DevTool/components/SettingList}/typing.d.ts +2 -2
  106. package/dist/v3/{SourceCode → DevTool/components/SourceCode}/Line.vue +127 -116
  107. package/dist/v3/{SourceCode → DevTool/components/SourceCode}/index.vue +8 -8
  108. package/dist/v3/{SourceCode → DevTool/components/SourceCode}/parseCode.ts +609 -701
  109. package/dist/v3/{StorageList → DevTool/components/StorageList}/index.vue +174 -174
  110. package/dist/v3/{TransferList → DevTool/components/TransferList}/TransferDetail.vue +268 -268
  111. package/dist/v3/{TransferList → DevTool/components/TransferList}/TransferItem.vue +4 -4
  112. package/dist/v3/{TransferList → DevTool/components/TransferList}/index.vue +8 -8
  113. package/dist/v3/{UniEvent → DevTool/components/UniEvent}/UniEventItem.vue +6 -7
  114. package/dist/v3/{UniEvent → DevTool/components/UniEvent}/index.vue +6 -6
  115. package/dist/v3/{VuexList → DevTool/components/VuexList}/index.vue +84 -84
  116. package/dist/v3/{WebSocket → DevTool/components/WebSocket}/WebSocketDetail.vue +8 -8
  117. package/dist/v3/{WebSocket → DevTool/components/WebSocket}/WebSocketItem.vue +4 -4
  118. package/dist/v3/{WebSocket → DevTool/components/WebSocket}/index.vue +8 -8
  119. package/dist/v3/DevTool/index.vue +179 -5
  120. package/dist/v3/{AppTransition → components/AppTransition}/index.vue +176 -170
  121. package/dist/v3/{AutoSizer → components/AutoSizer}/index.vue +192 -192
  122. package/dist/v3/{AutoSizer → components/AutoSizer}/index1.vue +184 -184
  123. package/dist/v3/{AutoSizer → components/AutoSizer}/utils.ts +49 -49
  124. package/dist/v3/components/Barrage/BarrageItem.vue +137 -0
  125. package/dist/v3/components/Barrage/index.vue +202 -0
  126. package/dist/v3/{CircularButton → components/CircularButton}/index.vue +84 -84
  127. package/dist/v3/{CustomSwiper → components/CustomSwiper}/CustomSwiperItem.vue +49 -49
  128. package/dist/v3/{CustomSwiper → components/CustomSwiper}/index.vue +104 -104
  129. package/dist/v3/{DraggableContainer → components/DraggableContainer}/index.vue +1 -1
  130. package/dist/v3/{Empty → components/Empty}/index.vue +29 -29
  131. package/dist/v3/{FilterInput → components/FilterInput}/index.vue +1 -1
  132. package/dist/v3/{FilterSelect → components/FilterSelect}/index.vue +179 -179
  133. package/dist/v3/{JsonPretty → components/JsonPretty}/components/Brackets/index.vue +27 -27
  134. package/dist/v3/{JsonPretty → components/JsonPretty}/components/Carets/index.vue +59 -59
  135. package/dist/v3/{JsonPretty → components/JsonPretty}/components/CheckController/index.vue +136 -136
  136. package/dist/v3/{JsonPretty → components/JsonPretty}/components/TreeNode/index.vue +387 -381
  137. package/dist/v3/{JsonPretty → components/JsonPretty}/hooks/useClipboard.ts +21 -21
  138. package/dist/v3/{JsonPretty → components/JsonPretty}/hooks/useError.ts +21 -21
  139. package/dist/v3/{JsonPretty → components/JsonPretty}/index.vue +16 -13
  140. package/dist/v3/{JsonPretty → components/JsonPretty}/type.ts +127 -126
  141. package/dist/v3/{JsonPretty → components/JsonPretty}/utils/index.ts +169 -169
  142. package/dist/v3/{MovableContainer → components/MovableContainer}/index.vue +1 -1
  143. package/dist/v3/{Pick → components/Pick}/index.vue +322 -322
  144. package/dist/v3/{Tabs → components/Tabs}/index.vue +30 -4
  145. package/dist/v3/{Tag → components/Tag}/index.vue +113 -113
  146. package/dist/v3/{VirtualList → components/VirtualList}/AutoSize.vue +40 -40
  147. package/dist/v3/{VirtualList → components/VirtualList}/index.vue +416 -412
  148. package/dist/v3/hooks/useBluetooth/index.ts +561 -0
  149. package/dist/v3/hooks/useContainerStyle.ts +153 -153
  150. package/dist/v3/hooks/useNFC/index.ts +107 -107
  151. package/dist/v3/hooks/useNFC/typing.d.ts +396 -396
  152. package/dist/v3/hooks/useNFC/useNFCAndroid.ts +966 -966
  153. package/dist/v3/hooks/useNFC/useNFCMpWeiXin.ts +812 -812
  154. package/dist/v3/hooks/useNFC/utils.ts +754 -754
  155. package/dist/v3/hooks/useRequest/index.ts +586 -573
  156. package/dist/v3/hooks/useRequest/utils.ts +267 -267
  157. package/dist/v3/hooks/useScanCode/index.ts +206 -206
  158. package/dist/v3/hooks/useWebsocket/README.md +79 -0
  159. package/dist/v3/hooks/useWebsocket/index.ts +253 -0
  160. package/dist/v3/styles/theme.css +17 -10
  161. package/dist/v3/styles/theme.ts +12 -12
  162. package/package.json +59 -64
  163. package/dist/plugins/uniParseStock/index.d.ts +0 -10
  164. package/dist/plugins/uniParseStock/index.d.ts.map +0 -1
  165. /package/dist/v3/{DevToolWindow → DevTool/components/DevToolWindow}/index.css +0 -0
  166. /package/dist/v3/{SettingList → DevTool/components/SettingList}/index.css +0 -0
  167. /package/dist/v3/{Empty → components/Empty}/empty.png +0 -0
  168. /package/dist/v3/{VirtualList → components/VirtualList}/readme.md +0 -0
@@ -1,701 +1,609 @@
1
- /** 行前空格匹配 */
2
- const startSpaceReg = /^(\s*)/;
3
-
4
- /** 括号 */
5
- const bracketList = ['<', '>', '(', ')', '{', '}', '[', ']'];
6
-
7
- /** 引号 */
8
- const quotationList = ["'", '"', '‘', '’', '`'];
9
-
10
- /** 操作符 */
11
- const operatorList = ['+', '-', '*', '/', '%', '=', '!', '&'];
12
-
13
- /** 符号 */
14
- const symbolList = [
15
- ' ',
16
- '<',
17
- '>',
18
- '(',
19
- ')',
20
- '{',
21
- '}',
22
- '[',
23
- ']',
24
- ';',
25
- ':',
26
- ',',
27
- '.',
28
- '=',
29
- '?',
30
- '+',
31
- '-',
32
- '*',
33
- '/',
34
- '\\',
35
- "'",
36
- '"',
37
- '@',
38
- '#',
39
- '$',
40
- '!',
41
- '&',
42
- '|',
43
- '^',
44
- '%',
45
- ];
46
-
47
- /** 关键字 */
48
- const keywordList = [
49
- // JavaScript 关键字
50
- 'var',
51
- 'let',
52
- 'const',
53
- 'function',
54
- 'class',
55
- 'import',
56
- 'export',
57
- 'from',
58
- 'as',
59
- 'this',
60
- 'super',
61
- 'console',
62
- 'window',
63
- 'document',
64
- 'if',
65
- 'else',
66
- 'for',
67
- 'while',
68
- 'do',
69
- 'switch',
70
- 'case',
71
- 'default',
72
- 'break',
73
- 'continue',
74
- 'return',
75
- 'try',
76
- 'catch',
77
- 'finally',
78
- 'throw',
79
- 'new',
80
- 'delete',
81
- 'typeof',
82
- 'instanceof',
83
- 'in',
84
- 'of',
85
- 'void',
86
- 'yield',
87
- 'await',
88
- 'async',
89
- 'extends',
90
- 'implements',
91
- 'interface',
92
- 'package',
93
- 'private',
94
- 'protected',
95
- 'public',
96
- 'static',
97
- 'type',
98
- 'enum',
99
- 'declare',
100
- 'readonly',
101
- 'any',
102
- 'number',
103
- 'string',
104
- 'boolean',
105
- 'null',
106
- 'undefined',
107
- 'true',
108
- 'false',
109
- 'get',
110
- 'set',
111
-
112
- // Vue 特定关键字
113
- 'ref',
114
- 'reactive',
115
- 'computed',
116
- 'watch',
117
- 'watchEffect',
118
- 'provide',
119
- 'inject',
120
- 'setup',
121
- 'onMounted',
122
- 'onUnmounted',
123
- 'onUpdated',
124
- 'onBeforeMount',
125
- 'onBeforeUpdate',
126
- 'onBeforeUnmount',
127
- 'nextTick',
128
- 'defineComponent',
129
- 'defineProps',
130
- 'defineEmits',
131
- 'defineExpose',
132
- 'withDefaults',
133
-
134
- // UniApp 特定关键字
135
- 'uni',
136
- 'getCurrentInstance',
137
- 'onLoad',
138
- 'onShow',
139
- 'onReady',
140
- 'onHide',
141
- 'onUnload',
142
- 'onPullDownRefresh',
143
- 'onReachBottom',
144
- 'onShareAppMessage',
145
- 'onShareTimeline',
146
- 'onAddToFavorites',
147
- 'onPageScroll',
148
- 'onResize',
149
- 'onTabItemTap',
150
- ];
151
-
152
- /** 内置全局对象/类型 */
153
- const builtInList = [
154
- 'Object',
155
- 'Array',
156
- 'String',
157
- 'Number',
158
- 'Boolean',
159
- 'Promise',
160
- 'Math',
161
- 'JSON',
162
- 'Date',
163
- 'RegExp',
164
- 'Set',
165
- 'Map',
166
- 'Error',
167
- 'Symbol',
168
- ];
169
-
170
- type Word = {
171
- /** 字符 */
172
- word: string;
173
- /** css 样式 */
174
- class: string;
175
- /** 分类
176
- * - space: 空格
177
- * - bracket: 括号
178
- * - quotation: 引号
179
- * - string: 字符串
180
- * - function: 函数
181
- * - attribute: 属性
182
- * - operator: 操作符
183
- * - symbol: 符号
184
- * - keyword: 关键字
185
- * - number: 数字
186
- * - builtin: 内置对象
187
- * - other: 其他
188
- */
189
- type:
190
- | 'space'
191
- | 'bracket'
192
- | 'quotation'
193
- | 'string'
194
- | 'function'
195
- | 'attribute'
196
- | 'operator'
197
- | 'symbol'
198
- | 'keyword'
199
- | 'number'
200
- | 'builtin'
201
- | 'other';
202
- };
203
-
204
- export type LineInfo = {
205
- type: 'empty' | 'tag' | 'script' | 'style' | 'note';
206
- class: string;
207
- words: Word[];
208
- };
209
-
210
- const scriptStartReg = /<script(\s[^>]*)?>/i;
211
- const scriptEndReg = /<\/script>/i;
212
-
213
- const styleStartReg = /<style(\s[^>]*)?>/i;
214
- const styleEndReg = /<\/style>/i;
215
-
216
- const preClass = 'source-code-pre';
217
-
218
- /**
219
- * 获取代码中所有 <script> 块的起止行号(不包含标签本身)
220
- * @param lines 源码按行分割后的字符串数组
221
- * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
222
- */
223
- function getScriptStartAndEndRow(lines: string[]): number[][] {
224
- // 如果没有找到任何<script>标签,则认为整段代码都是script代码
225
- let hasScriptTags = false;
226
- const groups: number[][] = [];
227
- let currentStart: number | null = null; // 记录当前 <script> 开始行号
228
-
229
- lines.forEach((line, index) => {
230
- if (scriptStartReg.test(line)) {
231
- hasScriptTags = true;
232
- // 遇到开始标签,记录下一行作为内容开始位置
233
- currentStart = index + 1;
234
- } else if (scriptEndReg.test(line) && currentStart !== null) {
235
- // 遇到结束标签且已有开始位置,则记录为一个有效块
236
- // 如果开始行大于等于结束行(即<script></script>在同一行或相邻行),则不构成有效内容区间
237
- if (currentStart <= index - 1) {
238
- groups.push([currentStart, index - 1]); // 结束行为上一行(排除 </script>)
239
- }
240
- currentStart = null; // 重置等待下一个 <script>
241
- }
242
- });
243
-
244
- // 如果完全没有<script>标签,则整段代码都算作script内容
245
- if (!hasScriptTags && lines.length > 0) {
246
- // 整个文件都是script代码
247
- groups.push([0, lines.length - 1]);
248
- }
249
-
250
- return groups;
251
- }
252
-
253
- /**
254
- * 获取代码中所有 <style> 块的起止行号(不包含标签本身)
255
- * @param lines 源码按行分割后的字符串数组
256
- * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
257
- */
258
-
259
- function getStyleStartAndEndRow(lines: string[]): number[][] {
260
- const groups: number[][] = [];
261
- let currentStart: number | null = null; // 记录当前 <style> 开始行号
262
-
263
- lines.forEach((line, index) => {
264
- if (styleStartReg.test(line)) {
265
- // 遇到开始标签,记录下一行作为内容开始位置
266
- currentStart = index + 1;
267
- } else if (styleEndReg.test(line) && currentStart !== null) {
268
- // 遇到结束标签且已有开始位置,则记录为一个有效块
269
- // 如果开始行大于等于结束行(即<style></style>在同一行或相邻行),则不构成有效内容区间
270
- if (currentStart <= index - 1) {
271
- groups.push([currentStart, index - 1]); // 结束行为上一行(排除 </style>)
272
- }
273
- currentStart = null; // 重置等待下一个 <style>
274
- }
275
- });
276
-
277
- return groups;
278
- }
279
-
280
- /**
281
- * 获取代码中所有注释块的起止行号
282
- * @param lines 源码按行分割后的字符串数组
283
- * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
284
- */
285
- function getNoteStartAndEndRow(lines: string[]): number[][] {
286
- const groups: number[][] = [];
287
- let currentStart: number | null = null; // 记录当前注释块开始行号
288
- let inMultiLineComment = false; // 是否在多行注释 /* */ 中
289
- let inHtmlComment = false; // 是否在HTML注释 <!-- -->
290
-
291
- lines.forEach((line, index) => {
292
- const trimLine = line.trim();
293
-
294
- // 处理 /* */ 多行注释
295
- if (!inMultiLineComment && !inHtmlComment && trimLine.startsWith('/*')) {
296
- inMultiLineComment = true;
297
- currentStart = index;
298
-
299
- // 检查是否在同一行内结束多行注释
300
- if (
301
- trimLine.includes('*/') &&
302
- trimLine.indexOf('*/') > trimLine.indexOf('/*')
303
- ) {
304
- groups.push([currentStart, index]);
305
- inMultiLineComment = false;
306
- currentStart = null;
307
- }
308
- } else if (inMultiLineComment && trimLine.includes('*/')) {
309
- // 结束多行注释
310
- groups.push([currentStart!, index]);
311
- inMultiLineComment = false;
312
- currentStart = null;
313
- }
314
- // 处理 <!-- --> HTML注释
315
- else if (
316
- !inMultiLineComment &&
317
- !inHtmlComment &&
318
- trimLine.startsWith('<!--')
319
- ) {
320
- inHtmlComment = true;
321
- currentStart = index;
322
-
323
- // 检查是否在同一行内结束HTML注释
324
- if (
325
- trimLine.includes('-->') &&
326
- trimLine.indexOf('-->') > trimLine.indexOf('<!--')
327
- ) {
328
- groups.push([currentStart, index]);
329
- inHtmlComment = false;
330
- currentStart = null;
331
- }
332
- } else if (inHtmlComment && trimLine.includes('-->')) {
333
- // 结束HTML注释
334
- groups.push([currentStart!, index]);
335
- inHtmlComment = false;
336
- currentStart = null;
337
- }
338
- // 处理单行注释 // 或 /** */
339
- else if (
340
- !inMultiLineComment &&
341
- !inHtmlComment &&
342
- trimLine.startsWith('//')
343
- ) {
344
- groups.push([index, index]);
345
- }
346
- });
347
-
348
- return groups;
349
- }
350
-
351
- /**
352
- * 获取代码中的 标签起始行,不包含script 中的内容,不包含style中的内容
353
- * @param lines 源码按行分割后的字符串数组
354
- * @returns 包含标签、脚本、样式和注释范围的对象
355
- */
356
- function getTagStartAndEndRow(lines: string[]): {
357
- tagRanges: number[][];
358
- scriptRanges: number[][];
359
- styleRanges: number[][];
360
- noteRanges: number[][];
361
- } {
362
- // 先找出 script 和 style 的内容区域
363
- const scriptRanges = getScriptStartAndEndRow(lines);
364
- const styleRanges = getStyleStartAndEndRow(lines);
365
- const noteRanges = getNoteStartAndEndRow(lines);
366
-
367
- // 合并并排序所有需要排除的区间
368
- const excludeRanges = [...scriptRanges, ...styleRanges, ...noteRanges].sort(
369
- (a, b) => a[0] - b[0],
370
- );
371
-
372
- const tagRanges: number[][] = [];
373
-
374
- if (excludeRanges.length === 0) {
375
- // 如果没有 script 或 style 块,则整个文件都是 html 区域
376
- if (lines.length > 0) {
377
- tagRanges.push([0, lines.length - 1]);
378
- }
379
- } else {
380
- let currentStart = 0;
381
-
382
- for (let i = 0; i < excludeRanges.length; i++) {
383
- const [start, end] = excludeRanges[i];
384
-
385
- // 检查当前起始点到这个排除区间的开始是否有内容
386
- if (currentStart <= start - 1) {
387
- tagRanges.push([currentStart, start - 1]);
388
- }
389
-
390
- // 更新下一个可能的起始点为排除区间之后
391
- currentStart = end + 1;
392
- }
393
-
394
- // 检查最后一个排除区间结束后是否还有剩余内容
395
- if (currentStart < lines.length) {
396
- tagRanges.push([currentStart, lines.length - 1]);
397
- }
398
- }
399
-
400
- return {
401
- tagRanges,
402
- scriptRanges,
403
- styleRanges,
404
- noteRanges,
405
- };
406
- }
407
-
408
- /**
409
- * 获取指定行号对应的代码块类型
410
- *
411
- * @param {number} index
412
- * @param {{
413
- * tagRanges: number[][];
414
- * scriptRanges: number[][];
415
- * styleRanges: number[][];
416
- * }} {
417
- * tagRanges,
418
- * scriptRanges,
419
- * styleRanges,
420
- * }
421
- * @return {*}
422
- */
423
- function getLineIndexType(
424
- index: number,
425
- {
426
- tagRanges,
427
- scriptRanges,
428
- styleRanges,
429
- noteRanges,
430
- }: {
431
- tagRanges: number[][];
432
- scriptRanges: number[][];
433
- styleRanges: number[][];
434
- noteRanges: number[][];
435
- },
436
- ) {
437
- // 检查是否在注释区间内
438
- for (const [start, end] of noteRanges) {
439
- if (index >= start && index <= end) {
440
- return 'note';
441
- }
442
- }
443
-
444
- // 检查是否在 script 区间内
445
- for (const [start, end] of scriptRanges) {
446
- if (index >= start && index <= end) {
447
- return 'script';
448
- }
449
- }
450
-
451
- // 检查是否在 style 区间内
452
- for (const [start, end] of styleRanges) {
453
- if (index >= start && index <= end) {
454
- return 'style';
455
- }
456
- }
457
-
458
- // 检查是否在 tag 区间内
459
- for (const [start, end] of tagRanges) {
460
- if (index >= start && index <= end) {
461
- return 'tag';
462
- }
463
- }
464
-
465
- return 'empty';
466
- }
467
-
468
- /**
469
- * 获取代码中的单词信息
470
- *
471
- * TODO: 属性 , 方法
472
- *
473
- * @param {string[]} words
474
- * @return {*} {Word[]}
475
- */
476
- function getWordsInfo(words: string[]): Word[] {
477
- let quotation = '';
478
- return words.map((w, index) => {
479
- // 空格
480
- const isSpace = w === ' ';
481
- if (isSpace) {
482
- return {
483
- word: w,
484
- type: 'space',
485
- class: `${preClass}-space`,
486
- };
487
- }
488
-
489
- // 箭头函数
490
- const isArrow =
491
- (w === '=' && words?.[index + 1] === '>') ||
492
- (w === '>' && words?.[index - 1] === '=');
493
- if (isArrow) {
494
- return {
495
- word: w,
496
- type: 'keyword',
497
- class: `${preClass}-keyword`,
498
- };
499
- }
500
-
501
- // 括号
502
- const isBracket = bracketList.includes(w);
503
- if (isBracket) {
504
- return {
505
- word: w,
506
- type: 'bracket',
507
- class: `${preClass}-bracket`,
508
- };
509
- }
510
-
511
- // 引号
512
- const isQuotation = quotationList.includes(w);
513
- if (isQuotation) {
514
- quotation = quotation !== '' && quotation === w ? '' : w;
515
- return {
516
- word: w,
517
- type: 'quotation',
518
- class: `${preClass}-quotation`,
519
- };
520
- }
521
-
522
- if (quotation != '') {
523
- return {
524
- word: w,
525
- type: 'string',
526
- class: `${preClass}-string`,
527
- };
528
- }
529
-
530
- // 字符串 (处理引号后的第一个词)
531
- const isStr = quotationList.includes(words?.[index - 1]);
532
- if (isStr) {
533
- return {
534
- word: w,
535
- type: 'string',
536
- class: `${preClass}-string`,
537
- };
538
- }
539
-
540
- // 数字
541
- const isNumber =
542
- /^\d+(\.\d+)?(e\d+)?$/i.test(w) || /^0x[0-9a-f]+$/i.test(w);
543
- if (isNumber) {
544
- return {
545
- word: w,
546
- type: 'number',
547
- class: `${preClass}-number`,
548
- };
549
- }
550
-
551
- // 函数
552
- const isFunc =
553
- (words?.[index - 1] === ' ' && words?.[index - 2] === 'function') ||
554
- words?.[index + 1] === '(' ||
555
- (words?.[index + 1] === ' ' && words?.[index + 2] === '(');
556
- if (isFunc && !keywordList.includes(w)) {
557
- return {
558
- word: w,
559
- type: 'function',
560
- class: `${preClass}-function`,
561
- };
562
- }
563
-
564
- // 属性提取
565
- const preW = symbolList.includes(words?.[index - 1]);
566
- const isAttribute =
567
- (words?.[index + 1] === '.' || (words?.[index - 1] === '.' && !preW)) &&
568
- !isNumber;
569
- if (isAttribute) {
570
- return {
571
- word: w,
572
- type: 'attribute',
573
- class: `${preClass}-attribute`,
574
- };
575
- }
576
-
577
- // 关键字
578
- const isKeyword = keywordList.includes(w);
579
- if (isKeyword) {
580
- return {
581
- word: w,
582
- type: 'keyword',
583
- class: `${preClass}-keyword`,
584
- };
585
- }
586
-
587
- // 内置对象
588
- const isBuiltin = builtInList.includes(w);
589
- if (isBuiltin) {
590
- return {
591
- word: w,
592
- type: 'builtin',
593
- class: `${preClass}-builtin`,
594
- };
595
- }
596
-
597
- // 操作符
598
- const isOperator = operatorList.includes(w);
599
- if (isOperator) {
600
- return {
601
- word: w,
602
- type: 'operator',
603
- class: `${preClass}-operator`,
604
- };
605
- }
606
-
607
- // 符号
608
- const isSymbol = symbolList.includes(w);
609
- if (isSymbol) {
610
- return {
611
- word: w,
612
- type: 'symbol',
613
- class: `${preClass}-symbol`,
614
- };
615
- }
616
-
617
- // 其他
618
- return {
619
- word: w,
620
- type: 'other',
621
- class: `${preClass}-other`,
622
- };
623
- });
624
- }
625
-
626
- /**
627
- * 按符号列表分割字符串
628
- * @param lineTrim 需要分割的字符串
629
- * @param symbols 符号列表
630
- * @returns 分割后的词元数组
631
- /**
632
- *
633
- *
634
- * @param {string} lineTrim
635
- * @param {string[]} symbols
636
- * @return {*} {string[]}
637
- */
638
- function splitBySymbols(lineTrim: string, symbols: string[]): string[] {
639
- if (!lineTrim) return [];
640
-
641
- // 跳过注释行的处理
642
- if (
643
- lineTrim.startsWith('/*') ||
644
- lineTrim.startsWith('*') ||
645
- lineTrim.startsWith('//') ||
646
- lineTrim.startsWith('<!--') ||
647
- lineTrim.startsWith('-->') ||
648
- lineTrim.length > 5000
649
- ) {
650
- return [lineTrim];
651
- }
652
-
653
- // 转义特殊字符并构建正则表达式
654
- const escapedSymbols = symbols.map((s) =>
655
- s.replace(/[.*+?^${}()|[\]\\@$]/g, '\\$&'),
656
- );
657
- const regexPattern = `(${escapedSymbols.join('|')})`;
658
- const regex = new RegExp(regexPattern, 'g');
659
-
660
- // 使用正则表达式分割字符串并保留分隔符
661
- const parts = lineTrim.split(regex);
662
-
663
- // 过滤掉空字符串并返回结果
664
- return parts.filter((part) => part !== '');
665
- }
666
-
667
- export function parseCode(code: string): LineInfo[] {
668
- // 按行进行重新解析
669
- const lines = code.split('\n');
670
-
671
- const { tagRanges, scriptRanges, styleRanges, noteRanges } =
672
- getTagStartAndEndRow(lines);
673
-
674
- const parseLine = lines.map((line, index) => {
675
- // 解析行之前的空格
676
- const space =
677
- line?.length < 500
678
- ? (line.match(startSpaceReg)?.[1] || '')?.split('')
679
- : [];
680
- // 获取行类型
681
-
682
- const type = getLineIndexType(index, {
683
- tagRanges,
684
- scriptRanges,
685
- styleRanges,
686
- noteRanges,
687
- });
688
-
689
- const lineTrim = line.trim();
690
-
691
- // 按符号分割行内容
692
- const segments = splitBySymbols(lineTrim, symbolList);
693
-
694
- const words = getWordsInfo([...space, ...segments]);
695
-
696
- const lineInfo: LineInfo = { type, words, class: `${preClass}-${type}` };
697
- return lineInfo;
698
- });
699
-
700
- return parseLine;
701
- }
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
+ }