From efaa2c0e3f9385ccae7ac2c0b8b2917fc61cda85 Mon Sep 17 00:00:00 2001 From: Kevin Cunnane Date: Thu, 14 Mar 2019 14:27:08 -0700 Subject: [PATCH] Fix #4505 Notebooks: New Notebook will not work if existing untitled notebooks are rehydrated (#4506) * Fix #4505 Notebooks: New Notebook will not work if existing untitled notebooks are rehydrated * Also fixes #4508 * Unify behavior across New Notebook entry points - Use Notebook-{n} as the standard in both entry points - Use SQL as default provider in both - Ensure both check for other names and only use free number --- extensions/mssql/src/main.ts | 25 ++++++++++++++++--------- extensions/notebook/src/extension.ts | 20 +++++++++++++++++--- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/extensions/mssql/src/main.ts b/extensions/mssql/src/main.ts index 0ef1e49d67..cfebb419db 100644 --- a/extensions/mssql/src/main.ts +++ b/extensions/mssql/src/main.ts @@ -172,19 +172,27 @@ function saveProfileAndCreateNotebook(profile: azdata.IConnectionProfile): Promi return handleNewNotebookTask(undefined, profile); } +function findNextUntitledEditorName(): string { + let nextVal = untitledCounter; + // Note: this will go forever if it's coded wrong, or you have inifinite Untitled notebooks! + while (true) { + let title = `Notebook-${nextVal++}`; + let hasTextDoc = vscode.workspace.textDocuments.findIndex(doc => doc.isUntitled && doc.fileName === title) > -1; + let hasNotebookDoc = azdata.nb.notebookDocuments.findIndex(doc => doc.isUntitled && doc.fileName === title) > -1; + if (!hasTextDoc && !hasNotebookDoc) { + untitledCounter = nextVal; + return title; + } + } +} async function handleNewNotebookTask(oeContext?: azdata.ObjectExplorerContext, profile?: azdata.IConnectionProfile): Promise { // Ensure we get a unique ID for the notebook. For now we're using a different prefix to the built-in untitled files // to handle this. We should look into improving this in the future - let untitledUri = vscode.Uri.parse(`untitled:Notebook-${untitledCounter++}`); + let title = findNextUntitledEditorName(); + let untitledUri = vscode.Uri.parse(`untitled:${title}`); let editor = await azdata.nb.showNotebookDocument(untitledUri, { connectionProfile: profile, - providerId: jupyterNotebookProviderId, - preview: false, - defaultKernel: { - name: 'pyspark3kernel', - display_name: 'PySpark3', - language: 'python' - } + preview: false }); if (oeContext && oeContext.nodeInfo && oeContext.nodeInfo.nodePath) { // Get the file path after '/HDFS' @@ -221,7 +229,6 @@ async function handleOpenNotebookTask(profile: azdata.IConnectionProfile): Promi } else { await azdata.nb.showNotebookDocument(fileUri, { connectionProfile: profile, - providerId: jupyterNotebookProviderId, preview: false }); } diff --git a/extensions/notebook/src/extension.ts b/extensions/notebook/src/extension.ts index 4587346484..d77e1b99d2 100644 --- a/extensions/notebook/src/extension.ts +++ b/extensions/notebook/src/extension.ts @@ -20,7 +20,7 @@ const JUPYTER_NOTEBOOK_PROVIDER = 'jupyter'; const msgSampleCodeDataFrame = localize('msgSampleCodeDataFrame', 'This sample code loads the file into a data frame and shows the first 10 results.'); const noNotebookVisible = localize('noNotebookVisible', 'No notebook editor is active'); -let counter = 0; +let untitledCounter = 0; export let controller: JupyterController; @@ -54,7 +54,7 @@ export function activate(extensionContext: vscode.ExtensionContext) { } function newNotebook(connectionProfile: azdata.IConnectionProfile) { - let title = `Untitled-${counter++}`; + let title = findNextUntitledEditorName(); let untitledUri = vscode.Uri.parse(`untitled:${title}`); let options: azdata.nb.NotebookShowOptions = connectionProfile ? { viewColumn: null, @@ -71,6 +71,20 @@ function newNotebook(connectionProfile: azdata.IConnectionProfile) { }); } +function findNextUntitledEditorName(): string { + let nextVal = untitledCounter; + // Note: this will go forever if it's coded wrong, or you have infinite Untitled notebooks! + while (true) { + let title = `Notebook-${nextVal++}`; + let hasTextDoc = vscode.workspace.textDocuments.findIndex(doc => doc.isUntitled && doc.fileName === title) > -1; + let hasNotebookDoc = azdata.nb.notebookDocuments.findIndex(doc => doc.isUntitled && doc.fileName === title) > -1; + if (!hasTextDoc && !hasNotebookDoc) { + untitledCounter = nextVal; + return title; + } + } +} + async function openNotebook(): Promise { try { let filter = {}; @@ -123,7 +137,7 @@ async function addCell(cellType: azdata.nb.CellType): Promise { async function analyzeNotebook(oeContext?: azdata.ObjectExplorerContext): Promise { // Ensure we get a unique ID for the notebook. For now we're using a different prefix to the built-in untitled files // to handle this. We should look into improving this in the future - let untitledUri = vscode.Uri.parse(`untitled:Notebook-${counter++}`); + let untitledUri = vscode.Uri.parse(`untitled:Notebook-${untitledCounter++}`); let editor = await azdata.nb.showNotebookDocument(untitledUri, { connectionProfile: oeContext ? oeContext.connectionProfile : undefined,