vite-uni-dev-tool 1.1.0 → 1.2.1

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 (195) hide show
  1. package/README.md +21 -1
  2. package/dist/const.cjs +1 -1
  3. package/dist/const.d.ts +2 -1
  4. package/dist/const.js +1 -1
  5. package/dist/core-shared.d.ts +2 -3
  6. package/dist/core.d.ts +8 -8
  7. package/dist/core.js +2 -2
  8. package/dist/i18n/index.d.ts +0 -1
  9. package/dist/i18n/instance.d.ts +0 -1
  10. package/dist/i18n/locales/en.cjs +1 -1
  11. package/dist/i18n/locales/en.d.ts +6 -1
  12. package/dist/i18n/locales/en.js +1 -1
  13. package/dist/i18n/locales/zh-Hans.cjs +1 -1
  14. package/dist/i18n/locales/zh-Hans.d.ts +6 -1
  15. package/dist/i18n/locales/zh-Hans.js +1 -1
  16. package/dist/index.d.ts +0 -1
  17. package/dist/modules/devConsole/index.cjs +4 -4
  18. package/dist/modules/devConsole/index.d.ts +6 -107
  19. package/dist/modules/devConsole/index.js +4 -4
  20. package/dist/modules/devEvent/index.cjs +3 -3
  21. package/dist/modules/devEvent/index.d.ts +2 -3
  22. package/dist/modules/devEvent/index.js +3 -3
  23. package/dist/modules/devIntercept/index.cjs +14 -14
  24. package/dist/modules/devIntercept/index.d.ts +34 -6
  25. package/dist/modules/devIntercept/index.js +14 -14
  26. package/dist/modules/devStore/index.cjs +1 -1
  27. package/dist/modules/devStore/index.d.ts +48 -40
  28. package/dist/modules/devStore/index.js +1 -1
  29. package/dist/modules/devToolInfo/index.cjs +1 -1
  30. package/dist/modules/devToolInfo/index.d.ts +6 -25
  31. package/dist/modules/devToolInfo/index.js +1 -1
  32. package/dist/plugins/uniDevTool/transform/transformApp.d.ts +0 -1
  33. package/dist/plugins/uniDevTool/transform/transformMain.cjs +3 -3
  34. package/dist/plugins/uniDevTool/transform/transformMain.d.ts +2 -2
  35. package/dist/plugins/uniDevTool/transform/transformMain.js +3 -3
  36. package/dist/plugins/uniDevTool/transform/transformVue.cjs +31 -25
  37. package/dist/plugins/uniDevTool/transform/transformVue.d.ts +2 -2
  38. package/dist/plugins/uniDevTool/transform/transformVue.js +30 -24
  39. package/dist/plugins/uniDevTool/uniDevTool.cjs +3 -3
  40. package/dist/plugins/uniDevTool/uniDevTool.d.ts +3 -2
  41. package/dist/plugins/uniDevTool/uniDevTool.js +3 -3
  42. package/dist/plugins/uniGlobalComponents/uniGlobalComponents.d.ts +0 -1
  43. package/dist/plugins/utils/index.d.ts +0 -1
  44. package/dist/shims-uni.d.ts +26 -22
  45. package/dist/type.d.ts +47 -43
  46. package/dist/utils/array.d.ts +0 -1
  47. package/dist/utils/date.d.ts +0 -1
  48. package/dist/utils/file.d.ts +0 -1
  49. package/dist/utils/function.d.ts +0 -1
  50. package/dist/utils/index.d.ts +0 -1
  51. package/dist/utils/ip.d.ts +0 -1
  52. package/dist/utils/language.d.ts +14 -15
  53. package/dist/utils/object.cjs +1 -1
  54. package/dist/utils/object.d.ts +7 -8
  55. package/dist/utils/object.js +1 -1
  56. package/dist/utils/openLink.d.ts +0 -1
  57. package/dist/utils/page.d.ts +0 -1
  58. package/dist/utils/platform.d.ts +0 -1
  59. package/dist/utils/string.d.ts +0 -1
  60. package/dist/utils/utils.d.ts +0 -1
  61. package/dist/v3/DevTool/components/BluetoothList/BluetoothItem.vue +199 -199
  62. package/dist/v3/DevTool/components/BluetoothList/BluetoothTool.vue +730 -730
  63. package/dist/v3/DevTool/components/BluetoothList/index.vue +167 -167
  64. package/dist/v3/DevTool/components/CaptureScreen/index.vue +109 -109
  65. package/dist/v3/DevTool/components/ConsoleList/ConsoleItem.vue +230 -225
  66. package/dist/v3/DevTool/components/ConsoleList/RunJSInput.vue +247 -247
  67. package/dist/v3/DevTool/components/ConsoleList/index.vue +171 -171
  68. package/dist/v3/DevTool/components/ConsoleList/staticTips.ts +1145 -1145
  69. package/dist/v3/DevTool/components/DevToolButton/index.vue +42 -6
  70. package/dist/v3/DevTool/components/DevToolTitle/index.vue +24 -24
  71. package/dist/v3/DevTool/components/DevToolWindow/DevToolOverlay.vue +197 -197
  72. package/dist/v3/DevTool/components/DevToolWindow/hooks/dataUtils.ts +48 -48
  73. package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolData.ts +411 -387
  74. package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolHandlers.ts +644 -629
  75. package/dist/v3/DevTool/components/DevToolWindow/hooks/useDevToolOverlay.ts +201 -197
  76. package/dist/v3/DevTool/components/DevToolWindow/index.vue +27 -64
  77. package/dist/v3/DevTool/components/ElEvent/ElEventItem.vue +105 -105
  78. package/dist/v3/DevTool/components/ElEvent/index.vue +106 -106
  79. package/dist/v3/DevTool/components/Instance/components/InstanceTreeNode.vue +265 -265
  80. package/dist/v3/DevTool/components/Instance/flatten.ts +226 -226
  81. package/dist/v3/DevTool/components/Instance/index.vue +94 -94
  82. package/dist/v3/DevTool/components/Instance/registry.ts +49 -49
  83. package/dist/v3/DevTool/components/Instance/transformTree.ts +393 -375
  84. package/dist/v3/DevTool/components/Instance/transformTreeCtx.ts +268 -268
  85. package/dist/v3/DevTool/components/Instance/typing.d.ts +43 -43
  86. package/dist/v3/DevTool/components/InstanceDetail/index.vue +485 -485
  87. package/dist/v3/DevTool/components/JsonDetail/index.vue +70 -70
  88. package/dist/v3/DevTool/components/NFCList/NFCItem.vue +112 -112
  89. package/dist/v3/DevTool/components/NFCList/NFCTool.vue +454 -454
  90. package/dist/v3/DevTool/components/NFCList/const.ts +56 -56
  91. package/dist/v3/DevTool/components/NFCList/index.vue +94 -94
  92. package/dist/v3/DevTool/components/NetworkList/InterceptConfig.vue +624 -624
  93. package/dist/v3/DevTool/components/NetworkList/InterceptItem.vue +140 -140
  94. package/dist/v3/DevTool/components/NetworkList/NetworkDetail.vue +287 -287
  95. package/dist/v3/DevTool/components/NetworkList/NetworkIntercept.vue +88 -88
  96. package/dist/v3/DevTool/components/NetworkList/NetworkItem.vue +163 -163
  97. package/dist/v3/DevTool/components/NetworkList/NetworkSend.vue +589 -589
  98. package/dist/v3/DevTool/components/NetworkList/const.ts +4 -4
  99. package/dist/v3/DevTool/components/NetworkList/hooks/useNetworkForm.ts +86 -86
  100. package/dist/v3/DevTool/components/NetworkList/index.vue +160 -160
  101. package/dist/v3/DevTool/components/NetworkList/utils.ts +101 -101
  102. package/dist/v3/DevTool/components/Performance/index.vue +498 -498
  103. package/dist/v3/DevTool/components/Performance/modules/PerformanceMetrics.vue +153 -153
  104. package/dist/v3/DevTool/components/Performance/modules/usePerformanceChart.ts +460 -460
  105. package/dist/v3/DevTool/components/Performance/modules/usePerformanceData.ts +258 -258
  106. package/dist/v3/DevTool/components/PiniaList/index.vue +93 -93
  107. package/dist/v3/DevTool/components/RunJS/index.vue +148 -148
  108. package/dist/v3/DevTool/components/ScanCodeList/ScanCodeItem.vue +97 -97
  109. package/dist/v3/DevTool/components/ScanCodeList/index.vue +100 -100
  110. package/dist/v3/DevTool/components/SettingButton/index.vue +45 -45
  111. package/dist/v3/DevTool/components/SettingList/index.vue +226 -218
  112. package/dist/v3/DevTool/components/SettingList/modules/SettingBarrage.vue +320 -304
  113. package/dist/v3/DevTool/components/SettingList/modules/SettingDevTool.vue +212 -212
  114. package/dist/v3/DevTool/components/SettingList/modules/SettingInfo.vue +157 -157
  115. package/dist/v3/DevTool/components/SettingList/modules/SettingLanguage.vue +74 -74
  116. package/dist/v3/DevTool/components/SettingList/modules/SettingLog.vue +230 -230
  117. package/dist/v3/DevTool/components/SettingList/typing.d.ts +2 -2
  118. package/dist/v3/DevTool/components/SourceCode/Line.vue +127 -127
  119. package/dist/v3/DevTool/components/SourceCode/parseCode.ts +609 -609
  120. package/dist/v3/DevTool/components/StorageList/index.vue +174 -174
  121. package/dist/v3/DevTool/components/TransferList/TransferDetail.vue +268 -268
  122. package/dist/v3/DevTool/components/VuexList/index.vue +84 -84
  123. package/dist/v3/DevTool/index.vue +85 -138
  124. package/dist/v3/components/AppTransition/index.vue +156 -176
  125. package/dist/v3/components/AutoSizer/index.vue +192 -192
  126. package/dist/v3/components/AutoSizer/index1.vue +184 -184
  127. package/dist/v3/components/AutoSizer/utils.ts +49 -49
  128. package/dist/v3/components/Barrage/BarrageItem.vue +194 -137
  129. package/dist/v3/components/Barrage/index.vue +205 -202
  130. package/dist/v3/components/CircularButton/index.vue +84 -84
  131. package/dist/v3/components/CustomSwiper/CustomSwiperItem.vue +49 -49
  132. package/dist/v3/components/CustomSwiper/index.vue +104 -104
  133. package/dist/v3/components/DevErrorBoundary/index.vue +388 -0
  134. package/dist/v3/components/DraggableContainer/index.vue +186 -73
  135. package/dist/v3/components/Empty/index.vue +29 -29
  136. package/dist/v3/components/FilterSelect/index.vue +179 -179
  137. package/dist/v3/components/JsonPretty/components/Brackets/index.vue +27 -27
  138. package/dist/v3/components/JsonPretty/components/Carets/index.vue +59 -59
  139. package/dist/v3/components/JsonPretty/components/CheckController/index.vue +136 -136
  140. package/dist/v3/components/JsonPretty/components/TreeNode/index.vue +387 -387
  141. package/dist/v3/components/JsonPretty/hooks/useClipboard.ts +21 -21
  142. package/dist/v3/components/JsonPretty/hooks/useError.ts +21 -21
  143. package/dist/v3/components/JsonPretty/type.ts +127 -127
  144. package/dist/v3/components/JsonPretty/utils/index.ts +169 -169
  145. package/dist/v3/components/MovableContainer/index.vue +10 -4
  146. package/dist/v3/components/Pick/index.vue +321 -322
  147. package/dist/v3/components/Tag/index.vue +113 -113
  148. package/dist/v3/components/VirtualList/AutoSize.vue +40 -40
  149. package/dist/v3/components/VirtualList/index.vue +416 -416
  150. package/dist/v3/hooks/useBluetooth/index.ts +561 -561
  151. package/dist/v3/hooks/useContainerStyle.ts +95 -153
  152. package/dist/v3/hooks/useNFC/index.ts +107 -107
  153. package/dist/v3/hooks/useNFC/typing.d.ts +396 -396
  154. package/dist/v3/hooks/useNFC/useNFCAndroid.ts +966 -966
  155. package/dist/v3/hooks/useNFC/useNFCMpWeiXin.ts +812 -812
  156. package/dist/v3/hooks/useNFC/utils.ts +754 -754
  157. package/dist/v3/hooks/useRequest/index.ts +586 -586
  158. package/dist/v3/hooks/useRequest/utils.ts +267 -267
  159. package/dist/v3/hooks/useScanCode/index.ts +206 -206
  160. package/dist/v3/hooks/useWebsocket/index.ts +253 -253
  161. package/dist/v3/styles/theme.ts +12 -12
  162. package/package.json +70 -59
  163. package/dist/const.d.ts.map +0 -1
  164. package/dist/core-shared.d.ts.map +0 -1
  165. package/dist/core.d.ts.map +0 -1
  166. package/dist/i18n/index.d.ts.map +0 -1
  167. package/dist/i18n/instance.d.ts.map +0 -1
  168. package/dist/i18n/locales/en.d.ts.map +0 -1
  169. package/dist/i18n/locales/zh-Hans.d.ts.map +0 -1
  170. package/dist/index.d.ts.map +0 -1
  171. package/dist/modules/devConsole/index.d.ts.map +0 -1
  172. package/dist/modules/devEvent/index.d.ts.map +0 -1
  173. package/dist/modules/devIntercept/index.d.ts.map +0 -1
  174. package/dist/modules/devStore/index.d.ts.map +0 -1
  175. package/dist/modules/devToolInfo/index.d.ts.map +0 -1
  176. package/dist/plugins/uniDevTool/transform/transformApp.d.ts.map +0 -1
  177. package/dist/plugins/uniDevTool/transform/transformMain.d.ts.map +0 -1
  178. package/dist/plugins/uniDevTool/transform/transformVue.d.ts.map +0 -1
  179. package/dist/plugins/uniDevTool/uniDevTool.d.ts.map +0 -1
  180. package/dist/plugins/uniGlobalComponents/uniGlobalComponents.d.ts.map +0 -1
  181. package/dist/plugins/utils/index.d.ts.map +0 -1
  182. package/dist/type.d.ts.map +0 -1
  183. package/dist/utils/array.d.ts.map +0 -1
  184. package/dist/utils/date.d.ts.map +0 -1
  185. package/dist/utils/file.d.ts.map +0 -1
  186. package/dist/utils/function.d.ts.map +0 -1
  187. package/dist/utils/index.d.ts.map +0 -1
  188. package/dist/utils/ip.d.ts.map +0 -1
  189. package/dist/utils/language.d.ts.map +0 -1
  190. package/dist/utils/object.d.ts.map +0 -1
  191. package/dist/utils/openLink.d.ts.map +0 -1
  192. package/dist/utils/page.d.ts.map +0 -1
  193. package/dist/utils/platform.d.ts.map +0 -1
  194. package/dist/utils/string.d.ts.map +0 -1
  195. package/dist/utils/utils.d.ts.map +0 -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
+ }