语言插件按和语言无关的 id 报块属性(通用的由宿主从 facts 推),翻译插件起名,webview 只按 id 筛; 筛选整个工作区共用、记在 workspaceState;JavaScript 不再显示「抽象:否」「仅类型:否」
59 lines
2.7 KiB
TypeScript
59 lines
2.7 KiB
TypeScript
import type { Memento } from 'vscode'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { createTypeScriptAdapter } from '../languages/typescript'
|
|
import { CodeReaderRegistry } from '../registry'
|
|
import { runtime, WASM_DIR } from '../testSupport'
|
|
import { typescriptZhTranslator } from '../translators/typescriptZh'
|
|
import { documentMessage } from './documentMessage'
|
|
|
|
function registryWithTypeScript(): CodeReaderRegistry {
|
|
const store: Record<string, unknown> = {}
|
|
const registry = new CodeReaderRegistry({
|
|
get: <T>(key: string): T | undefined => store[key] as T | undefined,
|
|
update: async (key: string, value: unknown) => {
|
|
store[key] = value
|
|
},
|
|
} as Pick<Memento, 'get' | 'update'>)
|
|
registry.registerLanguage(createTypeScriptAdapter(WASM_DIR))
|
|
registry.registerTranslator(typescriptZhTranslator)
|
|
return registry
|
|
}
|
|
|
|
function doc(languageId: string, text: string, version = 3): { languageId: string, version: number, getText: () => string } {
|
|
return { languageId, version, getText: () => text }
|
|
}
|
|
|
|
describe('预览的 document 消息', () => {
|
|
it('受支持的语言:带文档版本、代码块、可选翻译和选中的翻译', async () => {
|
|
const message = await documentMessage(doc('typescript', 'if (!folder)\n return []\n'), registryWithTypeScript(), runtime)
|
|
expect(message).toMatchObject({
|
|
type: 'document',
|
|
version: 3,
|
|
languageId: 'typescript',
|
|
translators: [{ id: typescriptZhTranslator.id, label: typescriptZhTranslator.label }],
|
|
translatorId: typescriptZhTranslator.id,
|
|
})
|
|
expect(message.error).toBeUndefined()
|
|
expect(message.blocks.map(b => b.kind)).toEqual(['if'])
|
|
expect(message.blocks[0].children).toMatchObject([{ kind: 'return', translation: [{ text: '返回' }, { text: ' ' }, { text: '空数组' }] }])
|
|
expect(message.attributes).toEqual([
|
|
{ id: 'kind', name: '种类', values: [{ id: 'if', name: '条件', count: 1 }, { id: 'return', name: '返回', count: 1 }] },
|
|
])
|
|
})
|
|
|
|
it('没有语言插件的语言:带 error,不解析', async () => {
|
|
const message = await documentMessage(doc('rust', 'fn main() {}'), registryWithTypeScript(), runtime)
|
|
expect(message.blocks).toEqual([])
|
|
expect(message.attributes).toEqual([])
|
|
expect(message.translators).toEqual([])
|
|
expect(message.error).toContain('rust')
|
|
})
|
|
|
|
it('解析失败:把原因放进 error', async () => {
|
|
const failing = { parse: async () => Promise.reject(new Error('语法包加载失败')) }
|
|
const message = await documentMessage(doc('typescript', 'const a = 1'), registryWithTypeScript(), failing)
|
|
expect(message.blocks).toEqual([])
|
|
expect(message.error).toBe('语法包加载失败')
|
|
})
|
|
})
|