Skip to content

Commit f560b15

Browse files
committed
some fixes for editor reopen with another type
1 parent 80e9c11 commit f560b15

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

extensions/vscode-notebook-tests/src/notebook.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ suite('metadata', () => {
396396
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.metadata.custom!['testCellMetadata'] as number, 123);
397397
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript');
398398
});
399+
400+
// TODO copy cell should not copy metadata
399401
});
400402

401403
suite('regression', () => {
@@ -407,6 +409,8 @@ suite('regression', () => {
407409
assert.equal(vscode.notebook.activeNotebookEditor !== undefined, true, 'notebook first');
408410
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.source, '');
409411
assert.equal(vscode.notebook.activeNotebookEditor!.selection?.language, 'typescript');
412+
await vscode.commands.executeCommand('workbench.action.files.saveAll');
413+
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
410414
});
411415

412416
test('#97830, #97764. Support switch to other editor types', async function () {
@@ -427,6 +431,27 @@ suite('regression', () => {
427431
await vscode.commands.executeCommand('workbench.action.files.saveAll');
428432
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
429433
});
434+
435+
// open text editor, pin, and then open a notebook
436+
test('#96105 - dirty editors', async function () {
437+
const resource = vscode.Uri.parse(join(vscode.workspace.rootPath || '', './empty.vsctestnb'));
438+
await vscode.commands.executeCommand('vscode.openWith', resource, 'default');
439+
440+
await waitFor(500);
441+
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
442+
await vscode.commands.executeCommand('default:type', { text: 'var abc = 0;' });
443+
444+
// now it's dirty, open the resource with notebook editor should open a new one
445+
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
446+
await waitFor(500);
447+
448+
assert.notEqual(vscode.notebook.activeNotebookEditor, undefined, 'notebook first');
449+
assert.notEqual(vscode.window.activeTextEditor, undefined);
450+
451+
// await vscode.commands.executeCommand('workbench.action.files.saveAll');
452+
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
453+
});
454+
430455
});
431456

432457
suite('webview resource uri', () => {

src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri
185185
if ((originalInput.group === group.id || originalInput.group === undefined) && (originalInput.viewType === id || typeof id !== 'string')) {
186186
// No need to do anything
187187
originalInput.updateGroup(group.id);
188-
return undefined;
188+
return {
189+
override: this.editorService.openEditor(originalInput, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group)
190+
};
189191
} else {
190192
// Create a copy of the input.
191193
// Unlike normal editor inputs, we do not want to share custom editor inputs
@@ -225,6 +227,13 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri
225227
// user pick a non-notebook editor for this resource
226228
return undefined;
227229
}
230+
} else {
231+
const existingEditors = group.editors.filter(editor => editor.resource && isEqual(editor.resource, resource) && (editor instanceof NotebookEditorInput) && editor.viewType === id);
232+
233+
if (existingEditors.length) {
234+
// switch to this cell
235+
return { override: this.editorService.openEditor(existingEditors[0], new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) };
236+
}
228237
}
229238

230239
if (this._resourceMapping.has(resource)) {
@@ -267,7 +276,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri
267276
input.updateGroup(group.id);
268277
this._resourceMapping.set(resource, input);
269278

270-
return { override: this.editorService.openEditor(input, options, group) };
279+
return { override: this.editorService.openEditor(input, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: !options?.pinned }), group) };
271280
}
272281
}
273282

0 commit comments

Comments
 (0)