Vite 加 code-reader.html 第二入口;代码块树(层级竖线、折叠、种类标签配色)、译文 / 源码按词性着色、块内直接编辑(防抖回写整行、Esc 回预览)、翻译下拉、解析失败提示;看板与代码阅读共用带类型的 acquireVsCodeApi 包装和 ErrorBoundary。
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
import type { Segment } from './types'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { recolor, sliceSegments } from './segments'
|
|
|
|
const tokens: Segment[] = [
|
|
{ text: 'const', role: 'storage' },
|
|
{ text: ' ', role: 'plain' },
|
|
{ text: 'uris', role: 'variable' },
|
|
{ text: ' = ', role: 'plain' },
|
|
{ text: '42', role: 'number' },
|
|
]
|
|
const code = 'const uris = 42'
|
|
|
|
function join(segments: Segment[]): string {
|
|
return segments.map(s => s.text).join('')
|
|
}
|
|
|
|
describe('sliceSegments', () => {
|
|
it('跨段切出一段时每个字保留原词性', () => {
|
|
expect(sliceSegments(tokens, 3, 8)).toEqual([
|
|
{ text: 'st', role: 'storage' },
|
|
{ text: ' ', role: 'plain' },
|
|
{ text: 'ur', role: 'variable' },
|
|
])
|
|
})
|
|
|
|
it('空区间返回空数组', () => {
|
|
expect(sliceSegments(tokens, 4, 4)).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('recolor', () => {
|
|
it('文字没变时原样返回着色', () => {
|
|
expect(recolor(tokens, code, code)).toBe(tokens)
|
|
})
|
|
|
|
it('中间插入的字不着色,前后沿用原词性', () => {
|
|
const text = 'const allUris = 42'
|
|
const result = recolor(tokens, code, text)
|
|
expect(join(result)).toBe(text)
|
|
expect(result).toEqual([
|
|
{ text: 'const', role: 'storage' },
|
|
{ text: ' ', role: 'plain' },
|
|
{ text: 'allU', role: 'plain' },
|
|
{ text: 'ris', role: 'variable' },
|
|
{ text: ' = ', role: 'plain' },
|
|
{ text: '42', role: 'number' },
|
|
])
|
|
})
|
|
|
|
it('删掉结尾时只剩前缀的着色', () => {
|
|
expect(recolor(tokens, code, 'const ur')).toEqual([
|
|
{ text: 'const', role: 'storage' },
|
|
{ text: ' ', role: 'plain' },
|
|
{ text: 'ur', role: 'variable' },
|
|
])
|
|
})
|
|
|
|
it('重复字符不会让前后缀重叠', () => {
|
|
const result = recolor([{ text: 'aa', role: 'variable' }], 'aa', 'aaa')
|
|
expect(join(result)).toBe('aaa')
|
|
})
|
|
|
|
it('清空后没有任何段', () => {
|
|
expect(recolor(tokens, code, '')).toEqual([])
|
|
})
|
|
})
|