-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension.js
349 lines (301 loc) · 11.1 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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode')
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
const os = require('os')
const platform = os.platform()
let py
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const { exec, spawn } = require('child_process')
const http = require('http')
const port = 7395
let python_path = ''
switch (platform) {
case 'darwin':
python_path = vscode.workspace.getConfiguration('latex-sympy-calculator').get('mac')
break;
case 'linux':
python_path = vscode.workspace.getConfiguration('latex-sympy-calculator').get('linux')
break;
case 'win32':
python_path = vscode.workspace.getConfiguration('latex-sympy-calculator').get('windows')
break;
default:
vscode.window.showErrorMessage('Unknown operate system.')
return
}
// run auto update
exec(python_path + ' -m pip install --upgrade latex2sympy2', (err, stdout, stderr) => {
if (err) {
console.log(err)
}
if (stderr) {
console.log(stderr)
}
if (stdout) {
console.log(stdout)
}
})
// run server
py = spawn(python_path, [context.asAbsolutePath("server.py")])
py.on('error', (err) => {
console.log(err)
vscode.window.showErrorMessage('Running python failed... Please read the guide and make sure you have install "python", "latex2sympy2" and "Flask"')
})
py.on('exit', (code) => {
console.log(`Exit Code: ${code}`)
vscode.window.showErrorMessage('Running python failed... Please make sure you have "latex2sympy2 >= 1.8.0" and "Flask"')
vscode.window.showErrorMessage('You can update it by "pip install --upgrade latex2sympy2" and reboot your computer')
})
/**
* @param {string} data
* @param {string} path
* @param {function} onSuccess
* @param {function} onError
*/
function post(data, path, onSuccess, onError) {
const _data = JSON.stringify({ data: data })
const options = {
hostname: '127.0.0.1',
port: port,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': _data.length
}
}
const req = http.request(options, res => {
res.on('data', data => {
const result = JSON.parse(data)
if (result.error) {
onError(result.error)
} else {
onSuccess(result.data)
}
})
})
req.on('error', () => {
vscode.window.showInformationMessage('Activating the server...\nPlease retry for a moment.')
})
req.write(_data)
req.end()
}
/**
* @param {string} path
* @param {function} onSuccess
*/
function get(path, onSuccess) {
const options = {
hostname: '127.0.0.1',
port: port,
path: path,
method: 'GET'
}
const req = http.request(options, res => {
res.on('data', data => {
const result = JSON.parse(data)
onSuccess(result)
})
})
req.on('error', () => {
vscode.window.showInformationMessage('Activating the server...\r\nPlease try it again.')
})
req.end()
}
/**
* @param {string} latex
* @param {function} onSuccess
* @param {function} onError
*/
function sendLatex(latex, onSuccess, onError) {
post(latex, '/latex', onSuccess, onError)
}
function sendMatrixRawEchelonForm(latex, onSuccess, onError) {
post(latex, '/matrix-raw-echelon-form', onSuccess, onError)
}
function sendNumerical(latex, onSuccess, onError) {
post(latex, '/numerical', onSuccess, onError)
}
function sendFactor(latex, onSuccess, onError) {
post(latex, '/factor', onSuccess, onError)
}
function sendExpand(latex, onSuccess, onError) {
post(latex, '/expand', onSuccess, onError)
}
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.equal', function () {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
let doc = editor.document
let selection = editor.selection
let text = doc.getText(selection)
sendLatex(text, (data) => {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
editor.edit((edit) => {
edit.insert(selection.end, ' = ' + data)
})
}, (err) => {
vscode.window.showErrorMessage(err)
})
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.matrix-raw-echelon-form', function () {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
let doc = editor.document
let selection = editor.selection
let text = doc.getText(selection)
sendMatrixRawEchelonForm(text, (data) => {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
editor.edit((edit) => {
edit.insert(selection.end, ' \\to ' + data)
})
}, (err) => {
vscode.window.showErrorMessage(err)
})
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.numerical', function () {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
let doc = editor.document
let selection = editor.selection
let text = doc.getText(selection)
sendNumerical(text, (data) => {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
editor.edit((edit) => {
edit.replace(selection, data)
})
}, (err) => {
vscode.window.showErrorMessage(err)
})
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.factor', function () {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
let doc = editor.document
let selection = editor.selection
let text = doc.getText(selection)
sendFactor(text, (data) => {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
editor.edit((edit) => {
edit.replace(selection, data)
})
}, (err) => {
vscode.window.showErrorMessage(err)
})
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.expand', function () {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
let doc = editor.document
let selection = editor.selection
let text = doc.getText(selection)
sendExpand(text, (data) => {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
editor.edit((edit) => {
edit.replace(selection, data)
})
}, (err) => {
vscode.window.showErrorMessage(err)
})
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.replace', function () {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
let doc = editor.document
let selection = editor.selection
let text = doc.getText(selection)
sendLatex(text, (data) => {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
editor.edit((edit) => {
edit.replace(selection, data)
})
}, (err) => {
vscode.window.showErrorMessage(err)
})
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.define', function () {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
let doc = editor.document
let selection = editor.selection
let text = doc.getText(selection)
sendLatex(text, () => {
vscode.window.showInformationMessage('Define: ' + text)
}, (err) => {
vscode.window.showErrorMessage(err)
})
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.variances', function () {
get('/variances', (data) => {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
editor.edit((edit) => {
const result = '\r\n' + Object.keys(data).map((key) => key + ' = ' + data[key]).join('\r\n')
edit.insert(editor.selection.end, result)
})
})
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.reset', function () {
get('/reset', () => { vscode.window.showInformationMessage('Success to reset the current variances') })
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.toggle-complex-number', function () {
get('/complex', (data) => { vscode.window.showInformationMessage('Toggle Complex Number to ' + (data.value ? 'Off' : 'On')) })
})
)
context.subscriptions.push(
vscode.commands.registerCommand('latex-sympy-calculator.python', function () {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
let doc = editor.document
let selection = editor.selection
let text = doc.getText(selection)
post(text, '/python', (data) => {
let editor = vscode.window.activeTextEditor
if (!editor) { return }
editor.edit((edit) => {
edit.insert(selection.end, ' = ' + data)
})
}, (err) => {
vscode.window.showErrorMessage(err)
})
})
)
}
exports.activate = activate
// this method is called when your extension is deactivated
function deactivate() {
py.kill()
}
module.exports = {
activate,
deactivate
}