forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotebook.ts
More file actions
48 lines (46 loc) · 2.3 KB
/
notebook.ts
File metadata and controls
48 lines (46 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable } from 'inversify';
import { DocumentSelector, Event, EventEmitter, workspace } from 'vscode';
import type { notebook, NotebookConcatTextDocument, NotebookDocument } from 'vscode-proposed';
import { UseProposedApi } from '../constants';
import { IApplicationEnvironment, IVSCodeNotebook } from './types';
@injectable()
export class VSCodeNotebook implements IVSCodeNotebook {
public get onDidOpenNotebookDocument(): Event<NotebookDocument> {
const onDidOpenNotebookDocument =
this.notebook.onDidOpenNotebookDocument ?? (workspace as any).onDidOpenNotebookDocument;
return this.canUseNotebookApi ? onDidOpenNotebookDocument : new EventEmitter<NotebookDocument>().event;
}
public get onDidCloseNotebookDocument(): Event<NotebookDocument> {
const onDidCloseNotebookDocument =
this.notebook.onDidCloseNotebookDocument ?? (workspace as any).onDidCloseNotebookDocument;
return this.canUseNotebookApi ? onDidCloseNotebookDocument : new EventEmitter<NotebookDocument>().event;
}
public get notebookDocuments(): ReadonlyArray<NotebookDocument> {
const notebookDocuments = this.notebook.notebookDocuments ?? (workspace as any).notebookDocuments;
return this.canUseNotebookApi ? notebookDocuments : [];
}
private get notebook() {
if (!this._notebook) {
this._notebook = require('vscode').notebook ?? require('vscode').notebooks;
}
return this._notebook!;
}
private _notebook?: typeof notebook;
private readonly canUseNotebookApi?: boolean;
constructor(
@inject(UseProposedApi) private readonly useProposedApi: boolean,
@inject(IApplicationEnvironment) readonly env: IApplicationEnvironment,
) {
if (this.useProposedApi) {
this.canUseNotebookApi = true;
}
}
public createConcatTextDocument(doc: NotebookDocument, selector?: DocumentSelector): NotebookConcatTextDocument {
if (this.useProposedApi) {
return this.notebook.createConcatTextDocument(doc, selector) as any; // Types of Position are different for some reason. Fix this later.
}
throw new Error('createConcatDocument not supported');
}
}