🐛 fix(vscode): 代码阅读把 Python 赋值后面的字符串当变量说明,其余单独的字符串显示成注释

This commit is contained in:
cruldra committed 2026-09-26 18:57:07 +08:00
1 parent a51f90e34d
commit 1f86201373
14 files changed
+358 -59

No files matched your search

+47
View File
@@ -2,6 +2,7 @@ import type { BlockView } from './blockView'
import type { LanguageAdapter, Translator } from './contract'
import { describe, expect, it } from 'vitest'
import { readBlockViews, toBlockViews } from './blockView'
import { replaceLines } from './editText'
import { createPythonAdapter } from './languages/python'
import { createTypeScriptAdapter } from './languages/typescript'
import { previewText, propertyLines, readFixture, runtime, WASM_DIR, withBlocks } from './testSupport'
@@ -33,6 +34,7 @@ const SAMPLES: [LanguageAdapter, string, Translator, string][] = [
[py, 'python', pythonZhTranslator, 'sample.py'],
[py, 'python', pythonZhTranslator, 'routes.py'],
[py, 'python', pythonZhTranslator, 'classes.py'],
[py, 'python', pythonZhTranslator, 'attributeDocs.py'],
]
const PROPERTY_KINDS = new Set(['function', 'class', 'import', 'const', 'variable'])
@@ -122,6 +124,51 @@ describe('blockView', () => {
expect(notFound.children[0]).toMatchObject({ kind: 'variable', startLine: 20, endLine: 20 })
})
it('属性 docstring 算在常量、变量、字段块里:常量、变量的「说明」紧跟名称,字段的说明排在译文下面;编辑范围连着它', async () => {
const source = [
'CASE_DEPLOYMENT_UNIQUE_CONSTRAINT = "uq_case_library_deployment"',
'"""一 deployment 一案例的唯一约束名; 入库侧按它认"重复入库", 故与建表同源。"""',
'',
'',
'class CaseStatus:',
' ANALYZING = "analyzing"',
'',
' """正在分析源 deployment."""',
' title: str = ""',
' """案例标题,',
' 用户可编辑."""',
].join('\n')
const list = await views(py, 'python', pythonZhTranslator, source)
const snapshot = flatten(list).map(view => ({
id: view.id,
kind: view.kind,
lines: [view.startLine, view.endLine],
...('properties' in view ? { rows: propertyLines(view.properties) } : { translation: previewText(view.translation), doc: view.doc }),
}))
expect(snapshot).toEqual([
{
id: '0',
kind: 'const',
lines: [0, 1],
rows: [
'名称: CASE_DEPLOYMENT_UNIQUE_CONSTRAINT',
'说明: 一 deployment 一案例的唯一约束名; 入库侧按它认"重复入库", 故与建表同源。',
'访问级别: 公共',
'值: "uq_case_library_deployment"',
],
},
{ id: '1', kind: 'class', lines: [4, 4], rows: ['名称: CaseStatus', '访问级别: 公共'] },
{ id: '1.0', kind: 'variable', lines: [5, 7], rows: ['名称: ANALYZING', '说明: 正在分析源 deployment.', '访问级别: 公共', '初始值: "analyzing"'] },
{ id: '1.1', kind: 'field', lines: [8, 10], translation: 'title: 字符串 = ""', doc: { markdown: '案例标题,\n用户可编辑.' } },
])
expect(byId(list, '0').code).toBe(source.split('\n').slice(0, 2).join('\n'))
expect(byId(list, '1.0').code).toBe('ANALYZING = "analyzing"\n\n"""正在分析源 deployment."""')
expect(byId(list, '1.1').code).toBe('title: str = ""\n"""案例标题,\n用户可编辑."""')
for (const view of flatten(list))
expect(replaceLines(source, view.startLine, view.endLine, view.code), view.id).toBe(source)
expect(replaceLines(source, 8, 10, 'title: str = "x"\n"""新说明"""').split('\n').slice(8)).toEqual([' title: str = "x"', ' """新说明"""'])
})
it('注释块的译文、函数的「说明」是 Markdown 原文,webview 按它渲染', async () => {
const env = await views(ts, 'typescript', typescriptZhTranslator, readFixture('envLock.ts.txt'))
expect(byId(env, '0')).toMatchObject({ kind: 'comment', translation: { markdown: expect.stringMatching(/^\.env file lock \/ unlock helpers\.\n\nRecursively/) } })