⚡ perf(vscode): 代码阅读预览的文档消息里源码和着色只带一份:块不再各带一份连子块在内的整段源码和上色,编辑框按块的编辑范围从整份源码和着色里取,5126 行的消息 4.3MB → 1.9MB

This commit is contained in:
cruldra committed 2026-10-13 15:50:33 +11:00
1 parent 0371712176
commit 2e3602242e
43 files changed
+501 -239

No files matched your search

@@ -6,7 +6,7 @@ import { packageCommandRules } from '../commands/packageCommands'
import { shellCommandRules } from '../commands/shellCommands'
import { systemCommandRules } from '../commands/systemCommands'
import { createBashAdapter } from '../languages/bash'
import { flatSegments, readFixture, runtime, viewLines, WASM_DIR } from '../testSupport'
import { flatSegments, lineColors, readFixture, runtime, viewLines, WASM_DIR } from '../testSupport'
import { bashZhTranslator } from './bashZh'
const bash = createBashAdapter(WASM_DIR)
@@ -212,7 +212,8 @@ describe('shell 中文直译', () => {
})
it('着色:字符串里写死的部分是字符串色,展开的变量是变量色,全大写的名字是常量色', async () => {
const [block] = await read(`PY_URL="https://x/$PY_RELEASE/c-\${PY_VERSION}-$target.tgz"\n`)
const source = `PY_URL="https://x/$PY_RELEASE/c-\${PY_VERSION}-$target.tgz"\n`
const { blocks: [block], colors } = await readBlockViews(runtime, bash, bashZhTranslator, 'shellscript', source)
const value = 'properties' in block ? block.properties.find(row => row.label === '初始值')?.value : undefined
expect(value && !('markdown' in value) && flatSegments(value).map(({ text, role }) => [text, role])).toEqual([
['https://x/', 'string'],
@@ -223,7 +224,7 @@ describe('shell 中文直译', () => {
['target', 'variable'],
['.tgz', 'string'],
])
expect(block.codeTokens.filter(({ role }) => role !== 'plain').map(({ text, role }) => [text, role])).toContainEqual(['PY_URL', 'constant'])
expect(lineColors(source, colors, block.startLine, block.endLine).filter(({ role }) => role !== 'plain').map(({ text, role }) => [text, role])).toContainEqual(['PY_URL', 'constant'])
})
it('命令规则出错时照抄命令并报告,别的命令不受影响', async () => {
@@ -1,4 +1,4 @@
import type { BlockView } from '../blockView'
import type { BlockView, DocumentViews } from '../blockView'
import { describe, expect, it } from 'vitest'
import { readBlockViews } from '../blockView'
import { commandRuleLookup } from '../commandRules'
@@ -7,7 +7,7 @@ import { shellCommandRules } from '../commands/shellCommands'
import { systemCommandRules } from '../commands/systemCommands'
import { createBashAdapter } from '../languages/bash'
import { createDockerfileAdapter } from '../languages/dockerfile'
import { DOCKERFILE_WASM_DIR, flatSegments, readFixture, runtime, viewLines, WASM_DIR } from '../testSupport'
import { DOCKERFILE_WASM_DIR, flatSegments, lineColors, readFixture, runtime, viewLines, WASM_DIR } from '../testSupport'
import { bashZhTranslator } from './bashZh'
import { dockerfileZhTranslator } from './dockerfileZh'
@@ -16,13 +16,16 @@ const docker = createDockerfileAdapter(DOCKERFILE_WASM_DIR)
const RULES = [...shellCommandRules, ...systemCommandRules, ...packageCommandRules]
/** RUN 里的 shell 交给 bash 的语言插件和中文直译(withShell 为假时不给,照抄)。 */
async function read(source: string, withShell = true): Promise<BlockView[]> {
function readViews(source: string, withShell = true): Promise<DocumentViews> {
const commandRule = commandRuleLookup(RULES, () => {})
const { blocks } = await readBlockViews(runtime, docker, dockerfileZhTranslator, 'dockerfile', source, {
return readBlockViews(runtime, docker, dockerfileZhTranslator, 'dockerfile', source, {
commandRule,
...(withShell && { embedded: (id: string) => id === 'shellscript' ? { adapter: bash, translator: bashZhTranslator, commandRule } : undefined }),
})
return blocks
}
async function read(source: string, withShell = true): Promise<BlockView[]> {
return (await readViews(source, withShell)).blocks
}
async function readLines(source: string): Promise<string[]> {
@@ -313,11 +316,11 @@ describe('dockerfile 中文直译', () => {
it('能跳到定义的名字:从阶段复制的阶段名、表里的变量名带源码下标;RUN 的源码按 shell 着色', async () => {
const source = 'FROM node AS deps\nFROM node\nCOPY --from=deps /a /a\nENV A=1\nENV B=2\nRUN apt-get update\n'
const [, stage] = await read(source)
const { blocks: [, stage], colors } = await readViews(source)
const [copy, env, run] = stage.children
const deps = 'translation' in copy && Array.isArray(copy.translation) ? flatSegments(copy.translation).find(segment => segment.text === 'deps') : undefined
expect(deps?.offset).toBe(source.indexOf('deps', 20))
expect('properties' in env && env.properties[0].items?.map(item => item.defines && source.slice(item.defines.startIndex, item.defines.endIndex))).toEqual(['A', 'B'])
expect(run.codeTokens.filter(token => token.role !== 'plain').map(token => [token.text, token.role])).toEqual([['RUN', 'keyword'], ['apt-get', 'function']])
expect(lineColors(source, colors, run.startLine, run.endLine).filter(token => token.role !== 'plain').map(token => [token.text, token.role])).toEqual([['RUN', 'keyword'], ['apt-get', 'function']])
})
})