Skip to content

Commit

Permalink
feat: support for compiling to python
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 19, 2019
1 parent b147ff6 commit 7a0bac4
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -44,6 +44,13 @@ Punctuations will be automatically replaced to fit the language.

*You will get the result immediately, the delay is added for demostraction purpose*

### Configurations

| Fields | Default | Note |
| --- | --- | --- |
| `wenyan-lang.executablePath` | built-in | Filepath to executable `wenyan.js` |
| `wenyan-lang.targetLanguage` | `javascript` | Target language that compiles to |


## ToDo

Expand All @@ -52,10 +59,10 @@ Punctuations will be automatically replaced to fit the language.
- [x] Dynamic Snippets
- [x] Execute
- [x] Compile
- [ ] Compile to Python
- [x] Compile to Python
- [x] Rendering
- [ ] Code Completion
- [ ] Language Server
- [ ] Rendering

## License

Expand Down
9 changes: 9 additions & 0 deletions package.json
Expand Up @@ -139,6 +139,15 @@
"wenyan-lang.executablePath": {
"type": "string",
"description": "%config.executable_path%"
},
"wenyan-lang.targetLanguage": {
"type": "string",
"enum": [
"javascript",
"python"
],
"default": "javascript",
"description": "%config.target_language%"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Expand Up @@ -4,6 +4,7 @@
"command.reload": "Reload Result",
"command.render": "Render Document",
"config.executable_path": "Filepath to executable `wenyan.js`",
"config.target_language": "Target language that compiles to",
"extname": "文言 Wenyan",
"prompt.enter_the_title_for_rendering": "Enter the title for rendering",
"prompt.open_in_viewer": "Open in Viewer",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh-cn.json
Expand Up @@ -4,6 +4,7 @@
"command.reload": "重新加载",
"command.render": "渲染文件",
"config.executable_path": "编译器 wenyan.js 的文件路径",
"config.target_language": "编译目标语言",
"extname": "文言 Wenyan",
"prompt.enter_the_title_for_rendering": "输入渲染用的文章标题",
"prompt.open_in_viewer": "用默认程序查看",
Expand Down
7 changes: 4 additions & 3 deletions src/commands/index.ts
@@ -1,6 +1,7 @@
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'
Expand All @@ -22,7 +23,7 @@ const m: ExtensionModule = () => {
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)}`)
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 })
}
Expand All @@ -31,8 +32,8 @@ const m: ExtensionModule = () => {
const document = window.activeTextEditor?.document
if (document?.languageId === LANG_ID) {
const { name } = path.parse(document.uri.fsPath)
const filename = `${name}(Compiled).js`
const uri = Uri.parse(`${DOC_SCHEMA}:${filename}?action=compile&filepath=${encodeURIComponent(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 })
}
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Expand Up @@ -3,11 +3,20 @@ import { workspace } from 'vscode'
import { EXT_NAMESPACE } from './meta'
import { getCTX } from './ctx'

export type SupportTargetLanguage = 'py' | 'js'

export class Config {
static get executablePath () {
return this.getConfig<string>('executablePath') || path.join(getCTX().extensionPath, 'vendor', 'wenyan.js')
}

static get targetLanguage (): SupportTargetLanguage {
if (this.getConfig<string>('targetLanguage') === 'python')
return 'py'
else
return 'js'
}

private static getConfig<T = any> (key: string): T | undefined {
const config = workspace
.getConfiguration(EXT_NAMESPACE)
Expand Down
19 changes: 12 additions & 7 deletions src/editor/documentProvider.ts
Expand Up @@ -3,17 +3,21 @@ import prettier from 'prettier/standalone'
import parserBabel from 'prettier/parser-babylon'
import { parseQuery } from '../utils'
import { DOC_SCHEMA } from '../meta'
import { SupportTargetLanguage } from '../config'
import { ExtensionModule } from '../module'
import { Exec } from '../exec'

async function getCompiledResult (filepath: string) {
const result = await Exec(filepath) || ''
async function getCompiledResult (filepath: string, target: SupportTargetLanguage) {
const result = await Exec(filepath, { lang: target }) || ''

return prettier.format(result, { semi: false, parser: 'babel', plugins: [parserBabel] })
if (target === 'py')
return result
else
return prettier.format(result, { semi: false, parser: 'babel', plugins: [parserBabel] })
}

async function getExecResult (filepath: string) {
const result = await Exec(filepath, { exec: true }) || ''
async function getExecResult (filepath: string, target: SupportTargetLanguage) {
const result = await Exec(filepath, { exec: true, lang: target }) || ''

// remove first line or compiled code
return result.split('\n').slice(1).join('\n')
Expand All @@ -27,14 +31,15 @@ class DocumentProvider implements TextDocumentContentProvider {
const query = parseQuery(uri.query)
const filepath = query.filepath
const action = query.action
const target = query.target as SupportTargetLanguage

if (!filepath || !action)
return ''

if (action === 'execute')
return await getExecResult(filepath)
return await getExecResult(filepath, target)
if (action === 'compile')
return await getCompiledResult(filepath)
return await getCompiledResult(filepath, target)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/exec.ts
@@ -1,10 +1,10 @@
import cp from 'child_process'
import { Log } from './log'
import { Config } from './config'
import { Config, SupportTargetLanguage } from './config'

export interface ExecuteOptions {
exec?: boolean
lang?: 'js' | 'py'
lang?: SupportTargetLanguage
roman?: string
render?: string
output?: string
Expand Down

0 comments on commit 7a0bac4

Please sign in to comment.