fix the endless loop scenario that breaks tree scroll (#17971)

* fix the endless loop scenario

* add comments
This commit is contained in:
Maddy
2022-01-21 17:43:37 -08:00
committed by GitHub
parent 7509188764
commit 4012f26976

View File

@@ -425,6 +425,8 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
if (shouldReveal || this._bookViewer?.visible) {
// CAVEAT: findAndExpandParentNode assumes that the file structure defined in the toc
// follows the location on disk, it can fail otherwise.
bookItem = notebookPath ? await this.findAndExpandParentNode(notebookPath, shouldFocus) : undefined;
// Select + focus item in viewlet if books viewlet is already open, or if we pass in variable
if (bookItem?.contextValue && bookItem.contextValue !== 'pinnedNotebook') {
@@ -461,6 +463,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
let depthOfNotebookInBook: number = path.relative(notebookPath, parentBook.bookPath).split(path.sep).length;
// Walk the tree, expanding parent nodes as needed to load the child nodes until
// we find the one for our Notebook
let expandedBookItems: BookTreeItem[] = [parentBook.rootNode];
while (depthOfNotebookInBook > -1) {
// check if the notebook is available in already expanded levels.
bookItem = parentBook.bookItems.find(b => b.tooltip === notebookPath);
@@ -478,9 +481,11 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
// since bookItems will not have them yet, check the sections->file property
bookItemToExpand = parentBook.bookItems.find(b => b.sections?.find(n => notebookFolders[notebookFolders.length - depthOfNotebookInBook - 1].indexOf(n.file.substring(n.file.lastIndexOf('/') + 1)) > -1));
// book isn't found even in the same level, break out and return.
if (!bookItemToExpand) {
if (!bookItemToExpand || expandedBookItems.includes(bookItemToExpand)) {
break;
}
// Since we don't have the same structure defined in the toc on disk,
// Check to see if the files are in the same level as the parent ->
// increment to reset the depth since parent is in the same level
depthOfNotebookInBook++;
}
@@ -492,6 +497,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
try {
// TO DO: Check why the reveal fails during initial load with 'TreeError [bookTreeView] Tree element not found'
await this._bookViewer.reveal(bookItemToExpand, { select: false, focus: shouldFocus, expand: true });
expandedBookItems.push(bookItemToExpand);
}
catch (e) {
console.error(e);