✨ feat(vscode): 代码阅读入口、文件树与预览编辑器

This commit is contained in:
cruldra committed 2026-09-24 21:36:00 +08:00
1 parent 4d34b8c715
commit 2b823a32ca
24 files changed
+768 -72

No files matched your search

+30
View File
@@ -39,3 +39,33 @@ export function replaceLines(source: string, startLine: number, endLine: number,
const replacement = splitLines(reindentBlock(lines.slice(startLine, endLine + 1), text))
return [...lines.slice(0, startLine), ...replacement, ...lines.slice(endLine + 1)].join(eol)
}
/** 写回文件时要整行替换的一段:从 startLine 行首到 endLine 行尾(不含换行符)。 */
export interface LineReplacement {
startLine: number
endLine: number
/** endLine 这一行的长度。 */
endCharacter: number
/** 补回缩进后的新文本,行间用 \n(写进文档时 VS Code 按文档的换行符转换)。 */
text: string
/** 和原来那几行一字不差:不必写回,免得留下一次空的撤销记录。 */
unchanged: boolean
}
/**
* 预览里块内改完的文本 → 对当前文档的整行替换。行号已不在文档里(文档刚被别处改短)时返回 undefined。
* @param lineAt 读文档第 n 行(不含换行符)
*/
export function blockReplacement(lineAt: (line: number) => string, lineCount: number, startLine: number, endLine: number, text: string): LineReplacement | undefined {
if (!Number.isInteger(startLine) || !Number.isInteger(endLine) || startLine < 0 || endLine < startLine || endLine >= lineCount)
return undefined
const original = Array.from({ length: endLine - startLine + 1 }, (_, i) => lineAt(startLine + i))
const replacement = reindentBlock(original, text)
return {
startLine,
endLine,
endCharacter: original[original.length - 1].length,
text: replacement,
unchanged: replacement === original.join('\n'),
}
}