This repository has been archived by the owner on Nov 16, 2024. It is now read-only.
forked from funkyremi/vscode-google-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
486 lines (450 loc) · 15.3 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
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
const vscode = require("vscode");
const languages = require("./languages.js");
const gti = require("@vitalets/google-translate-api");
const tunnel = require("tunnel");
const he = require("he");
const path = require("path");
const vscodeLanguageClient = require("vscode-languageclient");
const humanizeString = require("humanize-string");
const camelcase = require("camelcase");
const pkg = require("./package.json");
function translate(text, options) {
const gotopts = {};
if (options && options.proxy) {
const proxy = {
host: options.proxy.host,
port: options.proxy.port,
headers: {
"User-Agent": "Node",
},
};
if (options.proxy.auth) {
proxy.proxyAuth = `${options.proxy.auth.username}:${options.proxy.auth.password}`;
}
gotopts.agent = tunnel.httpsOverHttp({
proxy: proxy,
});
}
return gti(text, options, gotopts).then((res) => {
res.data = [res.text];
return res;
});
}
/**
* @typedef TranslateRes
* @property {vscode.Selection} selection Selection
* @property {string} translation Result
*/
/**
* The list of recently used languages
*
* @type {Array.<string>}
*/
const recentlyUsed = [];
let client = null;
/**
* Updates languages lists for the convenience of users
*
* @param {string} selectedLanguage The language code to update
* @returns {undefined}
*/
function updateLanguageList(selectedLanguage) {
const index = recentlyUsed.findIndex((r) => r === selectedLanguage);
if (index !== -1) {
// Remove the recently used language from the list
recentlyUsed.splice(index, 1);
}
// Add the language in recently used languages
recentlyUsed.splice(0, 0, selectedLanguage);
}
/**
* Extracts a text from the active document selection
*
* @param {vscode.TextDocument} document The current document
* @param {vscode.Selection} selection The current selection
* @returns {string} A text
*/
function getSelectedText(document, selection) {
const charRange = new vscode.Range(selection.start.line, selection.start.character, selection.end.line, selection.end.character);
return document.getText(charRange);
}
/**
* Gets a text of the first line from active selection
*
* @param {vscode.TextDocument} document The current document
* @param {vscode.Selection} selection The current selection
* @returns {string}
*/
function getSelectedLineText(document, selection) {
return document.getText(document.lineAt(selection.start.line).rangeIncludingLineBreak);
}
/**
* Translates the selectedText to the selectedLanguage like a Promise
*
* @param {string} selectedText Text
* @param {string} selectedLanguage Language
* @param {vscode.Selection} selection Selection
* @returns {Promise.<TranslateRes>}
*/
function getTranslationPromise(selectedText, selectedLanguage, selection) {
return new Promise((resolve, reject) => {
const { host, port, username, password } = getProxyConfig();
const translationConfiguration = {
to: selectedLanguage,
};
if (!!host) {
translationConfiguration.proxy = {
host,
port: Number(port),
};
if (!!username) {
translationConfiguration.proxy.auth = {
username,
password,
};
}
}
let decoration = vscode.window.createTextEditorDecorationType({
color: '#FF2D00',
backgroundColor: "transparent"
});
vscode.window.activeTextEditor.setDecorations(decoration, [selection]);
translate(selectedText, translationConfiguration)
.then((res) => {
if (!!res && !!res.data) {
// If google rejects the string it will return the same string as input
// We can try to split the string into parts, then translate again. Then return it to a
// camel style casing
if (res.data[0] === selectedText) {
translate(humanizeString(selectedText), translationConfiguration).then((res) => {
if (!!res && !!res.data) {
resolve(
/** @type {TranslateRes} */ {
selection,
translation: camelcase(res.data[0]),
}
);
} else {
reject(new Error("Google Translation API issue"));
}
});
} else {
resolve(
/** @type {TranslateRes} */ {
selection,
translation: res.data[0],
}
);
}
} else {
reject(new Error("Google Translation API issue"));
}
decoration.dispose();
})
.catch((e) => {
reject(new Error("Google Translation API issue: " + e.message))
decoration.dispose();
});
});
}
/**
* Generates the array of promises based on selections
*
* @param {Array.<vscode.Selection>} selections Array of selections
* @param {vscode.TextDocument} document The current document
* @param {string} selectedLanguage The current language
* @returns {Array.<Promise<TranslateRes>>}
*/
function getTranslationsPromiseArray(selections, document, selectedLanguage) {
return selections.map((selection) => {
const selectedText = getSelectedText(document, selection);
return getTranslationPromise(selectedText, selectedLanguage, selection);
});
}
/**
* Gets arrays of Translation Promises based on the first lines under the cursor.
*
* @param {vscode.Selection} selections The current selection
* @param {vscode.TextDocument} document The current document
* @param {string} selectedLanguage
* @returns {Array.<Promise<TranslateRes>>}
*/
function getTranslationsPromiseArrayLine(selections, document, selectedLanguage) {
return selections.map((selection) => {
const selectedLineText = getSelectedLineText(document, selection);
return getTranslationPromise(selectedLineText, selectedLanguage, selection);
});
}
/**
* Returns user settings Preferred language.
* If user hasn't set preferred lang. Prompt to set.
*/
function getPreferredLanguage() {
return vscode.workspace.getConfiguration("vscodeGoogleTranslate").get("preferredLanguage") || setPreferredLanguage();
}
async function setPreferredLanguage() {
const quickPickData = recentlyUsed
.map((r) => ({
label: r,
description: "(recently used)",
}))
.concat(languages.map((r) => ({ label: r.name })));
const selectedLanguage = await vscode.window.showQuickPick(quickPickData);
if (!selectedLanguage) {
return;
}
vscode.workspace
.getConfiguration()
.update("vscodeGoogleTranslate.preferredLanguage", selectedLanguage.label, vscode.ConfigurationTarget.Global);
return selectedLanguage.label;
}
/**
* Returns user settings proxy config
*
* @returns {string}
*/
function getProxyConfig() {
const config = vscode.workspace.getConfiguration("vscodeGoogleTranslate");
return {
host: config.get("proxyHost"),
port: config.get("proxyPort"),
username: config.get("proxyUsername"),
password: config.get("proxyPassword"),
};
}
/**
* Platform binding function
*
* @param {vscode.ExtensionContext} context
* @returns {undefined} There is no an API public surface now (7/3/2019)
*/
async function activate(context) {
try {
const translateText = vscode.commands.registerCommand("extension.translateText", function () {
const editor = vscode.window.activeTextEditor;
const { document, selections } = editor;
const quickPickData = recentlyUsed
.map((r) => ({
label: r,
description: "(recently used)",
}))
.concat(languages.map((r) => ({ label: r.name })));
vscode.window
.showQuickPick(quickPickData)
.then((selectedLanguage) => {
if (!selectedLanguage) return;
updateLanguageList(selectedLanguage.label);
const translationsPromiseArray = getTranslationsPromiseArray(
selections,
document,
languages.find((r) => r.name === selectedLanguage.label).value
);
Promise.all(translationsPromiseArray)
.then(function (results) {
editor.edit((builder) => {
results.forEach((r) => {
if (!!r.translation) {
builder.replace(r.selection, he.decode(r.translation));
}
});
});
})
.catch((e) => vscode.window.showErrorMessage(e.message));
})
.catch((err) => {
vscode.window.showErrorMessage(err.message);
});
});
context.subscriptions.push(translateText);
const setPreferredLanguageFnc = vscode.commands.registerCommand("extension.setPreferredLanguage", setPreferredLanguage);
context.subscriptions.push(setPreferredLanguageFnc);
const translateTextPreferred = vscode.commands.registerCommand("extension.translateTextPreferred", async function () {
const editor = vscode.window.activeTextEditor;
const { document, selections } = editor;
// vscodeTranslate.preferredLanguage
const preferredLanguage = await getPreferredLanguage();
const locale = languages.find((element) => element.name === preferredLanguage).value;
if (!locale) {
return;
}
const translationsPromiseArray = getTranslationsPromiseArray(selections, document, locale);
Promise.all(translationsPromiseArray)
.then(function (results) {
editor.edit((builder) => {
results.forEach((r) => {
if (!!r.translation) {
builder.replace(r.selection, he.decode(r.translation));
}
});
});
})
.catch((e) => vscode.window.showErrorMessage(e.message));
});
context.subscriptions.push(translateTextPreferred);
const translateLinesUnderCursor = vscode.commands.registerCommand(
"extension.translateLinesUnderCursor",
function translateLinesUnderCursorcallback() {
const editor = vscode.window.activeTextEditor;
const { document, selections } = editor;
const quickPickData = recentlyUsed
.map((r) => ({
label: r.name,
description: "(recently used)",
}))
.concat(languages.map((r) => ({ label: r.name })));
vscode.window
.showQuickPick(quickPickData)
.then((selectedLanguage) => {
if (!selectedLanguage) return;
updateLanguageList(selectedLanguage.label);
const translationsPromiseArray = getTranslationsPromiseArrayLine(
selections,
document,
languages.find((r) => r.name === selectedLanguage.label).value
);
Promise.all(translationsPromiseArray)
.then(function (results) {
editor.edit((builder) => {
results.forEach((r) => {
if (!!r.translation) {
const ffix = ["", "\n"];
if (editor.document.lineCount - 1 === r.selection.start.line) [ffix[0], ffix[1]] = [ffix[1], ffix[0]];
const p = new vscode.Position(r.selection.start.line + 1);
builder.insert(p, `${ffix[0]}${r.translation}${ffix[1]}`);
}
});
});
})
.catch((e) => vscode.window.showErrorMessage(e.message));
})
.catch((err) => {
vscode.window.showErrorMessage(err.message);
});
}
);
context.subscriptions.push(translateLinesUnderCursor);
const translateLinesUnderCursorPreferred = vscode.commands.registerCommand(
"extension.translateLinesUnderCursorPreferred",
async function translateLinesUnderCursorPreferredcallback() {
const editor = vscode.window.activeTextEditor;
const { document, selections } = editor;
const preferredLanguage = await getPreferredLanguage();
const locale = languages.find((element) => element.name === preferredLanguage).value;
if (!locale) {
vscode.window.showWarningMessage("Prefered language is requeried for this feature! Please set this in the settings.");
return;
}
const translationsPromiseArray = getTranslationsPromiseArrayLine(selections, document, locale);
Promise.all(translationsPromiseArray)
.then(function (results) {
editor.edit((builder) => {
results.forEach((r) => {
if (!!r.translation) {
const ffix = ["", "\n"];
if (editor.document.lineCount - 1 === r.selection.start.line) [ffix[0], ffix[1]] = [ffix[1], ffix[0]];
const p = new vscode.Position(r.selection.start.line + 1);
builder.insert(p, `${ffix[0]}${r.translation}${ffix[1]}`);
}
});
});
})
.catch((e) => vscode.window.showErrorMessage(e.message));
}
);
context.subscriptions.push(translateLinesUnderCursorPreferred);
// Don't initialize the server if it's not wanted
if (vscode.workspace.getConfiguration("vscodeGoogleTranslate").get("HoverTranslations") == "false") {
return;
}
// All Below code initializes the Comment Hovering Translation feature
let serverModule = context.asAbsolutePath(path.join("server", "out", "server.js"));
// The debug options for the server
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
let debugOptions = { execArgv: ["--nolazy", "--inspect=16009"] };
// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
let serverOptions = {
run: {
module: serverModule,
transport: vscodeLanguageClient.TransportKind.ipc,
},
debug: {
module: serverModule,
transport: vscodeLanguageClient.TransportKind.ipc,
options: debugOptions,
},
};
let extAll = vscode.extensions.all;
let languageId = 2;
let grammarExtensions = [];
let canLanguages = [];
extAll.forEach((extension) => {
if (!(extension.packageJSON.contributes && extension.packageJSON.contributes.grammars)) return;
let languages = [];
((extension.packageJSON.contributes && extension.packageJSON.contributes.languages) || []).forEach((language) => {
languages.push({
id: languageId++,
name: language.id,
});
});
grammarExtensions.push({
languages: languages,
value: extension.packageJSON.contributes && extension.packageJSON.contributes.grammars,
extensionLocation: extension.extensionPath,
});
canLanguages = canLanguages.concat(extension.packageJSON.contributes.grammars.map((g) => g.language));
});
let BlackLanguage = ["log", "Log"];
let userLanguage = vscode.env.language;
// Options to control the language client
let clientOptions = {
// Register the server for plain text documents
revealOutputChannelOn: 4,
initializationOptions: {
grammarExtensions,
appRoot: vscode.env.appRoot,
userLanguage,
},
documentSelector: canLanguages.filter((v) => v).filter((v) => BlackLanguage.indexOf(v) < 0),
};
// Create the language client and start the client.
client = new vscodeLanguageClient.LanguageClient("CommentTranslate", "Comment Translate", serverOptions, clientOptions);
// Start the client. This will also launch the server
client.start();
await client.onReady();
client.onRequest("selectionContains", (textDocumentPosition) => {
let editor = vscode.window.activeTextEditor;
if (editor && editor.document.uri.toString() === textDocumentPosition.textDocument.uri) {
let position = new vscode.Position(textDocumentPosition.position.line, textDocumentPosition.position.character);
let selection = editor.selections.find((selection) => {
return !selection.isEmpty && selection.contains(position);
});
if (selection) {
return {
range: selection,
comment: editor.document.getText(selection),
};
}
}
return null;
});
vscode.window.setStatusBarMessage(`${pkg.name} extension is now active !`, 5000);
} catch (error) {
vscode.window.showInformationMessage(`${pkg.name} extension active failed ! error:`, err);
}
}
exports.activate = activate;
/**
* Platform binding function
* this method is called when your extension is deactivated
*
* @param {vscode.ExtensionContext} context
* @returns {undefined} There is no an API public surface now (7/3/2019)
*/
function deactivate() {
if (!client) {
return undefined;
}
return client.stop();
}
exports.deactivate = deactivate;