From f5e3b511515add944fdbb52152b4fbda8946aae8 Mon Sep 17 00:00:00 2001 From: Lucy Zhang Date: Fri, 9 Aug 2019 14:19:10 -0700 Subject: [PATCH] Move finding toc.yml files logic to async method in Books (#6623) * moved findTOC logic to async function * set max number of subdirectories to search * log error, change for loop syntax * hadnling edge cases --- extensions/notebook/package.json | 6 + extensions/notebook/package.nls.json | 3 +- extensions/notebook/src/book/bookTreeView.ts | 47 ++++---- extensions/notebook/src/common/constants.ts | 3 +- extensions/notebook/yarn.lock | 110 ++++++++++++++++++- 5 files changed, 142 insertions(+), 27 deletions(-) diff --git a/extensions/notebook/package.json b/extensions/notebook/package.json index 46e4be3501..d4c914a98b 100644 --- a/extensions/notebook/package.json +++ b/extensions/notebook/package.json @@ -17,6 +17,11 @@ "type": "object", "title": "%notebook.configuration.title%", "properties": { + "notebook.maxBookSearchDepth": { + "type": "number", + "default": 5, + "description": "%notebook.maxBookSearchDepth.description%" + }, "notebook.pythonPath": { "type": "string", "default": "", @@ -350,6 +355,7 @@ "@types/rimraf": "^2.0.2", "decompress": "^4.2.0", "error-ex": "^1.3.1", + "fast-glob": "^3.0.4", "figures": "^2.0.0", "fs-extra": "^5.0.0", "glob": "^7.1.1", diff --git a/extensions/notebook/package.nls.json b/extensions/notebook/package.nls.json index f79a04fb0d..a3766bfa3a 100644 --- a/extensions/notebook/package.nls.json +++ b/extensions/notebook/package.nls.json @@ -6,6 +6,7 @@ "notebook.useExistingPython.description": "Local path to a preexisting python installation used by Notebooks.", "notebook.overrideEditorTheming.description": "Override editor default settings in the Notebook editor. Settings include background color, current line color and border", "notebook.maxTableRows.description": "Maximum number of rows returned per table in the Notebook editor", + "notebook.maxBookSearchDepth.description": "Maximum depth of subdirectories to search for Books (Enter 0 for infinite)", "notebook.command.new": "New Notebook", "notebook.command.open": "Open Notebook", "notebook.analyzeJupyterNotebook": "Analyze in Notebook", @@ -26,4 +27,4 @@ "title.reinstallNotebookDependencies": "Reinstall Notebook dependencies", "title.configurePython": "Configure Python for Notebooks", "title.managePackages": "Manage Packages" -} \ No newline at end of file +} diff --git a/extensions/notebook/src/book/bookTreeView.ts b/extensions/notebook/src/book/bookTreeView.ts index f4f39fa5bf..8f1677a865 100644 --- a/extensions/notebook/src/book/bookTreeView.ts +++ b/extensions/notebook/src/book/bookTreeView.ts @@ -8,45 +8,44 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs'; import * as yaml from 'js-yaml'; +import * as glob from 'fast-glob'; import { BookTreeItem, BookTreeItemType } from './bookTreeItem'; +import { maxBookSearchDepth, notebookConfigKey } from '../common/constants'; import * as nls from 'vscode-nls'; const localize = nls.loadMessageBundle(); - export class BookTreeViewProvider implements vscode.TreeDataProvider, azdata.nb.NavigationProvider { readonly providerId: string = 'BookNavigator'; private _onDidChangeTreeData: vscode.EventEmitter = new vscode.EventEmitter(); readonly onDidChangeTreeData: vscode.Event = this._onDidChangeTreeData.event; - private _tableOfContentsPath: string[]; + private _tableOfContentPaths: string[] = []; private _allNotebooks = new Map(); private _extensionContext: vscode.ExtensionContext; private _throttleTimer: any; private _resource: string; constructor(workspaceFolders: vscode.WorkspaceFolder[], extensionContext: vscode.ExtensionContext) { - let workspacePaths: string[] = workspaceFolders.map(a => a.uri.fsPath); - this._tableOfContentsPath = this.getTableOfContentFiles(workspacePaths); - let bookOpened: boolean = this._tableOfContentsPath && this._tableOfContentsPath.length > 0; - vscode.commands.executeCommand('setContext', 'bookOpened', bookOpened); + this.getTableOfContentFiles(workspaceFolders).then(() => undefined, (err) => { console.log(err); }); this._extensionContext = extensionContext; } - private getTableOfContentFiles(directories: string[]): string[] { - let tableOfContentPaths: string[] = []; - let paths: string[]; - directories.forEach(dir => { - paths = fs.readdirSync(dir); - paths.forEach(filename => { - let fullPath = path.join(dir, filename); - if (fs.statSync(fullPath).isDirectory()) { - tableOfContentPaths = tableOfContentPaths.concat(this.getTableOfContentFiles([fullPath])); - } else if (filename === 'toc.yml') { - tableOfContentPaths.push(fullPath); - } - }); - }); - return tableOfContentPaths; + async getTableOfContentFiles(workspaceFolders: vscode.WorkspaceFolder[]): Promise { + let notebookConfig = vscode.workspace.getConfiguration(notebookConfigKey); + let maxDepth = notebookConfig[maxBookSearchDepth]; + // Use default value if user enters an invalid value + if (maxDepth === undefined || maxDepth < 0) { + maxDepth = 5; + } else if (maxDepth === 0) { // No limit of search depth if user enters 0 + maxDepth = undefined; + } + let workspacePaths: string[] = workspaceFolders.map(a => a.uri.fsPath); + for (let path of workspacePaths) { + let tableOfContentPaths = await glob([path + '/**/_data/toc.yml'], { deep: maxDepth }); + this._tableOfContentPaths = this._tableOfContentPaths.concat(tableOfContentPaths); + } + let bookOpened: boolean = this._tableOfContentPaths.length > 0; + vscode.commands.executeCommand('setContext', 'bookOpened', bookOpened); } async openNotebook(resource: string): Promise { @@ -123,11 +122,11 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider