diff --git a/extensions/notebook/src/book/bookTreeView.ts b/extensions/notebook/src/book/bookTreeView.ts index d3f5f9fd7d..799125adc0 100644 --- a/extensions/notebook/src/book/bookTreeView.ts +++ b/extensions/notebook/src/book/bookTreeView.ts @@ -56,6 +56,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider { + await this.initialized; // Whenever the viewer changes visibility then try and reveal the currently active document // in the tree view let openDocument = azdata.nb.activeNotebookEditor; @@ -397,10 +398,10 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider { + notebookPath = notebookPath.replace(/\\/g, '/'); const parentBook = this.books.find(b => notebookPath.indexOf(b.bookPath) > -1); if (!parentBook) { // No parent book, likely because the Notebook is at the top level and not under a Notebook. @@ -431,23 +433,23 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider 0) { + while (depthOfNotebookInBook > -1) { // check if the notebook is available in already expanded levels. bookItem = parentBook.bookItems.find(b => b.tooltip === notebookPath); if (bookItem) { return bookItem; } - // Search for the parent item - // notebook can be inside the same folder as parent and can be in a different folder as well - // so check for both scenarios. - let bookItemToExpand = parentBook.bookItems.find(b => b.tooltip.indexOf(parentPath) > -1) ?? + // Walk down from the top level parent folder one level at each iteration + // and keep expanding until we reach the target notebook leaf + let parentBookPath: string = notebookFolders.slice(0, notebookFolders.length - depthOfNotebookInBook).join('/'); + let bookItemToExpand = parentBook.bookItems.find(b => b.tooltip.indexOf(parentBookPath) > -1) ?? parentBook.bookItems.find(b => path.relative(notebookPath, b.tooltip)?.split(path.sep)?.length === depthOfNotebookInBook); if (!bookItemToExpand) { break; @@ -457,7 +459,13 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider trustableBookPaths.some(trustableBookPath => trustableBookPath === path.join(bookItem.book.root, path.sep))) .some(bookItem => normalizedNotebookUri.startsWith(bookItem.book.version === BookVersion.v1 ? path.join(bookItem.book.root, 'content', path.sep) : path.join(bookItem.book.root, path.sep))); - let isNotebookTrusted = hasTrustedBookPath && this.books.some(bookModel => bookModel.getNotebook(normalizedNotebookUri)); + let isNotebookTrusted = hasTrustedBookPath && this.books.some(bookModel => bookModel.getNotebook(vscode.Uri.file(normalizedNotebookUri).fsPath)); return isNotebookTrusted; } diff --git a/extensions/notebook/src/test/book/book.test.ts b/extensions/notebook/src/test/book/book.test.ts index 3e6ae6e2e8..42422606b3 100644 --- a/extensions/notebook/src/test/book/book.test.ts +++ b/extensions/notebook/src/test/book/book.test.ts @@ -233,7 +233,7 @@ describe('BooksTreeViewTests', function () { }); - it.skip('getParent should return when element is a valid child notebook', async () => { + it('getParent should return when element is a valid child notebook', async () => { let parent = await bookTreeViewProvider.getParent(); should(parent).be.undefined(); @@ -243,8 +243,9 @@ describe('BooksTreeViewTests', function () { }); it('revealActiveDocumentInViewlet should return correct bookItem for highlight', async () => { - let notebook1Path = vscode.Uri.file(path.join(rootFolderPath, 'Book', 'content', 'notebook1.ipynb')).fsPath; + let notebook1Path = path.join(rootFolderPath, 'Book', 'content', 'notebook1.ipynb').replace(/\\/g, '/'); let currentSelection = await bookTreeViewProvider.findAndExpandParentNode(notebook1Path); + should(currentSelection).not.be.undefined(); equalBookItems(currentSelection, expectedNotebook1); }); @@ -327,8 +328,9 @@ describe('BooksTreeViewTests', function () { }); it('revealActiveDocumentInViewlet should return correct bookItem for highlight', async () => { - let notebook1Path = path.join(rootFolderPath, 'Book', 'content', 'notebook1.ipynb'); + let notebook1Path = path.join(rootFolderPath, 'Book', 'content', 'notebook1.ipynb').replace(/\\/g, '/'); let currentSelection = await providedbookTreeViewProvider.findAndExpandParentNode(notebook1Path); + should(currentSelection).not.be.undefined(); equalBookItems(currentSelection, expectedNotebook1); });