♻️ refactor(vscode): 代码阅读的翻译插件只经契约拿语言插件的词性和推导式结构

This commit is contained in:
cruldra committed 2026-09-26 16:24:36 +08:00
1 parent 07eaf96a6b
commit 202fa954ba
12 files changed
+185 -76

No files matched your search

@@ -1,4 +1,4 @@
import type { Inline, LanguageAdapter, PropertyItem, PropertyRow, Segment, Translator } from '../contract'
import type { Inline, LanguageAdapter, PropertyItem, PropertyRow, Segment, TokenRole, Translator } from '../contract'
import type { ZhTable } from './zhTemplate'
import { describe, expect, it } from 'vitest'
import { createPythonAdapter } from '../languages/python'
@@ -22,7 +22,7 @@ function translateAll(adapter: LanguageAdapter, languageId: string, translator:
const line = block.header.startRow + 1
if (byLine.has(line) || 'facts' in block)
return
const translation = translator.translate(block, source)
const translation = translator.translate(block, { source, language: adapter })
byLine.set(line, 'markdown' in translation
? { kind: block.kind, segments: [], markdown: translation.markdown }
: { kind: block.kind, segments: translation })
@@ -38,7 +38,7 @@ function describeAll(adapter: LanguageAdapter, languageId: string, translator: T
walk(blocks, (block) => {
const line = block.header.startRow + 1
if (!byLine.has(line) && 'facts' in block)
byLine.set(line, translator.describe(block, source))
byLine.set(line, translator.describe(block, { source, language: adapter }))
})
return byLine
})
@@ -1034,7 +1034,6 @@ describe('中文模板引擎', () => {
id: 'test.zh-template',
label: '测试',
languageIds: ['python'],
tokenRole: () => 'keyword',
keywords: {},
literals: {},
builtinTypes: {},
@@ -1050,6 +1049,11 @@ describe('中文模板引擎', () => {
})
}
/** 按 Python 分块、词性都是 role 的语言插件:引擎着色只看翻译时传进来的语言插件。 */
function pythonColored(role: TokenRole): LanguageAdapter {
return { ...py, tokenRole: () => role }
}
it('关键字按父节点类型选译法,更长的节点类型链优先,都不匹配用默认', async () => {
const translator = testTranslator({
keywords: {
@@ -1066,7 +1070,7 @@ describe('中文模板引擎', () => {
'else:',
' pass',
].join('\n')
const map = texts(await translateAll(py, 'python', translator, source))
const map = texts(await translateAll(pythonColored('keyword'), 'python', translator, source))
expect(map[1]).toBe('for k 乙 items')
expect(map[2]).toBe('if k 甲 seen')
expect(map[4]).toBe('丁')
@@ -1076,7 +1080,6 @@ describe('中文模板引擎', () => {
it('节点模板按节点类型重排:槽位按字段名或具名子节点序号取子节点并递归翻译,译词按关键字着色', async () => {
const translator = testTranslator({
tokenRole: () => 'variable',
keywords: { in: '在', else: { zh: '丙', byContext: { else_clause: '丁' } } },
operators: { '==': '等于' },
operatorParents: ['comparison_operator'],
@@ -1091,7 +1094,7 @@ describe('中文模板引擎', () => {
'print((a # 注释不占序号',
' if k == 1 else b))',
].join('\n')
const map = await translateAll(py, 'python', translator, source)
const map = await translateAll(pythonColored('variable'), 'python', translator, source)
expect(texts(map)).toEqual({
1: '令 n 加上 若 k 在 seen 则 a 不然 b',
2: 'print(若 c 则 a 不然 若 d 则 b 不然 e)',
@@ -1103,13 +1106,12 @@ describe('中文模板引擎', () => {
it('属性表里的类型、值用同一张表和同一套模板翻', async () => {
const translator = testTranslator({
tokenRole: () => 'variable',
keywords: { in: '在' },
builtinTypes: { int: '整型' },
isTypePosition: leaf => leaf.parent?.type === 'type',
nodeTemplates: { conditional_expression: ['若', { namedChild: 1 }, '则', { namedChild: 0 }, '不然', { namedChild: 2 }] },
})
const rows = await describeAll(py, 'python', translator, 'n: int = a if k in seen else b')
const rows = await describeAll(pythonColored('variable'), 'python', translator, 'n: int = a if k in seen else b')
expect(rowTexts(rows)[1]).toEqual(['名称: n', '访问级别: 公共', '类型: 整型', '初始值: 若 k 在 seen 则 a 不然 b'])
})
@@ -1127,7 +1129,7 @@ describe('中文模板引擎', () => {
'else:',
' pass',
].join('\n')
expect(texts(await translateAll(py, 'python', translator, source))).toEqual({
expect(texts(await translateAll(pythonColored('keyword'), 'python', translator, source))).toEqual({
1: 'x += a if k 在 seen 丙 b',
2: 'if k 在 seen',
3: 'pass',