Fix book provider load to not throw on startup (#8835)

* Fix book provider load to not throw on startup

* Move tests to stable

* Rename method

* Fix floating promises and broken test
This commit is contained in:
Charles Gagnon
2020-01-07 17:32:51 -08:00
committed by GitHub
parent b1526603cc
commit 041f34beda
4 changed files with 153 additions and 115 deletions

View File

@@ -41,28 +41,23 @@ export class BookModel implements azdata.nb.NavigationProvider {
return this._allNotebooks;
}
public async getTableOfContentFiles(workspacePath: string): Promise<void> {
try {
let notebookConfig = vscode.workspace.getConfiguration(notebookConfigKey);
let maxDepth = notebookConfig[maxBookSearchDepth];
// Use default value if user enters an invalid value
if (isNullOrUndefined(maxDepth) || maxDepth < 0) {
maxDepth = 5;
} else if (maxDepth === 0) { // No limit of search depth if user enters 0
maxDepth = undefined;
}
public async getTableOfContentFiles(folderPath: string): Promise<void> {
let notebookConfig = vscode.workspace.getConfiguration(notebookConfigKey);
let maxDepth = notebookConfig[maxBookSearchDepth];
// Use default value if user enters an invalid value
if (isNullOrUndefined(maxDepth) || maxDepth < 0) {
maxDepth = 5;
} else if (maxDepth === 0) { // No limit of search depth if user enters 0
maxDepth = undefined;
}
let p = path.join(workspacePath, '**', '_data', 'toc.yml').replace(/\\/g, '/');
let tableOfContentPaths = await glob(p, { deep: maxDepth });
if (tableOfContentPaths.length > 0) {
this._tableOfContentPaths = this._tableOfContentPaths.concat(tableOfContentPaths);
vscode.commands.executeCommand('setContext', 'bookOpened', true);
} else {
vscode.window.showErrorMessage(loc.errBookInitialize);
}
} catch (error) {
console.log(error);
let p = path.join(folderPath, '**', '_data', 'toc.yml').replace(/\\/g, '/');
let tableOfContentPaths = await glob(p, { deep: maxDepth });
if (tableOfContentPaths.length > 0) {
this._tableOfContentPaths = this._tableOfContentPaths.concat(tableOfContentPaths);
vscode.commands.executeCommand('setContext', 'bookOpened', true);
} else {
throw new Error(loc.missingTocError);
}
}

View File

@@ -27,7 +27,6 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
// For testing
private _errorMessage: string;
private _onReadAllTOCFiles: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
private _openAsUntitled: boolean;
public viewId: string;
public books: BookModel[];
@@ -37,29 +36,24 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
this._openAsUntitled = openAsUntitled;
this._extensionContext = extensionContext;
this.books = [];
this.initialize(workspaceFolders.map(a => a.uri.fsPath)).catch(e => console.error(e));
this.initialize(workspaceFolders).catch(e => console.error(e));
this.viewId = view;
this.prompter = new CodeAdapter();
}
private async initialize(bookPaths: string[]): Promise<void> {
private async initialize(workspaceFolders: vscode.WorkspaceFolder[]): Promise<void> {
await vscode.commands.executeCommand('setContext', 'unsavedBooks', this._openAsUntitled);
await Promise.all(bookPaths.map(async (bookPath) => {
let book: BookModel = new BookModel(bookPath, this._openAsUntitled, this._extensionContext);
await book.initializeContents();
this.books.push(book);
if (!this.currentBook) {
this.currentBook = book;
await Promise.all(workspaceFolders.map(async (workspaceFolder) => {
try {
await this.createAndAddBookModel(workspaceFolder.uri.fsPath);
} catch {
// no-op, not all workspace folders are going to be valid books
}
}));
this._initializeDeferred.resolve();
}
public get onReadAllTOCFiles(): vscode.Event<void> {
return this._onReadAllTOCFiles.event;
}
public get initialized(): Promise<void> {
return this._initializeDeferred.promise;
}
@@ -73,7 +67,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
await this.showPreviewFile(urlToOpen);
}
else {
await this.initialize([bookPath]);
await this.createAndAddBookModel(bookPath);
let bookViewer = vscode.window.createTreeView(this.viewId, { showCollapseAll: true, treeDataProvider: this });
this.currentBook = this.books.filter(book => book.bookPath === bookPath)[0];
bookViewer.reveal(this.currentBook.bookItems[0], { expand: vscode.TreeItemCollapsibleState.Expanded, focus: true, select: true });
@@ -84,6 +78,20 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
}
/**
* Creates a model for the specified folder path and adds it to the known list of books if we
* were able to successfully parse it.
* @param bookPath The path to the book folder to create the model for
*/
private async createAndAddBookModel(bookPath: string): Promise<void> {
const book: BookModel = new BookModel(bookPath, this._openAsUntitled, this._extensionContext);
await book.initializeContents();
this.books.push(book);
if (!this.currentBook) {
this.currentBook = book;
}
}
async showPreviewFile(urlToOpen?: string): Promise<void> {
if (this.currentBook) {
const bookRoot = this.currentBook.bookItems[0];