11import { injectable , inject } from 'inversify' ;
2- import { window , commands , workspace , Uri } from 'vscode' ;
2+ import { commands , window , workspace , TreeView , Uri , l10n } from 'vscode' ;
33
44import { IExtensionContext } from '../../platform/common/types' ;
5+ import { IDeepnoteNotebookManager } from '../types' ;
56import { DeepnoteTreeDataProvider } from './deepnoteTreeDataProvider' ;
67import { type DeepnoteTreeItem , DeepnoteTreeItemType , type DeepnoteTreeItemContext } from './deepnoteTreeItem' ;
7- import { DeepnoteNotebookSerializer } from './deepnoteSerializer' ;
88
99/**
1010 * Manages the Deepnote explorer tree view and related commands
1111 */
1212@injectable ( )
1313export class DeepnoteExplorerView {
14- private treeDataProvider : DeepnoteTreeDataProvider ;
15- private serializer : DeepnoteNotebookSerializer ;
14+ private readonly treeDataProvider : DeepnoteTreeDataProvider ;
1615
17- constructor ( @inject ( IExtensionContext ) private extensionContext : IExtensionContext ) {
16+ private treeView : TreeView < DeepnoteTreeItem > ;
17+
18+ constructor (
19+ @inject ( IExtensionContext ) private readonly extensionContext : IExtensionContext ,
20+ @inject ( IDeepnoteNotebookManager ) private readonly manager : IDeepnoteNotebookManager
21+ ) {
1822 this . treeDataProvider = new DeepnoteTreeDataProvider ( ) ;
19- this . serializer = new DeepnoteNotebookSerializer ( ) ;
2023 }
2124
2225 public activate ( ) : void {
23- const treeView = window . createTreeView ( 'deepnoteExplorer' , {
26+ this . treeView = window . createTreeView ( 'deepnoteExplorer' , {
2427 treeDataProvider : this . treeDataProvider ,
2528 showCollapseAll : true
2629 } ) ;
2730
28- this . extensionContext . subscriptions . push ( treeView ) ;
31+ this . extensionContext . subscriptions . push ( this . treeView ) ;
2932 this . extensionContext . subscriptions . push ( this . treeDataProvider ) ;
3033
3134 this . registerCommands ( ) ;
@@ -56,26 +59,36 @@ export class DeepnoteExplorerView {
5659 }
5760
5861 private async openNotebook ( context : DeepnoteTreeItemContext ) : Promise < void > {
62+ console . log ( `Opening notebook: ${ context . notebookId } in project: ${ context . projectId } .` ) ;
63+
5964 if ( ! context . notebookId ) {
65+ await window . showWarningMessage ( l10n . t ( 'Cannot open: missing notebook id.' ) ) ;
66+
6067 return ;
6168 }
6269
6370 try {
64- const fileUri = Uri . file ( context . filePath ) ;
65- const manager = this . serializer . getManager ( ) ;
71+ // Create a unique URI by adding the notebook ID as a query parameter
72+ // This ensures VS Code treats each notebook as a separate document
73+ const fileUri = Uri . file ( context . filePath ) . with ( { query : `notebook=${ context . notebookId } ` } ) ;
6674
67- manager . selectNotebookForProject ( context . projectId , context . notebookId ) ;
75+ console . log ( `Selecting notebook in manager.` ) ;
6876
69- console . log ( `Opening notebook ${ context . notebookId } from project ${ context . projectId } .` ) ;
77+ this . manager . selectNotebookForProject ( context . projectId , context . notebookId ) ;
78+
79+ console . log ( `Opening notebook document.` , fileUri ) ;
7080
7181 const document = await workspace . openNotebookDocument ( fileUri ) ;
7282
83+ console . log ( `Showing notebook document.` ) ;
84+
7385 await window . showNotebookDocument ( document , {
7486 preview : false ,
7587 preserveFocus : false
7688 } ) ;
7789 } catch ( error ) {
7890 const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
91+
7992 await window . showErrorMessage ( `Failed to open notebook: ${ errorMessage } ` ) ;
8093 }
8194 }
@@ -88,6 +101,7 @@ export class DeepnoteExplorerView {
88101 try {
89102 const fileUri = Uri . file ( treeItem . context . filePath ) ;
90103 const document = await workspace . openTextDocument ( fileUri ) ;
104+
91105 await window . showTextDocument ( document ) ;
92106 } catch ( error ) {
93107 const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
@@ -111,10 +125,28 @@ export class DeepnoteExplorerView {
111125 return ;
112126 }
113127
114- await window . showInformationMessage (
115- `Active notebook: ${ notebookMetadata ?. deepnoteNotebookName || 'Untitled' } in project ${
116- notebookMetadata ?. deepnoteProjectName || 'Untitled'
117- } `
118- ) ;
128+ // Try to reveal the notebook in the explorer
129+ try {
130+ const treeItem = await this . treeDataProvider . findTreeItem ( projectId , notebookId ) ;
131+
132+ if ( treeItem ) {
133+ await this . treeView . reveal ( treeItem , { select : true , focus : true , expand : true } ) ;
134+ } else {
135+ // Fall back to showing information if node not found
136+ await window . showInformationMessage (
137+ `Active notebook: ${ notebookMetadata ?. deepnoteNotebookName || 'Untitled' } in project ${
138+ notebookMetadata ?. deepnoteProjectName || 'Untitled'
139+ } `
140+ ) ;
141+ }
142+ } catch ( error ) {
143+ // Fall back to showing information if reveal fails
144+ console . error ( 'Failed to reveal notebook in explorer:' , error ) ;
145+ await window . showInformationMessage (
146+ `Active notebook: ${ notebookMetadata ?. deepnoteNotebookName || 'Untitled' } in project ${
147+ notebookMetadata ?. deepnoteProjectName || 'Untitled'
148+ } `
149+ ) ;
150+ }
119151 }
120152}
0 commit comments