✨ feat(vscode): 代码阅读的解析与模板直译核心
This commit is contained in:
1 parent
af56985d9a
commit
4d34b8c715
24 files changed
+2294
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { toBlockViews } from './blockView'
|
||||
import { commonIndent, reindentBlock, replaceLines } from './editText'
|
||||
import { createTypeScriptAdapter } from './languages/typescript'
|
||||
import { readFixture, WASM_DIR, withBlocks } from './testSupport'
|
||||
|
||||
describe('行替换', () => {
|
||||
it('多行块替换后每个非空行补回原来的缩进,空行保持空', () => {
|
||||
const source = [
|
||||
'function f() {',
|
||||
' if (a)',
|
||||
' return 1',
|
||||
'}',
|
||||
].join('\n')
|
||||
const edited = replaceLines(source, 1, 2, 'if (a && b) {\n\n return 2\n}')
|
||||
expect(edited).toBe([
|
||||
'function f() {',
|
||||
' if (a && b) {',
|
||||
'',
|
||||
' return 2',
|
||||
' }',
|
||||
'}',
|
||||
].join('\n'))
|
||||
})
|
||||
|
||||
it('保留 \\r\\n 换行', () => {
|
||||
expect(replaceLines('a\r\n b\r\n c\r\n', 1, 1, 'x\ny')).toBe('a\r\n x\r\n y\r\n c\r\n')
|
||||
})
|
||||
|
||||
it('共同缩进逐字符比,混用制表符和空格时只取相同的前缀', () => {
|
||||
expect(commonIndent(['\t a', '', '\t b'])).toBe('\t ')
|
||||
expect(commonIndent(['\ta', ' b'])).toBe('')
|
||||
expect(reindentBlock([' x'], 'a\n b')).toBe(' a\n b')
|
||||
})
|
||||
|
||||
it('把预览里的 code 原样写回,文件不变(去缩进与补缩进互逆)', async () => {
|
||||
const adapter = createTypeScriptAdapter(WASM_DIR)
|
||||
const source = readFixture('envLock.ts.txt')
|
||||
const views = await withBlocks(adapter, 'typescript', source, blocks => toBlockViews(blocks, source, adapter, undefined))
|
||||
const all = views.flatMap(function flat(view): typeof views {
|
||||
return [view, ...view.children.flatMap(flat)]
|
||||
})
|
||||
for (const view of all)
|
||||
expect(replaceLines(source, view.startLine, view.endLine, view.code), view.id).toBe(source)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user