diff --git a/extensions/vscode-notebook-tests/src/notebook.test.ts b/extensions/vscode-notebook-tests/src/notebook.test.ts index 1c3e4d067a3..30fac9913d8 100644 --- a/extensions/vscode-notebook-tests/src/notebook.test.ts +++ b/extensions/vscode-notebook-tests/src/notebook.test.ts @@ -396,6 +396,8 @@ suite('metadata', () => { assert.equal(vscode.notebook.activeNotebookEditor!.selection?.metadata.custom!['testCellMetadata'] as number, 123); assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript'); }); + + // TODO copy cell should not copy metadata }); suite('regression', () => { @@ -407,6 +409,8 @@ suite('regression', () => { assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first'); assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, ''); assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript'); + await vscode.commands.executeCommand('workbench.action.files.saveAll'); + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); }); test('#97830, #97764. Support switch to other editor types', async function () { @@ -427,6 +431,27 @@ suite('regression', () => { await vscode.commands.executeCommand('workbench.action.files.saveAll'); await vscode.commands.executeCommand('workbench.action.closeAllEditors'); }); + + // open text editor, pin, and then open a notebook + test('#96105 - dirty editors', async function () { + const resource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './empty.vsctestnb')); + await vscode.commands.executeCommand('vscode.openWith', resource, 'default'); + + await waitFor(500); + await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow'); + await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' }); + + // now it's dirty, open the resource with notebook editor should open a new one + await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest'); + await waitFor(500); + + assert.notEqual(vscode.notebook.activeNotebookEditor, undefined, 'notebook first'); + assert.notEqual(vscode.window.activeTextEditor, undefined); + + // await vscode.commands.executeCommand('workbench.action.files.saveAll'); + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); + }); + }); suite('webview resource uri', () => { diff --git a/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts b/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts index 31b8364d603..c3f2b7e72c3 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts @@ -185,7 +185,9 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri if ((originalInput.group === group.id || originalInput.group === undefined) && (originalInput.viewType === id || typeof id !== 'string')) { // No need to do anything originalInput.updateGroup(group.id); - return undefined; + return { + override: this.editorService.openEditor(originalInput, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) + }; } else { // Create a copy of the input. // Unlike normal editor inputs, we do not want to share custom editor inputs @@ -225,6 +227,13 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri // user pick a non-notebook editor for this resource return undefined; } + } else { + const existingEditors = group.editors.filter(editor => editor.resource && isEqual(editor.resource, resource) && (editor instanceof NotebookEditorInput) && editor.viewType === id); + + if (existingEditors.length) { + // switch to this cell + return { override: this.editorService.openEditor(existingEditors[0], new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) }; + } } if (this._resourceMapping.has(resource)) { @@ -267,7 +276,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri input.updateGroup(group.id); this._resourceMapping.set(resource, input); - return { override: this.editorService.openEditor(input, options, group) }; + return { override: this.editorService.openEditor(input, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: !options?.pinned }), group) }; } }