侧栏「问 AI」打开代码阅读预览。文件夹是没有代码块的一页,源码文件在整页顶部聊,代码块下面的问答不动。历史按工作区文件夹和相对路径记在窗口状态里,不写进按块存的记录。
39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
/**
|
|
* 文件夹、以及代码阅读不认识的文件,用这个协议打开代码阅读预览:
|
|
* 文档是空的,页面上没有代码块,只在顶部聊天。源码文件直接开它自己的预览。
|
|
*/
|
|
|
|
import type { Uri, WorkspaceFolder } from 'vscode'
|
|
import * as path from 'node:path'
|
|
import { workspace } from 'vscode'
|
|
import { relativeQaKey } from '../qa/pathQa'
|
|
|
|
export const PATH_PREVIEW_SCHEME = 'code-reader-path'
|
|
|
|
export type PathSubject = 'file' | 'folder'
|
|
|
|
export function pathPreviewUri(target: Uri, kind: PathSubject): Uri {
|
|
return target.with({ scheme: PATH_PREVIEW_SCHEME, query: kind, fragment: '' })
|
|
}
|
|
|
|
/** 预览文档是路径页时,还原成真实文件或文件夹;普通源码预览不是。 */
|
|
export function pathPreviewOf(uri: Uri): { uri: Uri, kind: PathSubject } | undefined {
|
|
if (uri.scheme !== PATH_PREVIEW_SCHEME)
|
|
return undefined
|
|
if (uri.query !== 'file' && uri.query !== 'folder')
|
|
return undefined
|
|
return { uri: uri.with({ scheme: 'file', query: '', fragment: '' }), kind: uri.query }
|
|
}
|
|
|
|
/**
|
|
* 这条路径在哪个工作区文件夹、相对路径是什么。
|
|
* 工作区根本身的相对路径是空字符串(locate 不认根,顶部问答要认)。
|
|
*/
|
|
export function pathQaLocation(uri: Uri): { folder: WorkspaceFolder, key: string } | undefined {
|
|
const folder = workspace.getWorkspaceFolder(uri)
|
|
if (!folder)
|
|
return undefined
|
|
const key = relativeQaKey(path.posix.normalize(folder.uri.path), path.posix.normalize(uri.path))
|
|
return key === undefined ? undefined : { folder, key }
|
|
}
|