Skip to content

Commit

Permalink
move file'ish things into workspace namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Jun 2, 2021
1 parent 0b6e703 commit ac05ae7
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ suite('Notebook Document', function () {
});

suiteSetup(function () {
disposables.push(vscode.notebook.registerNotebookContentProvider('notebook.nbdtest', complexContentProvider));
disposables.push(vscode.notebook.registerNotebookSerializer('notebook.nbdserializer', simpleContentProvider));
disposables.push(vscode.workspace.registerNotebookContentProvider('notebook.nbdtest', complexContentProvider));
disposables.push(vscode.workspace.registerNotebookSerializer('notebook.nbdserializer', simpleContentProvider));
});

test('cannot register sample provider multiple times', function () {
assert.throws(() => {
vscode.notebook.registerNotebookContentProvider('notebook.nbdtest', complexContentProvider);
vscode.workspace.registerNotebookContentProvider('notebook.nbdtest', complexContentProvider);
});
// assert.throws(() => {
// vscode.notebook.registerNotebookSerializer('notebook.nbdserializer', simpleContentProvider);
// vscode.workspace.registerNotebookSerializer('notebook.nbdserializer', simpleContentProvider);
// });
});

test('cannot open unknown types', async function () {
try {
await vscode.notebook.openNotebookDocument(vscode.Uri.parse('some:///thing.notTypeKnown'));
await vscode.workspace.openNotebookDocument(vscode.Uri.parse('some:///thing.notTypeKnown'));
assert.ok(false);
} catch {
assert.ok(true);
Expand All @@ -72,7 +72,7 @@ suite('Notebook Document', function () {

test('document basics', async function () {
const uri = await utils.createRandomFile(undefined, undefined, '.nbdtest');
const notebook = await vscode.notebook.openNotebookDocument(uri);
const notebook = await vscode.workspace.openNotebookDocument(uri);

assert.strictEqual(notebook.uri.toString(), uri.toString());
assert.strictEqual(notebook.isDirty, false);
Expand All @@ -92,7 +92,7 @@ suite('Notebook Document', function () {
// ignore other open events
return;
}
const notebook = vscode.notebook.notebookDocuments.find(notebook => {
const notebook = vscode.workspace.notebookDocuments.find(notebook => {
const cell = notebook.getCells().find(cell => cell.document === doc);
return Boolean(cell);
});
Expand All @@ -108,15 +108,15 @@ suite('Notebook Document', function () {
}, 15000);
});

await vscode.notebook.openNotebookDocument(uri);
await vscode.workspace.openNotebookDocument(uri);
await p;
assert.strictEqual(didHappen, true);
});

test('notebook open/close, all cell-documents are ready', async function () {
const uri = await utils.createRandomFile(undefined, undefined, '.nbdtest');

const p = utils.asPromise(vscode.notebook.onDidOpenNotebookDocument).then(notebook => {
const p = utils.asPromise(vscode.workspace.onDidOpenNotebookDocument).then(notebook => {
for (let i = 0; i < notebook.cellCount; i++) {
let cell = notebook.cellAt(i);

Expand All @@ -130,20 +130,20 @@ suite('Notebook Document', function () {
}
});

await vscode.notebook.openNotebookDocument(uri);
await vscode.workspace.openNotebookDocument(uri);
await p;
});

test('open untitled notebook', async function () {
const nb = await vscode.notebook.openNotebookDocument('notebook.nbdserializer');
const nb = await vscode.workspace.openNotebookDocument('notebook.nbdserializer');
assert.strictEqual(nb.isUntitled, true);
assert.strictEqual(nb.isClosed, false);
assert.strictEqual(nb.uri.scheme, 'untitled');
// assert.strictEqual(nb.cellCount, 0); // NotebookSerializer ALWAYS returns something here
});

test('open untitled with data', async function () {
const nb = await vscode.notebook.openNotebookDocument(
const nb = await vscode.workspace.openNotebookDocument(
'notebook.nbdserializer',
new vscode.NotebookData([
new vscode.NotebookCellData(vscode.NotebookCellKind.Code, 'console.log()', 'javascript'),
Expand All @@ -162,7 +162,7 @@ suite('Notebook Document', function () {
test('workspace edit API (replaceCells)', async function () {
const uri = await utils.createRandomFile(undefined, undefined, '.nbdtest');

const document = await vscode.notebook.openNotebookDocument(uri);
const document = await vscode.workspace.openNotebookDocument(uri);
assert.strictEqual(document.cellCount, 1);

// inserting two new cells
Expand Down Expand Up @@ -237,7 +237,7 @@ suite('Notebook Document', function () {

test('workspace edit API (replaceCells, event)', async function () {
const uri = await utils.createRandomFile(undefined, undefined, '.nbdtest');
const document = await vscode.notebook.openNotebookDocument(uri);
const document = await vscode.workspace.openNotebookDocument(uri);
assert.strictEqual(document.cellCount, 1);

const edit = new vscode.WorkspaceEdit();
Expand Down Expand Up @@ -279,7 +279,7 @@ suite('Notebook Document', function () {

test('document save API', async function () {
const uri = await utils.createRandomFile(undefined, undefined, '.nbdtest');
const notebook = await vscode.notebook.openNotebookDocument(uri);
const notebook = await vscode.workspace.openNotebookDocument(uri);

assert.strictEqual(notebook.uri.toString(), uri.toString());
assert.strictEqual(notebook.isDirty, false);
Expand Down Expand Up @@ -314,7 +314,7 @@ suite('Notebook Document', function () {
test('setTextDocumentLanguage for notebook cells', async function () {

const uri = await utils.createRandomFile(undefined, undefined, '.nbdtest');
const notebook = await vscode.notebook.openNotebookDocument(uri);
const notebook = await vscode.workspace.openNotebookDocument(uri);
const first = notebook.cellAt(0);
assert.strictEqual(first.document.languageId, 'javascript');

Expand All @@ -334,7 +334,7 @@ suite('Notebook Document', function () {

test('setTextDocumentLanguage when notebook editor is not open', async function () {
const uri = await utils.createRandomFile('', undefined, '.nbdtest');
const notebook = await vscode.notebook.openNotebookDocument(uri);
const notebook = await vscode.workspace.openNotebookDocument(uri);
const firstCelUri = notebook.cellAt(0).document.uri;
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');

Expand All @@ -345,7 +345,7 @@ suite('Notebook Document', function () {

test('dirty state - complex', async function () {
const resource = await utils.createRandomFile(undefined, undefined, '.nbdtest');
const document = await vscode.notebook.openNotebookDocument(resource);
const document = await vscode.workspace.openNotebookDocument(resource);
assert.strictEqual(document.isDirty, false);

const edit = new vscode.WorkspaceEdit();
Expand All @@ -360,7 +360,7 @@ suite('Notebook Document', function () {

test('dirty state - serializer', async function () {
const resource = await utils.createRandomFile(undefined, undefined, '.nbdserializer');
const document = await vscode.notebook.openNotebookDocument(resource);
const document = await vscode.workspace.openNotebookDocument(resource);
assert.strictEqual(document.isDirty, false);

const edit = new vscode.WorkspaceEdit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ suite('Notebook Editor', function () {
utils.disposeAll(disposables);
disposables.length = 0;

for (let doc of vscode.notebook.notebookDocuments) {
for (let doc of vscode.workspace.notebookDocuments) {
assert.strictEqual(doc.isDirty, false, doc.uri.toString());
}
});

suiteSetup(function () {
disposables.push(vscode.notebook.registerNotebookSerializer('notebook.nbdtest', contentSerializer));
disposables.push(vscode.workspace.registerNotebookSerializer('notebook.nbdtest', contentSerializer));
});


test('showNotebookDocment', async function () {

const p = utils.asPromise(vscode.notebook.onDidOpenNotebookDocument);
const p = utils.asPromise(vscode.workspace.onDidOpenNotebookDocument);
const uri = await utils.createRandomFile(undefined, undefined, '.nbdtest');

const editor = await vscode.window.showNotebookDocument(uri);
Expand All @@ -50,7 +50,7 @@ suite('Notebook Editor', function () {
const event = await p;
assert.strictEqual(event.uri.toString(), uri.toString());

const includes = vscode.notebook.notebookDocuments.includes(editor.document);
const includes = vscode.workspace.notebookDocuments.includes(editor.document);
assert.strictEqual(true, includes);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function createRandomNotebookFile() {

async function openRandomNotebookDocument() {
const uri = await createRandomNotebookFile();
return vscode.notebook.openNotebookDocument(uri);
return vscode.workspace.openNotebookDocument(uri);
}

async function saveAllFilesAndCloseAll() {
Expand Down Expand Up @@ -104,7 +104,7 @@ suite('Notebook API tests', function () {
});

suiteSetup(function () {
suiteDisposables.push(vscode.notebook.registerNotebookContentProvider('notebookCoreTest', {
suiteDisposables.push(vscode.workspace.registerNotebookContentProvider('notebookCoreTest', {
openNotebook: async (resource: vscode.Uri): Promise<vscode.NotebookData> => {
if (/.*empty\-.*\.vsctestnb$/.test(resource.path)) {
return {
Expand Down Expand Up @@ -166,7 +166,7 @@ suite('Notebook API tests', function () {

kernel1 = new Kernel('mainKernel', 'Notebook Primary Test Kernel');

const listener = vscode.notebook.onDidOpenNotebookDocument(async notebook => {
const listener = vscode.workspace.onDidOpenNotebookDocument(async notebook => {
if (notebook.notebookType === kernel1.controller.notebookType) {
await vscode.commands.executeCommand('notebook.selectKernel', {
extension: 'vscode.vscode-api-tests',
Expand Down Expand Up @@ -204,7 +204,7 @@ suite('Notebook API tests', function () {

test('shared document in notebook editors', async function () {
let counter = 0;
testDisposables.push(vscode.notebook.onDidOpenNotebookDocument(() => {
testDisposables.push(vscode.workspace.onDidOpenNotebookDocument(() => {
counter++;
}));

Expand Down Expand Up @@ -1149,7 +1149,7 @@ suite('Notebook API tests', function () {

test('#115855 onDidSaveNotebookDocument', async function () {
const resource = await createRandomNotebookFile();
const notebook = await vscode.notebook.openNotebookDocument(resource);
const notebook = await vscode.workspace.openNotebookDocument(resource);
const editor = await vscode.window.showNotebookDocument(notebook);

const cellsChangeEvent = asPromise<vscode.NotebookCellsChangeEvent>(vscode.notebook.onDidChangeNotebookCells);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ suite('vscode', function () {
});

test('no rpc, createNotebookEditorDecorationType(...)', function () {
const item = vscode.notebook.createNotebookEditorDecorationType({ top: {} });
const item = vscode.notebooks.createNotebookEditorDecorationType({ top: {} });
dispo.push(item);
assertNoRpcFromEntry([item, 'NotebookEditorDecorationType']);
});

test('no rpc, createNotebookController(...)', function () {
const ctrl = vscode.notebook.createNotebookController('foo', 'bar', '');
const ctrl = vscode.notebooks.createNotebookController('foo', 'bar', '');
dispo.push(ctrl);
assertNoRpcFromEntry([ctrl, 'NotebookController']);
});
Expand Down
4 changes: 2 additions & 2 deletions extensions/vscode-notebook-tests/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function activate(context: vscode.ExtensionContext): any {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.file(notebookPath));
}));

context.subscriptions.push(vscode.notebook.registerNotebookContentProvider('notebookSmokeTest', {
context.subscriptions.push(vscode.workspace.registerNotebookContentProvider('notebookSmokeTest', {
openNotebook: async (_resource: vscode.Uri) => {
const dto: vscode.NotebookData = {
metadata: {},
Expand Down Expand Up @@ -58,7 +58,7 @@ export function activate(context: vscode.ExtensionContext): any {
}
}));

const controller = vscode.notebook.createNotebookController(
const controller = vscode.notebooks.createNotebookController(
'notebookSmokeTest',
'notebookSmokeTest',
'notebookSmokeTest'
Expand Down
124 changes: 59 additions & 65 deletions src/vs/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10639,6 +10639,65 @@ declare module 'vscode' {
*/
export const onDidSaveTextDocument: Event<TextDocument>;

/**
* All notebook documents currently known to the editor.
*/
export const notebookDocuments: readonly NotebookDocument[];

/**
* Open a notebook. Will return early if this notebook is already {@link notebook.notebookDocuments loaded}. Otherwise
* the notebook is loaded and the {@link notebook.onDidOpenNotebookDocument `onDidOpenNotebookDocument`}-event fires.
*
* *Note* that the lifecycle of the returned notebook is owned by the editor and not by the extension. That means an
* {@link notebook.onDidCloseNotebookDocument `onDidCloseNotebookDocument`}-event can occur at any time after.
*
* *Note* that opening a notebook does not show a notebook editor. This function only returns a notebook document which
* can be showns in a notebook editor but it can also be used for other things.
*
* @param uri The resource to open.
* @returns A promise that resolves to a {@link NotebookDocument notebook}
*/
export function openNotebookDocument(uri: Uri): Thenable<NotebookDocument>;

/**
* Open an untitled notebook. The editor will prompt the user for a file
* path when the document is to be saved.
*
* @see {@link openNotebookDocument}
* @param notebookType The notebook type that should be used.
* @param content The initial contents of the notebook.
* @returns A promise that resolves to a {@link NotebookDocument notebook}.
*/
export function openNotebookDocument(notebookType: string, content?: NotebookData): Thenable<NotebookDocument>;

/**
* Register a {@link NotebookSerializer notebook serializer}.
*
* A notebook serializer must be contributed through the `notebooks` extension point. When opening a notebook file, the editor will send
* the `onNotebook:<notebookType>` activation event, and extensions must register their serializer in return.
*
* @param notebookType A notebook.
* @param serializer A notebook serialzier.
* @param options Optional context options that define what parts of a notebook should be persisted
* @return A {@link Disposable} that unregisters this serializer when being disposed.
*/
export function registerNotebookSerializer(notebookType: string, serializer: NotebookSerializer, options?: NotebookDocumentContentOptions): Disposable;

/**
* An event that is emitted when a {@link NotebookDocument notebook} is opened.
*/
export const onDidOpenNotebookDocument: Event<NotebookDocument>;

/**
* An event that is emitted when a {@link NotebookDocument notebook} is disposed.
*
* *Note 1:* There is no guarantee that this event fires when an editor tab is closed.
*
* *Note 2:* A notebook can be open but not shown in an editor which means this event can fire
* for a notebook that has not been shown in an editor.
*/
export const onDidCloseNotebookDocument: Event<NotebookDocument>;

/**
* An event that is emitted when files are being created.
*
Expand Down Expand Up @@ -12102,71 +12161,6 @@ declare module 'vscode' {
// todo@api what should be in this namespace? should notebookDocuments and friends be in the workspace namespace?
export namespace notebooks {

/**
* All notebook documents currently known to the editor.
*/
// todo@api move to workspace
export const notebookDocuments: readonly NotebookDocument[];

/**
* Open a notebook. Will return early if this notebook is already {@link notebook.notebookDocuments loaded}. Otherwise
* the notebook is loaded and the {@link notebook.onDidOpenNotebookDocument `onDidOpenNotebookDocument`}-event fires.
*
* *Note* that the lifecycle of the returned notebook is owned by the editor and not by the extension. That means an
* {@link notebook.onDidCloseNotebookDocument `onDidCloseNotebookDocument`}-event can occur at any time after.
*
* *Note* that opening a notebook does not show a notebook editor. This function only returns a notebook document which
* can be showns in a notebook editor but it can also be used for other things.
*
* @param uri The resource to open.
* @returns A promise that resolves to a {@link NotebookDocument notebook}
*/
// todo@api move to workspace
export function openNotebookDocument(uri: Uri): Thenable<NotebookDocument>;

/**
* Open an untitled notebook. The editor will prompt the user for a file
* path when the document is to be saved.
*
* @see {@link openNotebookDocument}
* @param notebookType The notebook type that should be used.
* @param content The initial contents of the notebook.
* @returns A promise that resolves to a {@link NotebookDocument notebook}.
*/
// todo@api move to workspace
export function openNotebookDocument(notebookType: string, content?: NotebookData): Thenable<NotebookDocument>;

/**
* An event that is emitted when a {@link NotebookDocument notebook} is opened.
*/
// todo@api move to workspace
export const onDidOpenNotebookDocument: Event<NotebookDocument>;

/**
* An event that is emitted when a {@link NotebookDocument notebook} is disposed.
*
* *Note 1:* There is no guarantee that this event fires when an editor tab is closed.
*
* *Note 2:* A notebook can be open but not shown in an editor which means this event can fire
* for a notebook that has not been shown in an editor.
*/
// todo@api move to workspace
export const onDidCloseNotebookDocument: Event<NotebookDocument>;

/**
* Register a {@link NotebookSerializer notebook serializer}.
*
* A notebook serializer must be contributed through the `notebooks` extension point. When opening a notebook file, the editor will send
* the `onNotebook:<notebookType>` activation event, and extensions must register their serializer in return.
*
* @param notebookType A notebook.
* @param serializer A notebook serialzier.
* @param options Optional context options that define what parts of a notebook should be persisted
* @return A {@link Disposable} that unregisters this serializer when being disposed.
*/
// todo@api move to workspace
export function registerNotebookSerializer(notebookType: string, serializer: NotebookSerializer, options?: NotebookDocumentContentOptions): Disposable;

/**
* Creates a new notebook controller.
*
Expand Down
Loading

0 comments on commit ac05ae7

Please sign in to comment.