/
index.ts
84 lines (76 loc) · 3.41 KB
/
index.ts
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
import path from 'path'
import { exec } from 'child_process'
import { commands, window, Uri, workspace, ViewColumn } from 'vscode'
import { Config } from '../config'
import { Exec } from '../exec'
import { CommandKeys, LANG_ID, DOC_SCHEMA } from '../meta'
import { ExtensionModule } from '../module'
import { documentProvider } from '../editor/documentProvider'
import i18n from '../i18n'
function getCommandLine () {
switch (process.platform) {
case 'darwin' : return 'open'
case 'win32' : return 'start'
default : return 'xdg-open'
}
}
const m: ExtensionModule = () => {
return [
commands.registerCommand(CommandKeys.execute, async () => {
const document = window.activeTextEditor?.document
if (document?.languageId === LANG_ID) {
const { name } = path.parse(document.uri.fsPath)
const filename = `${name}(Output)`
const uri = Uri.parse(`${DOC_SCHEMA}:${filename}?action=execute&filepath=${encodeURIComponent(document.uri.fsPath)}&target=${Config.targetLanguage}`)
documentProvider.onDidChangeEmitter.fire(uri)
window.showTextDocument(await workspace.openTextDocument(uri), { preview: false, viewColumn: ViewColumn.Beside })
}
}),
commands.registerCommand(CommandKeys.compile, async () => {
const document = window.activeTextEditor?.document
if (document?.languageId === LANG_ID) {
const { name } = path.parse(document.uri.fsPath)
const filename = `${name}(Compiled).${Config.targetLanguage}`
const uri = Uri.parse(`${DOC_SCHEMA}:${filename}?action=compile&filepath=${encodeURIComponent(document.uri.fsPath)}&target=${Config.targetLanguage}`)
documentProvider.onDidChangeEmitter.fire(uri)
window.showTextDocument(await workspace.openTextDocument(uri), { preview: false, viewColumn: ViewColumn.Beside })
}
}),
commands.registerCommand(CommandKeys.reload, async () => {
const document = window.activeTextEditor?.document
if (document?.uri.scheme === DOC_SCHEMA)
documentProvider.onDidChangeEmitter.fire(document.uri)
}),
commands.registerCommand(CommandKeys.render, async () => {
const document = window.activeTextEditor?.document
if (document?.languageId !== LANG_ID)
return
const defaultUri = Uri.file(document.uri.fsPath.replace(/\.wy$/, '.svg'))
const uri = await window.showSaveDialog({
defaultUri,
filters: { SVG: ['svg'] },
})
if (!uri)
return
const { name: defaultTitle } = path.parse(uri.fsPath)
const title = await window.showInputBox({
prompt: i18n.t('prompt.enter_the_title_for_rendering'), // TODO: i18n
value: defaultTitle,
})
if (!title)
return
const output = await Exec(document.uri.fsPath, { render: title, output: uri.fsPath }) || ''
const filenames = output.split('\n').map(i => i.trim()).filter(i => i)
if (filenames.length) {
const openInEditor = i18n.t('prompt.open_in_vscode')
const openInImageViewer = i18n.t('prompt.open_in_viewer')
const result = await window.showInformationMessage(i18n.t('prompt.successfully_rendered'), openInEditor, openInImageViewer)
if (result === openInEditor)
window.showTextDocument(await workspace.openTextDocument(Uri.file(filenames[0])))
if (result === openInImageViewer)
exec(`${getCommandLine()} ${filenames[0]}`)
}
}),
]
}
export default m