✨ feat(vscode): 代码阅读关键字按语境翻译
This commit is contained in:
1 parent
72d33032f0
commit
d134837c17
5 files changed
+155
-19
No files matched your search
@@ -5,6 +5,7 @@ import { createTypeScriptAdapter } from '../languages/typescript'
|
||||
import { readFixture, walk, WASM_DIR, withBlocks } from '../testSupport'
|
||||
import { pythonZhTranslator } from './pythonZh'
|
||||
import { typescriptZhTranslator } from './typescriptZh'
|
||||
import { createZhTemplateTranslator } from './zhTemplate'
|
||||
|
||||
const ts = createTypeScriptAdapter(WASM_DIR)
|
||||
const py = createPythonAdapter(WASM_DIR)
|
||||
@@ -124,6 +125,39 @@ describe('typeScript 中文直译', () => {
|
||||
].join('\n')
|
||||
expect(texts(await translateAll(ts, 'typescript', typescriptZhTranslator, source))[1]).toBe('call( first, 空数组 )')
|
||||
})
|
||||
|
||||
it('as / in / extends / default 按所在语法节点换译法', async () => {
|
||||
const source = [
|
||||
'import * as path from \'node:path\'',
|
||||
'export { run as start } from \'./run\'',
|
||||
'const v = x as string',
|
||||
'const k = [\'a\'] as const',
|
||||
'for (const key in obj) {}',
|
||||
'const has = \'k\' in obj',
|
||||
'class A extends B implements C {}',
|
||||
'interface I extends J, K {}',
|
||||
'function f<T extends X>(t: T) {}',
|
||||
'type R<T> = T extends string ? 1 : 2',
|
||||
'export default function g() {}',
|
||||
'switch (x) {',
|
||||
' default:',
|
||||
' break',
|
||||
'}',
|
||||
].join('\n')
|
||||
const map = texts(await translateAll(ts, 'typescript', typescriptZhTranslator, source))
|
||||
expect(map[1]).toBe('导入 * 作为 path 来自 \'node:path\'')
|
||||
expect(map[2]).toBe('公共 { run 作为 start } 来自 \'./run\'')
|
||||
expect(map[3]).toBe('常量 v = x 断言为 字符串')
|
||||
expect(map[4]).toBe('常量 k = [\'a\'] 断言为 常量')
|
||||
expect(map[5]).toBe('遍历 (常量 key 取自 obj)')
|
||||
expect(map[6]).toBe('常量 has = \'k\' 在 obj')
|
||||
expect(map[7]).toBe('类 A 继承 B 实现 C')
|
||||
expect(map[8]).toBe('接口 I 继承 J, K')
|
||||
expect(map[9]).toBe('函数 f<T 限定为 X>(t: T)')
|
||||
expect(map[10]).toBe('类型 R<T> = T 符合 字符串 ? 1 : 2')
|
||||
expect(map[11]).toBe('公共 默认 函数 g()')
|
||||
expect(map[13]).toBe('默认情况')
|
||||
})
|
||||
})
|
||||
|
||||
describe('python 中文直译', () => {
|
||||
@@ -152,7 +186,7 @@ describe('python 中文直译', () => {
|
||||
24: '使用 open(os.path.join(root, found[0])) 作为 handle',
|
||||
25: 'handle.read()',
|
||||
26: '捕获 OSError 作为 err',
|
||||
27: '抛出 RuntimeError(str(err)) 从 err',
|
||||
27: '抛出 RuntimeError(str(err)) 源自 err',
|
||||
28: '最终',
|
||||
29: 'skipped.clear()',
|
||||
30: '返回 found',
|
||||
@@ -177,14 +211,81 @@ describe('python 中文直译', () => {
|
||||
expect(map[2]).toBe('返回 [str(k) 遍历 k 取自 items 如果 k 不在 list]')
|
||||
})
|
||||
|
||||
it('from 在句首译成「从」,yield from 同一个词', async () => {
|
||||
it('from:导入里「从」,raise … from 里「源自」,yield from 里「自」', async () => {
|
||||
const source = [
|
||||
'from typing import Annotated',
|
||||
'def walk(nodes):',
|
||||
' yield from nodes',
|
||||
' raise RuntimeError(str(err)) from err',
|
||||
].join('\n')
|
||||
const map = texts(await translateAll(py, 'python', pythonZhTranslator, source))
|
||||
expect(map[1]).toBe('从 typing 导入 Annotated')
|
||||
expect(map[3]).toBe('产出 从 nodes')
|
||||
expect(map[3]).toBe('产出 自 nodes')
|
||||
expect(map[4]).toBe('抛出 RuntimeError(str(err)) 源自 err')
|
||||
})
|
||||
|
||||
it('else:if 后「否则」,for / while 后「未中断时」,try 后「未出错时」', async () => {
|
||||
const source = [
|
||||
'for k in items:',
|
||||
' if k:',
|
||||
' break',
|
||||
' else:',
|
||||
' pass',
|
||||
'else:',
|
||||
' pass',
|
||||
'while busy:',
|
||||
' pass',
|
||||
'else:',
|
||||
' pass',
|
||||
'try:',
|
||||
' pass',
|
||||
'except OSError:',
|
||||
' pass',
|
||||
'else:',
|
||||
' pass',
|
||||
].join('\n')
|
||||
const map = texts(await translateAll(py, 'python', pythonZhTranslator, source))
|
||||
expect(map[4]).toBe('否则')
|
||||
expect(map[6]).toBe('未中断时')
|
||||
expect(map[10]).toBe('未中断时')
|
||||
expect(map[16]).toBe('未出错时')
|
||||
})
|
||||
})
|
||||
|
||||
describe('中文模板引擎', () => {
|
||||
it('关键字按父节点类型选译法,更长的节点类型链优先,都不匹配用默认', async () => {
|
||||
const translator = createZhTemplateTranslator({
|
||||
id: 'test.zh-template',
|
||||
label: '测试',
|
||||
languageIds: ['python'],
|
||||
tokenRole: () => 'keyword',
|
||||
keywords: {
|
||||
in: { zh: '甲', byContext: { for_statement: '乙' } },
|
||||
else: { zh: '丙', byContext: { 'else_clause': '丁', 'for_statement > else_clause': '戊' } },
|
||||
},
|
||||
literals: {},
|
||||
builtinTypes: {},
|
||||
isTypePosition: () => false,
|
||||
operators: {},
|
||||
operatorParents: [],
|
||||
emptyLiterals: {},
|
||||
stringNodes: [],
|
||||
commentText: node => node.text,
|
||||
})
|
||||
const source = [
|
||||
'for k in items:',
|
||||
' if k in seen:',
|
||||
' pass',
|
||||
' else:',
|
||||
' x = a if c else b',
|
||||
'else:',
|
||||
' pass',
|
||||
].join('\n')
|
||||
const map = texts(await translateAll(py, 'python', translator, source))
|
||||
expect(map[1]).toBe('for k 乙 items')
|
||||
expect(map[2]).toBe('if k 甲 seen')
|
||||
expect(map[4]).toBe('丁')
|
||||
expect(map[5]).toBe('x = a if c 丙 b')
|
||||
expect(map[6]).toBe('戊')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user