@@ -6,6 +6,51 @@ import { analyzeCode } from './deepseekApi';
66import { getCurrentOperationController , resetCurrentOperationController , clearCurrentOperationController , doUploadCommand , saveAnalyzeCodeResult } from './extension' ;
77import { showInputMultiLineBox } from './UIComponents' ;
88
9+ class ChatPreviewFileSystemProvider implements vscode . FileSystemProvider {
10+ private content : Uint8Array = new Uint8Array ( ) ;
11+ private readonly _emitter = new vscode . EventEmitter < vscode . FileChangeEvent [ ] > ( ) ;
12+
13+ onDidChangeFile : vscode . Event < vscode . FileChangeEvent [ ] > = this . _emitter . event ;
14+
15+ watch ( ) : vscode . Disposable {
16+ return new vscode . Disposable ( ( ) => { } ) ;
17+ }
18+
19+ stat ( ) : vscode . FileStat {
20+ return {
21+ type : vscode . FileType . File ,
22+ ctime : Date . now ( ) ,
23+ mtime : Date . now ( ) ,
24+ size : this . content . length
25+ } ;
26+ }
27+
28+ readDirectory ( ) : never {
29+ throw vscode . FileSystemError . FileIsADirectory ( ) ;
30+ }
31+
32+ createDirectory ( ) : never {
33+ throw vscode . FileSystemError . FileIsADirectory ( ) ;
34+ }
35+
36+ readFile ( ) : Uint8Array {
37+ return this . content ;
38+ }
39+
40+ writeFile ( uri : vscode . Uri , content : Uint8Array ) : void {
41+ this . content = content ;
42+ this . _emitter . fire ( [ { type : vscode . FileChangeType . Changed , uri } ] ) ;
43+ }
44+
45+ delete ( ) : never {
46+ throw vscode . FileSystemError . NoPermissions ( ) ;
47+ }
48+
49+ rename ( ) : never {
50+ throw vscode . FileSystemError . NoPermissions ( ) ;
51+ }
52+ }
53+
954export function registerCvbContextMenu ( context : vscode . ExtensionContext ) {
1055
1156 // 注册右键菜单命令
@@ -59,16 +104,54 @@ export function registerCvbContextMenu(context: vscode.ExtensionContext) {
59104 context . subscriptions . push ( watcher ) ;
60105 }
61106
62- // 注册命令
63- context . subscriptions . push (
64- vscode . commands . registerCommand ( 'codeReDesign.showFile' , ( uri : vscode . Uri ) => {
65- // 如果插件已安装,使用其命令打开文件
66- vscode . commands . executeCommand ( 'markdown.showPreview' , uri ) ;
67- //vscode.commands.executeCommand('vscode.open', uri);
68- } )
69- ) ;
107+ const scheme = 'chatpreview' ;
108+ const provider = new ChatPreviewFileSystemProvider ( ) ;
109+ context . subscriptions . push ( vscode . workspace . registerFileSystemProvider ( scheme , provider , { isCaseSensitive : true } ) ) ;
110+
111+ const previewUri = vscode . Uri . parse ( `${ scheme } :/chat-preview.md` ) ;
112+
113+ context . subscriptions . push (
114+ vscode . commands . registerCommand ( 'codeReDesign.showFile' , async ( uri : vscode . Uri ) => {
115+ // 读取文件内容
116+ const content = await vscode . workspace . fs . readFile ( uri ) ;
117+ const rawText = Buffer . from ( content ) . toString ( 'utf-8' ) ;
118+
119+ // 处理文本内容
120+ const decoratedText = processChatContent ( rawText ) ;
121+
122+ // 更新虚拟文件内容
123+ provider . writeFile ( previewUri , Buffer . from ( decoratedText ) ) ;
124+
125+ // 打开或刷新预览
126+ await vscode . commands . executeCommand ( 'markdown.showPreview' , previewUri ) ;
127+ } )
128+ ) ;
70129}
71130
131+ // 处理聊天内容的独立函数
132+ function processChatContent ( text : string ) : string {
133+ // 修饰内容
134+ let processedText = text . replace ( / ^ @ u s e r : \n / gm, '🙋♂️ User:\n' ) ;
135+ processedText = processedText . replace ( / ^ @ A I : \n / gm, '🧠 AI:\n' ) ;
136+
137+ // 包裹对话块
138+ const blocks = processedText . split ( / ( 🙋 ♂ ️ U s e r : | 🧠 A I : ) / ) ;
139+ let decoratedText = '' ;
140+
141+ for ( let i = 0 ; i < blocks . length ; i ++ ) {
142+ if ( blocks [ i ] . startsWith ( '🙋♂️ User:' ) ) {
143+ // 用户:深蓝色背景,白色文字
144+ decoratedText += `<div style="background-color: #1E3A8A; color: #FFFFFF; padding: 10px; border-radius: 5px; margin-bottom: 10px;">\n${ blocks [ i ] } \n` ;
145+ } else if ( blocks [ i ] . startsWith ( '🧠 AI:' ) ) {
146+ // AI:浅灰色背景,深灰色文字
147+ decoratedText += `<div style="background-color: #F3F4F6; color: #1F2937; padding: 10px; border-radius: 5px; margin-bottom: 10px;">\n${ blocks [ i ] } \n` ;
148+ } else if ( blocks [ i ] . trim ( ) ) { // 只处理非空内容
149+ decoratedText += `${ blocks [ i ] } \n</div>\n` ;
150+ }
151+ }
152+
153+ return decoratedText ;
154+ }
72155
73156class CvbViewProvider implements vscode . TreeDataProvider < vscode . TreeItem > {
74157 // 修改返回类型为更通用的TreeItem
0 commit comments