mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Select active notebook in viewlet when opened from command pallet (#15027)
* await initialized * fixes specific for windows * address comments * update comment
This commit is contained in:
@@ -56,6 +56,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
|||||||
this.bookTocManager = new BookTocManager();
|
this.bookTocManager = new BookTocManager();
|
||||||
this._bookViewer = vscode.window.createTreeView(this.viewId, { showCollapseAll: true, treeDataProvider: this });
|
this._bookViewer = vscode.window.createTreeView(this.viewId, { showCollapseAll: true, treeDataProvider: this });
|
||||||
this._bookViewer.onDidChangeVisibility(async e => {
|
this._bookViewer.onDidChangeVisibility(async e => {
|
||||||
|
await this.initialized;
|
||||||
// Whenever the viewer changes visibility then try and reveal the currently active document
|
// Whenever the viewer changes visibility then try and reveal the currently active document
|
||||||
// in the tree view
|
// in the tree view
|
||||||
let openDocument = azdata.nb.activeNotebookEditor;
|
let openDocument = azdata.nb.activeNotebookEditor;
|
||||||
@@ -397,10 +398,10 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
|||||||
if (!uri) {
|
if (!uri) {
|
||||||
let openDocument = azdata.nb.activeNotebookEditor;
|
let openDocument = azdata.nb.activeNotebookEditor;
|
||||||
if (openDocument) {
|
if (openDocument) {
|
||||||
notebookPath = openDocument.document.uri.fsPath.replace(/\\/g, '/');
|
notebookPath = openDocument.document.uri.fsPath;
|
||||||
}
|
}
|
||||||
} else if (uri.fsPath) {
|
} else if (uri.fsPath) {
|
||||||
notebookPath = uri.fsPath.replace(/\\/g, '/');
|
notebookPath = uri.fsPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldReveal || this._bookViewer?.visible) {
|
if (shouldReveal || this._bookViewer?.visible) {
|
||||||
@@ -415,6 +416,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findAndExpandParentNode(notebookPath: string): Promise<BookTreeItem | undefined> {
|
async findAndExpandParentNode(notebookPath: string): Promise<BookTreeItem | undefined> {
|
||||||
|
notebookPath = notebookPath.replace(/\\/g, '/');
|
||||||
const parentBook = this.books.find(b => notebookPath.indexOf(b.bookPath) > -1);
|
const parentBook = this.books.find(b => notebookPath.indexOf(b.bookPath) > -1);
|
||||||
if (!parentBook) {
|
if (!parentBook) {
|
||||||
// No parent book, likely because the Notebook is at the top level and not under a Notebook.
|
// 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<BookTreeIte
|
|||||||
// the top we'll expand nodes until we find the parent of the Notebook we're looking for
|
// the top we'll expand nodes until we find the parent of the Notebook we're looking for
|
||||||
// get the children of root node and expand the nodes to the notebook level.
|
// get the children of root node and expand the nodes to the notebook level.
|
||||||
await this.getChildren(parentBook.rootNode);
|
await this.getChildren(parentBook.rootNode);
|
||||||
// The path to the parent of the Notebook we're looking for (this is the node we're looking to expand)
|
// The path to the Notebook we're looking for (these are the nodes we're looking to expand)
|
||||||
const parentPath = notebookPath.substring(0, notebookPath.lastIndexOf(path.posix.sep));
|
const notebookFolders = notebookPath.split('/');
|
||||||
// Find number of directories between the Notebook path and the root of the book it's contained in
|
// Find number of directories between the Notebook path and the root of the book it's contained in
|
||||||
// so we know how many parent nodes to expand
|
// so we know how many parent nodes to expand
|
||||||
let depthOfNotebookInBook: number = path.relative(notebookPath, parentBook.bookPath).split(path.sep).length;
|
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
|
// Walk the tree, expanding parent nodes as needed to load the child nodes until
|
||||||
// we find the one for our Notebook
|
// we find the one for our Notebook
|
||||||
while (depthOfNotebookInBook > 0) {
|
while (depthOfNotebookInBook > -1) {
|
||||||
// check if the notebook is available in already expanded levels.
|
// check if the notebook is available in already expanded levels.
|
||||||
bookItem = parentBook.bookItems.find(b => b.tooltip === notebookPath);
|
bookItem = parentBook.bookItems.find(b => b.tooltip === notebookPath);
|
||||||
if (bookItem) {
|
if (bookItem) {
|
||||||
return bookItem;
|
return bookItem;
|
||||||
}
|
}
|
||||||
// Search for the parent item
|
// Walk down from the top level parent folder one level at each iteration
|
||||||
// notebook can be inside the same folder as parent and can be in a different folder as well
|
// and keep expanding until we reach the target notebook leaf
|
||||||
// so check for both scenarios.
|
let parentBookPath: string = notebookFolders.slice(0, notebookFolders.length - depthOfNotebookInBook).join('/');
|
||||||
let bookItemToExpand = parentBook.bookItems.find(b => b.tooltip.indexOf(parentPath) > -1) ??
|
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);
|
parentBook.bookItems.find(b => path.relative(notebookPath, b.tooltip)?.split(path.sep)?.length === depthOfNotebookInBook);
|
||||||
if (!bookItemToExpand) {
|
if (!bookItemToExpand) {
|
||||||
break;
|
break;
|
||||||
@@ -457,7 +459,13 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
|||||||
// continue expanding and search its children
|
// continue expanding and search its children
|
||||||
await this.getChildren(bookItemToExpand);
|
await this.getChildren(bookItemToExpand);
|
||||||
}
|
}
|
||||||
await this._bookViewer.reveal(bookItemToExpand, { select: false, focus: true, expand: 3 });
|
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: true, expand: 3 });
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
depthOfNotebookInBook--;
|
depthOfNotebookInBook--;
|
||||||
}
|
}
|
||||||
return bookItem;
|
return bookItem;
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export class BookTrustManager implements IBookTrustManager {
|
|||||||
let hasTrustedBookPath: boolean = treeBookItems
|
let hasTrustedBookPath: boolean = treeBookItems
|
||||||
.filter(bookItem => trustableBookPaths.some(trustableBookPath => trustableBookPath === path.join(bookItem.book.root, path.sep)))
|
.filter(bookItem => 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)));
|
.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;
|
return isNotebookTrusted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
let parent = await bookTreeViewProvider.getParent();
|
||||||
should(parent).be.undefined();
|
should(parent).be.undefined();
|
||||||
|
|
||||||
@@ -243,8 +243,9 @@ describe('BooksTreeViewTests', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('revealActiveDocumentInViewlet should return correct bookItem for highlight', async () => {
|
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);
|
let currentSelection = await bookTreeViewProvider.findAndExpandParentNode(notebook1Path);
|
||||||
|
should(currentSelection).not.be.undefined();
|
||||||
equalBookItems(currentSelection, expectedNotebook1);
|
equalBookItems(currentSelection, expectedNotebook1);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -327,8 +328,9 @@ describe('BooksTreeViewTests', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('revealActiveDocumentInViewlet should return correct bookItem for highlight', async () => {
|
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);
|
let currentSelection = await providedbookTreeViewProvider.findAndExpandParentNode(notebook1Path);
|
||||||
|
should(currentSelection).not.be.undefined();
|
||||||
equalBookItems(currentSelection, expectedNotebook1);
|
equalBookItems(currentSelection, expectedNotebook1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user