-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n-swapper-plugin.js
1006 lines (891 loc) · 32.2 KB
/
i18n-swapper-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const utils = require('../utils');
const {
analyzeDocument
} = require('./src/panels/services/documentAnalyzer');
/**
* 递归查找对象中指定值的路径
* @param {Object} obj 要搜索的对象
* @param {string} value 要查找的值
* @param {string} currentPath 当前路径
* @returns {string|null} 返回找到的路径或null
*/
function findPathByValue(obj, value, currentPath = '') {
for (const key in obj) {
const newPath = currentPath ? `${currentPath}.${key}` : key;
if (typeof obj[key] === 'object' && obj[key] !== null) {
// 递归搜索对象
const result = findPathByValue(obj[key], value, newPath);
if (result) return result;
} else if (obj[key] === value) {
// 找到匹配的值
return newPath;
}
}
return null;
}
/**
* 加载国际化文件内容
* @param {string} filePath 文件路径
* @returns {Object|null} 返回文件内容对象或null
*/
function loadLocaleFile(filePath) {
try {
if (!fs.existsSync(filePath)) {
return null;
}
if (filePath.endsWith('.json')) {
// 加载JSON文件
const content = fs.readFileSync(filePath, 'utf8');
return JSON.parse(content);
} else if (filePath.endsWith('.js')) {
// 加载JS文件
try {
// 清除require缓存,确保获取最新内容
delete require.cache[require.resolve(filePath)];
return require(filePath);
} catch (e) {
console.error(`加载JS文件失败: ${e.message}`);
return null;
}
}
return null;
} catch (error) {
console.error(`加载文件失败: ${error.message}`);
return null;
}
}
/**
* 批量替换面板类
*/
class BatchReplacementPanel {
constructor(context) {
this.context = context;
this.panel = null;
this.replacements = [];
this.document = null;
this.editor = null;
}
/**
* 创建并显示面板
*/
createOrShow() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('没有打开的编辑器');
return;
}
this.editor = editor;
this.document = editor.document;
// 如果已有面板,显示现有面板
if (this.panel) {
this.panel.reveal(vscode.ViewColumn.Beside);
return;
}
// 创建新的 WebView 面板 - 在编辑器旁边打开
this.panel = vscode.window.createWebviewPanel(
'i18nBatchReplacement',
'批量替换国际化',
vscode.ViewColumn.Beside, // 关键修改:使用Beside而不是One
{
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots: [
vscode.Uri.file(path.join(this.context.extensionPath, 'media'))
]
}
);
// 处理关闭事件
this.panel.onDidDispose(() => {
this.panel = null;
}, null, this.context.subscriptions);
// 处理 WebView 中的消息
this.panel.webview.onDidReceiveMessage(
async message => {
switch (message.command) {
case 'replace':
debugger
await this.performReplacements(message.replacements);
return;
case 'cancel':
this.panel.dispose();
return;
case 'addPattern':
console.log('addPattern', message.pattern);
await this.addScanPattern(message.pattern);
return;
case 'removePattern':
await this.removeScanPattern(message.pattern);
return;
case 'refreshScan':
await this.refreshScan();
return;
}
},
null,
this.context.subscriptions
);
// 分析当前文档并加载面板内容
this.loadPanelContent();
}
/**
* 加载面板内容
*/
async loadPanelContent() {
if (!this.panel) return;
const text = this.document.getText();
const fileExtension = path.extname(this.document.fileName).toLowerCase();
// 获取配置
const config = vscode.workspace.getConfiguration('i18n-swapper');
const scanPatterns = config.get('scanPatterns', []);
const localesPaths = config.get('localesPaths', []);
// 分析当前文档
this.replacements = await analyzeDocument(
text, fileExtension, scanPatterns, localesPaths
);
// 设置 WebView HTML 内容
this.panel.webview.html = this.getWebviewContent(scanPatterns, this.replacements);
}
/**
* 刷新扫描
*/
async refreshScan() {
await this.loadPanelContent();
}
/**
* 添加扫描模式
*/
async addScanPattern(pattern) {
if (!pattern) return;
const config = vscode.workspace.getConfiguration('i18n-swapper');
const scanPatterns = config.get('scanPatterns', []);
if (!scanPatterns.includes(pattern)) {
scanPatterns.push(pattern);
await config.update('scanPatterns', scanPatterns, vscode.ConfigurationTarget.Workspace);
await this.loadPanelContent();
}
}
/**
* 移除扫描模式
*/
async removeScanPattern(pattern) {
const config = vscode.workspace.getConfiguration('i18n-swapper');
let scanPatterns = config.get('scanPatterns', []);
scanPatterns = scanPatterns.filter(p => p !== pattern);
await config.update('scanPatterns', scanPatterns, vscode.ConfigurationTarget.Workspace);
await this.loadPanelContent();
}
/**
* 分析 Vue 文件
* @param {string} text 文件内容
* @param {string[]} scanPatterns 要扫描的属性模式
* @param {Array} replacements 收集替换项的数组
*/
analyzeVueFile(text, scanPatterns, replacements) {
try {
// 处理模板部分的属性
const templateRegex = /<template>[\s\S]*?<\/template>/i;
const templateMatch = text.match(templateRegex);
if (templateMatch) {
const templateText = templateMatch[0];
// 查找属性模式,如 value="文本", placeholder="文本" 等
for (const pattern of scanPatterns) {
const attrRegex = new RegExp(`${pattern}=["']([^"']+)["']`, 'g');
let match;
while ((match = attrRegex.exec(templateText)) !== null) {
const value = match[1];
// 检查是否包含中文字符或值得国际化的内容
if (this.shouldBeInternationalized(value)) {
replacements.push({
text: value,
start: match.index + match[0].indexOf(value),
end: match.index + match[0].indexOf(value) + value.length,
source: `${pattern} 属性`,
selected: true
});
}
}
}
// 查找标签之间的文本内容
const tagContentRegex = />([^<>]+)</g;
let contentMatch;
while ((contentMatch = tagContentRegex.exec(templateText)) !== null) {
const value = contentMatch[1].trim();
if (value && this.shouldBeInternationalized(value)) {
replacements.push({
text: value,
start: templateMatch.index + contentMatch.index + contentMatch[0].indexOf(value),
end: templateMatch.index + contentMatch.index + contentMatch[0].indexOf(value) + value.length,
source: '标签内容',
selected: true
});
}
}
}
// 处理脚本部分
const scriptRegex = /<script[\s\S]*?<\/script>/i;
const scriptMatch = text.match(scriptRegex);
if (scriptMatch) {
const scriptText = scriptMatch[0];
this.analyzeJsContent(scriptText, scriptMatch.index, scanPatterns, replacements);
}
} catch (error) {
console.error('分析 Vue 文件时出错:', error);
}
}
/**
* 分析 JS/TS 文件
* @param {string} text 文件内容
* @param {string[]} scanPatterns 要扫描的属性模式
* @param {Array} replacements 收集替换项的数组
*/
analyzeJsFile(text, scanPatterns, replacements) {
this.analyzeJsContent(text, 0, scanPatterns, replacements);
}
/**
* 分析 JS 内容
* @param {string} text 内容
* @param {number} baseIndex 基础索引
* @param {string[]} scanPatterns 要扫描的属性模式
* @param {Array} replacements 收集替换项的数组
*/
analyzeJsContent(text, baseIndex, scanPatterns, replacements) {
try {
// 查找对象属性
for (const pattern of scanPatterns) {
// 查找 pattern: "value" 或 pattern: 'value' 格式
// 使用两个捕获组:一个是引号类型,一个是值
const propRegex = new RegExp(`${pattern}\\s*:\\s*(["'])([^"']+)\\1`, 'g');
let match;
while ((match = propRegex.exec(text)) !== null) {
const quoteType = match[1]; // 引号类型: ' 或 "
const value = match[2]; // 引号内文本: 远程开门
if (this.shouldBeInternationalized(value)) {
// 获取值在原始文本中的位置
const propStart = match.index;
const valueStart = match[0].indexOf(value, pattern.length);
const absoluteStart = baseIndex + propStart + valueStart;
// 添加替换项时记录额外信息
replacements.push({
text: value, // 文本内容(不含引号)
start: absoluteStart, // 文本开始位置
end: absoluteStart + value.length, // 文本结束位置
propText: match[0], // 整个属性文本,如 label: '远程开门'
propStart: baseIndex + propStart, // 属性开始位置
propEnd: baseIndex + propStart + match[0].length, // 属性结束位置
quoteType: quoteType, // 引号类型
hasQuotes: true, // 标记有引号
source: `${pattern} 属性`,
selected: true
});
}
}
}
// 查找字符串字面量
const stringLiteralRegex = /["']([^"']+)["']/g;
let strMatch;
while ((strMatch = stringLiteralRegex.exec(text)) !== null) {
const value = strMatch[1];
// 检查是否在已找到的替换项中
const alreadyFound = replacements.some(item =>
baseIndex + strMatch.index + strMatch[0].indexOf(value) === item.start
);
if (!alreadyFound && this.shouldBeInternationalized(value)) {
replacements.push({
text: value,
start: baseIndex + strMatch.index + strMatch[0].indexOf(value),
end: baseIndex + strMatch.index + strMatch[0].indexOf(value) + value.length,
source: '字符串字面量',
selected: false // 默认不选中普通字符串
});
}
}
} catch (error) {
console.error('分析 JS 内容时出错:', error);
}
}
/**
* 分析 JSON 文件
* @param {string} text 文件内容
* @param {Array} replacements 收集替换项的数组
*/
analyzeJsonFile(text, replacements) {
try {
// 查找键值对
const valueRegex = /["']:\s*["']([^"']+)["']/g;
let match;
while ((match = valueRegex.exec(text)) !== null) {
const value = match[1];
if (this.shouldBeInternationalized(value)) {
replacements.push({
text: value,
start: match.index + match[0].indexOf(value),
end: match.index + match[0].indexOf(value) + value.length,
source: 'JSON 值',
selected: true
});
}
}
} catch (error) {
console.error('分析 JSON 文件时出错:', error);
}
}
/**
* 判断文本是否需要国际化
* @param {string} text 文本
* @returns {boolean} 是否需要国际化
*/
shouldBeInternationalized(text) {
// 检查是否包含中文字符
const hasChinese = /[\u4e00-\u9fa5]/.test(text);
// 检查文本长度和组成
const isTextContent = text.length > 1 && /\S/.test(text);
// 排除纯数字、日期、变量等
const isExcluded = /^[0-9.]+$/.test(text) || // 纯数字
/^\d{4}-\d{2}-\d{2}/.test(text) || // 日期格式
/^{{\s*.*\s*}}$/.test(text); // 变量插值
return (hasChinese || isTextContent) && !isExcluded;
}
/**
* 在国际化文件中查找匹配项
* @param {Array} replacements 替换项数组
* @param {string[]} localesPaths 国际化文件路径
*/
async findI18nMatches(replacements, localesPaths) {
try {
// 获取工作区根目录
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
console.log('未找到工作区文件夹');
return;
}
const rootPath = workspaceFolders[0].uri.fsPath;
const config = vscode.workspace.getConfiguration('i18n-swapper');
const functionName = config.get('functionName', 't');
const configQuoteType = config.get('quoteType', 'single');
const codeQuote = configQuoteType === 'single' ? "'" : '"';
// 加载所有国际化文件
for (const relativePath of localesPaths) {
const filePath = path.join(rootPath, relativePath);
const localeData = loadLocaleFile(filePath);
if (!localeData) {
console.log(`跳过不存在或无法加载的文件: ${filePath}`);
continue;
}
// 查找每个替换项的匹配键
for (const item of replacements) {
if (item.i18nKey) continue; // 已找到匹配,跳过
const result = findPathByValue(localeData, item.text);
if (result) {
item.i18nKey = result;
// 设置替换预览文本
item.replacement = `${functionName}(${codeQuote}${result}${codeQuote})`;
item.i18nFile = relativePath;
}
}
}
} catch (error) {
console.error('查找国际化匹配项时出错:', error);
}
}
/**
* 获取 WebView 内容
*/
getWebviewContent(scanPatterns, replacements) {
const matchedCount = replacements.filter(item => item.i18nKey).length;
return `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>批量替换国际化</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
padding: 0;
margin: 0;
color: var(--vscode-foreground);
background-color: var(--vscode-editor-background);
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
.main-container {
display: flex;
flex: 1;
overflow: hidden;
padding: 20px 20px 0 20px;
}
.container {
display: flex;
flex: 1;
overflow: hidden;
min-height: 0; /* 重要: 允许flex子项收缩 */
}
.left-panel {
width: 250px;
border-right: 1px solid var(--vscode-panel-border);
padding-right: 15px;
margin-right: 15px;
overflow-y: auto;
flex-shrink: 0;
}
.right-panel {
flex: 1;
overflow-y: auto;
min-width: 0; /* 重要: 允许内容收缩 */
}
h2 {
margin-top: 0;
color: var(--vscode-editor-foreground);
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid var(--vscode-panel-border);
}
th {
font-weight: 600;
background-color: var(--vscode-editor-lineHighlightBackground);
position: sticky;
top: 0;
z-index: 1;
}
td {
word-break: break-all; /* 确保长文本换行 */
max-width: 300px; /* 限制单元格最大宽度 */
}
.pattern-list {
margin-bottom: 20px;
}
.pattern-list div {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 5px;
padding: 5px;
background-color: var(--vscode-input-background);
border-radius: 3px;
}
input[type="text"] {
width: 100%;
padding: 5px;
margin-bottom: 10px;
background-color: var(--vscode-input-background);
color: var(--vscode-input-foreground);
border: 1px solid var(--vscode-panel-border);
}
button {
padding: 5px 10px;
background-color: var(--vscode-button-background);
color: var(--vscode-button-foreground);
border: none;
cursor: pointer;
border-radius: 3px;
}
button:hover {
background-color: var(--vscode-button-hoverBackground);
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.remove-pattern {
background-color: transparent;
color: var(--vscode-editor-foreground);
opacity: 0.7;
}
.remove-pattern:hover {
opacity: 1;
background-color: transparent;
}
.no-results {
text-align: center;
padding: 20px;
color: var(--vscode-disabledForeground);
}
.stats {
margin-bottom: 10px;
font-style: italic;
color: var(--vscode-descriptionForeground);
}
.key-display {
color: var(--vscode-textLink-foreground);
font-family: monospace;
}
.action-bar {
padding: 10px 20px;
display: flex;
justify-content: flex-end;
gap: 10px;
background-color: var(--vscode-editor-background);
border-top: 1px solid var(--vscode-panel-border);
flex-shrink: 0; /* 防止按钮栏被压缩 */
}
</style>
</head>
<body>
<div class="main-container">
<div class="container">
<div class="left-panel">
<h2>扫描模式</h2>
<div class="pattern-list">
${scanPatterns.map(pattern => `
<div>
<span>${this.escapeHtml(pattern)}</span>
<button class="remove-pattern" data-pattern="${this.escapeHtml(pattern)}">✕</button>
</div>
`).join('') || '<p>未配置扫描模式</p>'}
</div>
<div>
<h3>添加模式</h3>
<input type="text" id="new-pattern" placeholder="输入属性名称..." />
<button id="add-pattern">添加</button>
</div>
<button id="refresh-scan" style="margin-top: 20px; width: 100%;">刷新扫描</button>
</div>
<div class="right-panel">
<h2>替换结果</h2>
<div class="stats">
找到 ${replacements.length} 处可能需要国际化的文本,其中 ${matchedCount} 处找到了匹配的国际化键。
</div>
${replacements.length > 0 ? `
<button id="select-all" style="margin-bottom: 10px;">全选/取消全选</button>
<table>
<thead>
<tr>
<th style="width: 40px;"></th>
<th>文本</th>
<th>国际化键</th>
<th>替换为</th>
<th>来源</th>
</tr>
</thead>
<tbody>
${replacements.map((item, index) => `
<tr>
<td>
<input type="checkbox" id="check-${index}"
${item.selected ? 'checked' : ''}
${!item.i18nKey ? 'disabled' : ''} />
</td>
<td>${this.escapeHtml(item.text)}</td>
<td class="key-display">${item.i18nKey ? this.escapeHtml(item.i18nKey) : '<span style="color:var(--vscode-disabledForeground)">未找到匹配</span>'}</td>
<td>${item.replacement ? this.escapeHtml(item.replacement) : '<span style="color:var(--vscode-disabledForeground)">-</span>'}</td>
<td>${this.escapeHtml(item.source || '')}</td>
</tr>
`).join('')}
</tbody>
</table>
` : `
<div class="no-results">
<p>未找到需要国际化的文本</p>
<p>尝试添加更多扫描模式,或检查您的国际化文件是否正确配置</p>
</div>
`}
</div>
</div>
</div>
<div class="action-bar">
<button id="cancel">取消</button>
<button id="replace" ${replacements.some(item => item.i18nKey) ? '' : 'disabled'}>应用替换</button>
</div>
<script>
(function() {
const vscode = acquireVsCodeApi();
// 初始化替换项数据
const replacements = ${JSON.stringify(replacements)};
// 全选/取消全选
const selectAllBtn = document.getElementById('select-all');
if (selectAllBtn) {
selectAllBtn.addEventListener('click', () => {
const checkboxes = document.querySelectorAll('input[type="checkbox"]:not(:disabled)');
const allChecked = Array.from(checkboxes).every(cb => cb.checked);
checkboxes.forEach(cb => {
cb.checked = !allChecked;
});
});
}
// 取消按钮
document.getElementById('cancel').addEventListener('click', () => {
vscode.postMessage({ command: 'cancel' });
});
// 替换按钮
const replaceBtn = document.getElementById('replace');
if (replaceBtn) {
replaceBtn.addEventListener('click', () => {
// 更新选中状态
for (let i = 0; i < replacements.length; i++) {
const checkbox = document.getElementById('check-' + i);
if (checkbox) {
replacements[i].selected = checkbox.checked;
}
}
vscode.postMessage({
command: 'replace',
replacements: replacements
});
});
}
// 添加扫描模式
document.getElementById('add-pattern').addEventListener('click', () => {
const inputEl = document.getElementById('new-pattern');
const pattern = inputEl.value.trim();
if (pattern) {
vscode.postMessage({
command: 'addPattern',
pattern: pattern
});
inputEl.value = '';
}
});
// 回车添加模式
document.getElementById('new-pattern').addEventListener('keyup', (e) => {
if (e.key === 'Enter') {
const pattern = e.target.value.trim();
if (pattern) {
vscode.postMessage({
command: 'addPattern',
pattern: pattern
});
e.target.value = '';
}
}
});
// 移除模式
document.querySelectorAll('.remove-pattern').forEach(btn => {
btn.addEventListener('click', () => {
const pattern = btn.getAttribute('data-pattern');
vscode.postMessage({
command: 'removePattern',
pattern: pattern
});
});
});
// 刷新扫描
document.getElementById('refresh-scan').addEventListener('click', () => {
vscode.postMessage({
command: 'refreshScan'
});
});
})();
</script>
</body>
</html>
`;
}
/**
* HTML转义
*/
escapeHtml(text) {
return String(text)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/**
* 查找文本周围的引号,扩展替换范围
* @param {Object} document VSCode文档对象
* @param {Object} item 替换项
* @returns {Object} 包含扩展范围的对象
*/
findQuotesAround(document, item) {
try {
// 原始范围
const originalRange = new vscode.Range(
document.positionAt(item.start),
document.positionAt(item.end)
);
// 获取当前行文本
const line = document.lineAt(document.positionAt(item.start).line).text;
// 获取替换文本前后的字符
const charBefore = line.charAt(document.positionAt(item.start).character - 1);
const charAfter = line.charAt(document.positionAt(item.end).character);
// 检查是否被引号包围
if ((charBefore === "'" && charAfter === "'") ||
(charBefore === '"' && charAfter === '"')) {
// 扩展范围,包括引号
const expandedRange = new vscode.Range(
new vscode.Position(originalRange.start.line, originalRange.start.character - 1),
new vscode.Position(originalRange.end.line, originalRange.end.character + 1)
);
return {
hasQuotes: true,
range: expandedRange,
originalRange: originalRange,
quoteType: charBefore
};
}
return {
hasQuotes: false,
range: originalRange
};
} catch (error) {
console.error('查找引号时出错:', error);
return {
hasQuotes: false,
range: new vscode.Range(
document.positionAt(item.start),
document.positionAt(item.end)
)
};
}
}
}
/**
* 激活插件
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// 注册命令
let disposable = vscode.commands.registerCommand('i18n-swapper.replaceWithI18n', async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('没有打开的编辑器');
return;
}
const selection = editor.selection;
if (selection.isEmpty) {
vscode.window.showInformationMessage('未选中文本');
return;
}
// 获取选中的文本
const selectedText = editor.document.getText(selection);
const textToFind = selectedText.trim();
if (!textToFind) {
vscode.window.showInformationMessage('选中的文本为空');
return;
}
// 获取配置
const config = vscode.workspace.getConfiguration('i18n-swapper');
// 检查并选择国际化文件
const localesPaths = await checkAndSelectLocaleFile();
if (localesPaths.length === 0) {
return; // 用户取消了操作或没有选择文件
}
const configQuoteType = config.get('quoteType', 'single');
const functionName = config.get('functionName', 't');
const codeQuote = configQuoteType === 'single' ? "'" : '"';
try {
// 获取工作区根目录
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
vscode.window.showErrorMessage('未找到工作区文件夹');
return;
}
const rootPath = workspaceFolders[0].uri.fsPath;
// 在所有配置的文件中查找
let foundPath = null;
let foundInFile = null;
for (const relativePath of localesPaths) {
const filePath = path.join(rootPath, relativePath);
const localeData = loadLocaleFile(filePath);
if (!localeData) {
console.log(`跳过不存在或无法加载的文件: ${filePath}`);
continue;
}
// 查找匹配的键
const result = findPathByValue(localeData, textToFind);
if (result) {
foundPath = result;
foundInFile = relativePath;
break;
}
}
if (foundPath) {
// 替换选中的文本为配置的国际化函数调用
await editor.edit(editBuilder => {
const replacementResult = utils.replaceFn(textToFind, foundPath, functionName, codeQuote, editor.document, selection.start);
editBuilder.replace(selection, replacementResult.replacementText);
});
vscode.window.showInformationMessage(`已替换为: ${functionName}(${codeQuote}${foundPath}${codeQuote}) (从 ${foundInFile} 找到)`);
} else {
vscode.window.showInformationMessage(`未找到文本 "${textToFind}" 的国际化键`);
}
} catch (error) {
vscode.window.showErrorMessage(`发生错误: ${error.message}`);
}
});
// 添加批量替换命令
let batchPanel = new BatchReplacementPanel(context);
let batchCommand = vscode.commands.registerCommand('i18n-swapper.batchReplaceWithI18n', () => {
batchPanel.createOrShow();
});
// 添加快速批量替换命令
let quickBatchCommand = vscode.commands.registerCommand(
'i18n-swapper.quickBatchReplace',
quickBatchReplace
);
context.subscriptions.push(
disposable,
batchCommand,
quickBatchCommand
);
}
function deactivate() {}
module.exports = {
activate,
deactivate
};
/**
* 检查并引导用户选择国际化文件
* @returns {Promise<string[]>} 国际化文件路径数组
*/
async function checkAndSelectLocaleFile() {
// 获取配置的国际化文件路径
const config = vscode.workspace.getConfiguration('i18n-swapper');
let localesPaths = config.get('localesPaths', []);
// 检查是否有配置的路径
if (!localesPaths || localesPaths.length === 0 || (localesPaths.length === 1 && !localesPaths[0])) {
// 提示用户选择国际化文件
const result = await vscode.window.showInformationMessage(
'未配置国际化文件路径,是否立即选择文件?',
{ modal: true },
'选择文件'
);
if (result === '选择文件') {
// 打开文件选择器
const fileUris = await vscode.window.showOpenDialog({
canSelectMany: true,
filters: {
'国际化文件': ['json', 'js']
},
title: '选择国际化文件'
});
if (fileUris && fileUris.length > 0) {
// 转换为相对于工作区的路径
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
vscode.window.showErrorMessage('未找到工作区文件夹');
return [];
}
const rootPath = workspaceFolders[0].uri.fsPath;
const relativePaths = fileUris.map(uri => {
const filePath = uri.fsPath;
return path.relative(rootPath, filePath).replace(/\\/g, '/');
});
// 更新配置
await config.update('localesPaths', relativePaths, vscode.ConfigurationTarget.Workspace);
vscode.window.showInformationMessage(`已添加 ${relativePaths.length} 个国际化文件`);
return relativePaths;
} else {
vscode.window.showWarningMessage('未选择任何文件,操作取消');
return [];
}
} else {
vscode.window.showWarningMessage('未配置国际化文件,操作取消');