✨ feat(vscode): 代码阅读的解析与模板直译核心
This commit is contained in:
1 parent
af56985d9a
commit
4d34b8c715
24 files changed
+2294
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import type { Node, Translator } from '../contract'
|
||||
import { insideAnnotation, pythonTokenRole } from '../languages/python'
|
||||
import { createZhTemplateTranslator } from './zhTemplate'
|
||||
|
||||
function stringContent(node: Node): string {
|
||||
if (node.type === 'string_content')
|
||||
return node.text
|
||||
return node.children.map(child => child ? stringContent(child) : '').join('')
|
||||
}
|
||||
|
||||
/** `#` 注释去掉井号;docstring 只取引号里的内容。 */
|
||||
function commentText(node: Node): string {
|
||||
return node.type === 'comment' ? node.text.replace(/^#+\s?/, '') : stringContent(node)
|
||||
}
|
||||
|
||||
export const pythonZhTranslator: Translator = createZhTemplateTranslator({
|
||||
id: 'python.zh-template',
|
||||
label: '中文直译',
|
||||
languageIds: ['python'],
|
||||
tokenRole: pythonTokenRole,
|
||||
keywords: {
|
||||
'import': '导入',
|
||||
'from': '来自',
|
||||
'as': '作为',
|
||||
'def': '函数',
|
||||
'async': '异步',
|
||||
'await': '等待',
|
||||
'class': '类',
|
||||
'return': '返回',
|
||||
'if': '如果',
|
||||
'elif': '否则如果',
|
||||
'else': '否则',
|
||||
'for': '遍历',
|
||||
'in': { zh: '在', byParent: { for_statement: '取自', for_in_clause: '取自' } },
|
||||
'while': '当',
|
||||
'try': '尝试',
|
||||
'except': '捕获',
|
||||
'finally': '最终',
|
||||
'raise': '抛出',
|
||||
'with': '使用',
|
||||
'yield': '产出',
|
||||
'lambda': '匿名函数',
|
||||
'pass': '跳过',
|
||||
'break': '跳出',
|
||||
'continue': '继续',
|
||||
'global': '全局',
|
||||
'nonlocal': '非局部',
|
||||
'del': '删除',
|
||||
'assert': '断言',
|
||||
'not': '非',
|
||||
'and': '与',
|
||||
'or': '或',
|
||||
'is': '是',
|
||||
'not in': '不在',
|
||||
'is not': '不是',
|
||||
'match': '匹配',
|
||||
'case': '情况',
|
||||
},
|
||||
literals: { none: '空', true: '真', false: '假' },
|
||||
builtinTypes: {
|
||||
str: '字符串',
|
||||
int: '整数',
|
||||
float: '浮点数',
|
||||
bool: '布尔',
|
||||
list: '列表',
|
||||
dict: '字典',
|
||||
set: '集合',
|
||||
tuple: '元组',
|
||||
bytes: '字节串',
|
||||
},
|
||||
isTypePosition: leaf => leaf.type === 'identifier' && leaf.parent?.type !== 'attribute' && insideAnnotation(leaf),
|
||||
operators: { '==': '等于', '!=': '不等于' },
|
||||
operatorParents: ['comparison_operator'],
|
||||
emptyLiterals: { list: '空列表', dictionary: '空字典' },
|
||||
stringNodes: ['string'],
|
||||
commentText,
|
||||
})
|
||||
Reference in new issue
Block a user