-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
294 lines (235 loc) · 8.58 KB
/
extension.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
const vscode = require('vscode');
const { baiduTranslator, bingTranslator, iflyrecTranslator, Languages } = require('node-translates');
var TRAN_SELECT = null;
// 翻译函数
async function translate(text) {
try {
// 输出翻译引擎
vscode.window.showInformationMessage(`Translator: ${TRAN_SELECT}`);
if (TRAN_SELECT === "baiduTranslator") {
return await baiduTranslator(text);
} else if (TRAN_SELECT === "iflyrecTranslator") {
return await iflyrecTranslator(text);
}
else {
return await bingTranslator(text);
}
} catch (error) {
vscode.window.showErrorMessage('Translation failed: ' + error.message);
}
}
// 英文翻译为中文
async function translate_en_to_zh(text) {
try {
// 输出翻译引擎
vscode.window.showInformationMessage(`Translator: ${TRAN_SELECT}`);
if (TRAN_SELECT === "baiduTranslator") {
return await baiduTranslator({ text, from: Languages.EN, to: Languages.ZH });
} else if (TRAN_SELECT === "iflyrecTranslator") {
return await iflyrecTranslator({ text, from: Languages.EN, to: Languages.ZH });
}
else {
return await bingTranslator({ text, from: Languages.EN, to: Languages.ZH });
}
} catch (error) {
vscode.window.showErrorMessage('Translation failed: ' + error.message);
}
}
// 处理翻译后的文本转化为小写下划线命名发去除空格
function toUnderlineCase(str) {
return str.toLowerCase().replace(/\s+/g, '_');
}
// 处理翻译后的文本转化为小驼峰命名 发去除空格
function toCamelCase(str) {
return str.toLowerCase().replace(/\s+/g, '_').replace(/_([a-z])/g, (all, letter) => letter.toUpperCase());
}
// 处理翻译后的文本转化为大驼峰命名 发去除空格
function toPascalCase(str) {
return str.toLowerCase().replace(/\s+/g, '_').replace(/_([a-z])/g, (all, letter) => letter.toUpperCase()).replace(/^[a-z]/, letter => letter.toUpperCase());
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// 读取配置选择翻译
TRAN_SELECT = vscode.workspace.getConfiguration('varTranPlugin').TranslatorSelection;
// 监听配置变化
vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration && event.affectsConfiguration('varTranPlugin.TranslatorSelection')) {
TRAN_SELECT = vscode.workspace.getConfiguration('varTranPlugin').TranslatorSelection;
vscode.window.showInformationMessage(`Change Translator: ${TRAN_SELECT}`);
}
});
// 普通翻译
let disposable = vscode.commands.registerCommand('var-tran-plugin.translate', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
// 获取选中的文本
const text = document.getText(selection);
// 如果选中的文本为空
if (!text) {
vscode.window.showErrorMessage('No text selected');
return;
}
try {
// 异步调用翻译函数并等待其结果
const replacedObject = await translate({ text, from: Languages.ZH, to: Languages.EN });
// 确保replacedObject有text属性和dst属性
if (replacedObject.text && replacedObject.dst) {
// 用替换后的文本替换原本选中的文本
await editor.edit(editBuilder => {
editBuilder.replace(selection, replacedObject.dst);
});
} else {
// 如果 replacedText 不是字符串,处理错误
vscode.window.showErrorMessage('Translation did not return a string.');
}
} catch (error) {
// 处理可能出现的错误
vscode.window.showErrorMessage('Error replacing text: ' + error.message);
}
}
});
// 小驼峰命名
let disposable_1 = vscode.commands.registerCommand('var-tran-plugin.translate_camelCase', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
// 获取选中的文本
const text = document.getText(selection);
// 如果选中的文本为空
if (!text) {
vscode.window.showErrorMessage('No text selected');
return;
}
try {
// 异步调用翻译函数并等待其结果
const replacedObject = await translate({ text, from: Languages.ZH, to: Languages.EN });
// 确保replacedObject有text属性和dst属性
if (replacedObject.text && replacedObject.dst) {
// 用替换后的文本替换原本选中的文本
await editor.edit(editBuilder => {
editBuilder.replace(selection, toCamelCase(replacedObject.dst));
});
} else {
// 如果 replacedText 不是字符串,处理错误
vscode.window.showErrorMessage('Translation did not return a string.');
}
} catch (error) {
// 处理可能出现的错误
vscode.window.showErrorMessage('Error replacing text: ' + error.message);
}
}
});
// 大驼峰命名
let disposable_2 = vscode.commands.registerCommand('var-tran-plugin.translate_PascalCase', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
// 获取选中的文本
const text = document.getText(selection);
// 如果选中的文本为空
if (!text) {
vscode.window.showErrorMessage('No text selected');
return;
}
try {
// 异步调用翻译函数并等待其结果
const replacedObject = await translate({ text, from: Languages.ZH, to: Languages.EN });
// 确保replacedObject有text属性和dst属性
if (replacedObject.text && replacedObject.dst) {
// 用替换后的文本替换原本选中的文本
await editor.edit(editBuilder => {
editBuilder.replace(selection, toPascalCase(replacedObject.dst));
});
} else {
// 如果 replacedText 不是字符串,处理错误
vscode.window.showErrorMessage('Translation did not return a string.');
}
} catch (error) {
// 处理可能出现的错误
vscode.window.showErrorMessage('Error replacing text: ' + error.message);
}
}
});
// 下划线命名
let disposable_3 = vscode.commands.registerCommand('var-tran-plugin.translate_snake_case', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
// 获取选中的文本
const text = document.getText(selection);
// 如果选中的文本为空
if (!text) {
vscode.window.showErrorMessage('No text selected');
return;
}
try {
// 异步调用翻译函数并等待其结果
const replacedObject = await translate({ text, from: Languages.ZH, to: Languages.EN });
// 确保replacedObject有text属性和dst属性
if (replacedObject.text && replacedObject.dst) {
// 用替换后的文本替换原本选中的文本
await editor.edit(editBuilder => {
editBuilder.replace(selection, toUnderlineCase(replacedObject.dst));
});
} else {
// 如果 replacedText 不是字符串,处理错误
vscode.window.showErrorMessage('Translation did not return a string.');
}
} catch (error) {
// 处理可能出现的错误
vscode.window.showErrorMessage('Error replacing text: ' + error.message);
}
}
});
// 英文翻译为中文
let disposable_4 = vscode.commands.registerCommand('var-tran-plugin.translate_china', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
// 获取选中的文本
const text = document.getText(selection);
// 如果选中的文本为空
if (!text) {
vscode.window.showErrorMessage('No text selected');
return;
}
try {
// 异步调用翻译函数并等待其结果
const replacedObject = await translate_en_to_zh(text);
// 确保replacedObject有text属性和dst属性
if (replacedObject.text && replacedObject.dst) {
// 用替换后的文本替换原本选中的文本
await editor.edit(editBuilder => {
editBuilder.replace(selection, replacedObject.dst);
});
} else {
// 如果 replacedText 不是字符串,处理错误
vscode.window.showErrorMessage('Translation did not return a string.');
}
} catch (error) {
// 处理可能出现的错误
vscode.window.showErrorMessage('Error replacing text: ' + error.message);
}
}
});
// 注册命令
context.subscriptions.push(disposable);
context.subscriptions.push(disposable_1);
context.subscriptions.push(disposable_2);
context.subscriptions.push(disposable_3);
context.subscriptions.push(disposable_4);
}
// This method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}