@@ -59,57 +59,59 @@ export function registerCvbContextMenu(context: vscode.ExtensionContext) {
5959 }
6060
6161 // 注册右键菜单命令
62- vscode . commands . registerCommand ( 'codeReDesign.openCvbFile ' , ( uri : vscode . Uri ) => {
62+ vscode . commands . registerCommand ( 'codeReDesign.showFile ' , ( uri : vscode . Uri ) => {
6363 vscode . window . showTextDocument ( uri ) ;
6464 } ) ;
6565}
6666
67- class CvbViewProvider implements vscode . TreeDataProvider < CvbFile > {
68- private _onDidChangeTreeData : vscode . EventEmitter < CvbFile | undefined > = new vscode . EventEmitter < CvbFile | undefined > ( ) ;
69- readonly onDidChangeTreeData : vscode . Event < CvbFile | undefined > = this . _onDidChangeTreeData . event ;
67+
68+ class CvbViewProvider implements vscode . TreeDataProvider < vscode . TreeItem > {
69+ // 修改返回类型为更通用的TreeItem
70+ private _onDidChangeTreeData : vscode . EventEmitter < vscode . TreeItem | undefined > = new vscode . EventEmitter < vscode . TreeItem | undefined > ( ) ;
71+ readonly onDidChangeTreeData : vscode . Event < vscode . TreeItem | undefined > = this . _onDidChangeTreeData . event ;
7072
7173 // 刷新视图
7274 refresh ( ) : void {
7375 this . _onDidChangeTreeData . fire ( undefined ) ;
7476 }
7577
7678 // 获取树节点
77- getTreeItem ( element : CvbFile ) : vscode . TreeItem {
79+ getTreeItem ( element : vscode . TreeItem ) : vscode . TreeItem {
7880 return element ;
7981 }
8082
8183 // 获取子节点
82- async getChildren ( element ?: CvbFile ) : Promise < CvbFile [ ] > {
84+ async getChildren ( element ?: vscode . TreeItem ) : Promise < vscode . TreeItem [ ] > {
8385 if ( element ) {
84- // 如果有子节点,可以在这里处理
8586 return [ ] ;
8687 } else {
87- // 从指定子文件夹中读取 .cvb 文件
8888 const workspaceFolders = vscode . workspace . workspaceFolders ;
89- if ( ! workspaceFolders ) {
90- return [ ] ;
91- }
89+ if ( ! workspaceFolders ) { return [ ] ; }
9290
93- const cvbFiles : CvbFile [ ] = [ ] ;
94- const targetFolder = path . join ( workspaceFolders [ 0 ] . uri . fsPath , '.CodeReDesignWorkSpace' ) ; // 替换为你的子文件夹名称
91+ const files : vscode . TreeItem [ ] = [ ] ;
92+ const targetFolder = path . join ( workspaceFolders [ 0 ] . uri . fsPath , '.CodeReDesignWorkSpace' ) ;
9593
96- // 读取文件夹中的文件
9794 if ( fs . existsSync ( targetFolder ) ) {
98- const files = fs . readdirSync ( targetFolder ) ;
99- files . forEach ( file => {
100- if ( file . endsWith ( '.cvb' ) || file . endsWith ( '.md' ) ) {
95+ fs . readdirSync ( targetFolder ) . forEach ( file => {
10196 const filePath = path . join ( targetFolder , file ) ;
102- cvbFiles . push ( new CvbFile ( file , vscode . Uri . file ( filePath ) ) ) ;
103- }
97+ const uri = vscode . Uri . file ( filePath ) ;
98+
99+ if ( file . endsWith ( '.cvb' ) ) {
100+ files . push ( new CvbFile ( file , uri ) ) ;
101+ } else if ( file . endsWith ( '.md' ) ) {
102+ files . push ( new MDFile ( file , uri ) ) ;
103+ }
104104 } ) ;
105105 }
106106
107- // 新增排序逻辑
108- cvbFiles . sort ( ( a , b ) =>
109- a . label . localeCompare ( b . label , undefined , { sensitivity : 'base' } )
110- ) ;
107+ // 修改后的排序逻辑
108+ files . sort ( ( a , b ) => {
109+ const labelA = a . label ? a . label . toString ( ) : '' ;
110+ const labelB = b . label ? b . label . toString ( ) : '' ;
111+ return labelA . localeCompare ( labelB , undefined , { sensitivity : 'base' } ) ;
112+ } ) ;
111113
112- return cvbFiles ;
114+ return files ;
113115 }
114116 }
115117}
@@ -120,24 +122,33 @@ class CvbFile extends vscode.TreeItem {
120122 public readonly uri : vscode . Uri
121123 ) {
122124 super ( label , vscode . TreeItemCollapsibleState . None ) ;
123-
124- // 设置命令,单击时打开文件
125125 this . command = {
126- command : 'codeReDesign.openCvbFile ' ,
126+ command : 'codeReDesign.showFile ' ,
127127 title : 'Open CVB File' ,
128128 arguments : [ uri ]
129129 } ;
130+ this . iconPath = new vscode . ThemeIcon ( 'files' ) ; // 使用代码图标
131+ this . contextValue = 'cvbFile' ; // 上下文值保持不变
132+ }
133+ }
130134
131- // 设置图标(可选)
132- this . iconPath = vscode . ThemeIcon . File ;
133-
134- this . resourceUri = uri ;
135-
136- // 添加上下文菜单
137- this . contextValue = 'cvbFile' ;
135+ class MDFile extends vscode . TreeItem {
136+ constructor (
137+ public readonly label : string ,
138+ public readonly uri : vscode . Uri
139+ ) {
140+ super ( label , vscode . TreeItemCollapsibleState . None ) ;
141+ this . command = {
142+ command : 'codeReDesign.showFile' , // 复用同一个打开命令
143+ title : 'Open Markdown File' ,
144+ arguments : [ uri ]
145+ } ;
146+ this . iconPath = new vscode . ThemeIcon ( 'comment-discussion' ) ; // 使用文档图标
147+ this . contextValue = 'mdFile' ; // 新的上下文值
138148 }
139149}
140150
151+
141152/**
142153 * 处理 .cvb 文件的函数
143154 * @param filePath .cvb 文件的路径
0 commit comments