Skip to content

Commit 8c8efb8

Browse files
authored
Ensure we can use proposed VS Code API with ts-node (microsoft#12038)
* Use a proxy type for propsed types (for ts-node) * Oh yes `ts-node` sucks, cuz it doesn't work the same way as `tsc` compiler
1 parent d4749aa commit 8c8efb8

16 files changed

Lines changed: 904 additions & 104 deletions

File tree

news/3 Code Health/12025.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure we can use proposed VS Code API with `ts-node`.

src/client/common/application/notebook.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
// Licensed under the MIT License.
33

44
import { inject, injectable } from 'inversify';
5-
import {
6-
Disposable,
7-
Event,
8-
EventEmitter,
9-
GlobPattern,
5+
import { Disposable, Event, EventEmitter, GlobPattern, TextDocument, window } from 'vscode';
6+
import type {
107
notebook,
118
NotebookContentProvider,
129
NotebookDocument,
1310
NotebookEditor,
1411
NotebookKernel,
1512
NotebookOutputRenderer,
16-
NotebookOutputSelector,
17-
TextDocument,
18-
window
19-
} from 'vscode';
13+
NotebookOutputSelector
14+
} from 'vscode-proposed';
2015
import { UseProposedApi } from '../constants';
2116
import { IDisposableRegistry } from '../types';
2217
import {
@@ -36,13 +31,13 @@ export class VSCodeNotebook implements IVSCodeNotebook {
3631
| NotebookCellLanguageChangeEvent
3732
>();
3833
public get onDidOpenNotebookDocument(): Event<NotebookDocument> {
39-
return notebook.onDidOpenNotebookDocument;
34+
return this.notebook.onDidOpenNotebookDocument;
4035
}
4136
public get onDidCloseNotebookDocument(): Event<NotebookDocument> {
42-
return notebook.onDidCloseNotebookDocument;
37+
return this.notebook.onDidCloseNotebookDocument;
4338
}
4439
public get notebookEditors() {
45-
return notebook.visibleNotebookEditors;
40+
return this.notebook.visibleNotebookEditors;
4641
}
4742
public get onDidChangeNotebookDocument(): Event<
4843
| NotebookCellsChangeEvent
@@ -64,36 +59,44 @@ export class VSCodeNotebook implements IVSCodeNotebook {
6459
}
6560
// Temporary, currently VSC API doesn't work well.
6661
// `notebook.activeNotebookEditor` is not reset when opening another file.
67-
if (!notebook.activeNotebookEditor) {
62+
if (!this.notebook.activeNotebookEditor) {
6863
return;
6964
}
7065
// If we have a text editor opened and it is not a cell, then we know for certain a notebook is not open.
7166
if (window.activeTextEditor && !this.isCell(window.activeTextEditor.document)) {
7267
return;
7368
}
7469
// Temporary until VSC API stabilizes.
75-
if (Array.isArray(notebook.visibleNotebookEditors)) {
76-
return notebook.visibleNotebookEditors.find((item) => item.active && item.visible);
70+
if (Array.isArray(this.notebook.visibleNotebookEditors)) {
71+
return this.notebook.visibleNotebookEditors.find((item) => item.active && item.visible);
7772
}
78-
return notebook.activeNotebookEditor;
73+
return this.notebook.activeNotebookEditor;
7974
}
8075
private addedEventHandlers?: boolean;
76+
private _notebook?: typeof notebook;
77+
private get notebook() {
78+
if (!this._notebook) {
79+
// tslint:disable-next-line: no-require-imports
80+
this._notebook = require('vscode').notebook;
81+
}
82+
return this._notebook!;
83+
}
8184
constructor(
8285
@inject(UseProposedApi) private readonly useProposedApi: boolean,
8386
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry
8487
) {}
8588
public registerNotebookContentProvider(notebookType: string, provider: NotebookContentProvider): Disposable {
86-
return notebook.registerNotebookContentProvider(notebookType, provider);
89+
return this.notebook.registerNotebookContentProvider(notebookType, provider);
8790
}
8891
public registerNotebookKernel(id: string, selectors: GlobPattern[], kernel: NotebookKernel): Disposable {
89-
return notebook.registerNotebookKernel(id, selectors, kernel);
92+
return this.notebook.registerNotebookKernel(id, selectors, kernel);
9093
}
9194
public registerNotebookOutputRenderer(
9295
id: string,
9396
outputSelector: NotebookOutputSelector,
9497
renderer: NotebookOutputRenderer
9598
): Disposable {
96-
return notebook.registerNotebookOutputRenderer(id, outputSelector, renderer);
99+
return this.notebook.registerNotebookOutputRenderer(id, outputSelector, renderer);
97100
}
98101
public isCell(textDocument: TextDocument) {
99102
return (
@@ -109,16 +112,16 @@ export class VSCodeNotebook implements IVSCodeNotebook {
109112
}
110113
this.disposables.push(
111114
...[
112-
notebook.onDidChangeCellLanguage((e) =>
115+
this.notebook.onDidChangeCellLanguage((e) =>
113116
this._onDidChangeNotebookDocument.fire({ ...e, type: 'changeCellLanguage' })
114117
),
115-
notebook.onDidChangeCellOutputs((e) =>
118+
this.notebook.onDidChangeCellOutputs((e) =>
116119
this._onDidChangeNotebookDocument.fire({ ...e, type: 'changeCellOutputs' })
117120
),
118-
notebook.onDidChangeNotebookCells((e) =>
121+
this.notebook.onDidChangeNotebookCells((e) =>
119122
this._onDidChangeNotebookDocument.fire({ ...e, type: 'changeCells' })
120123
),
121-
notebook.onDidMoveNotebookCell((e) =>
124+
this.notebook.onDidMoveNotebookCell((e) =>
122125
this._onDidChangeNotebookDocument.fire({ ...e, type: 'moveCell' })
123126
)
124127
]

src/client/common/application/types.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ import {
2424
InputBoxOptions,
2525
MessageItem,
2626
MessageOptions,
27-
NotebookCellLanguageChangeEvent as VSCNotebookCellLanguageChangeEvent,
28-
NotebookCellMoveEvent as VSCNotebookCellMoveEvent,
29-
NotebookCellOutputsChangeEvent as VSCNotebookCellOutputsChangeEvent,
30-
NotebookCellsChangeEvent as VSCNotebookCellsChangeEvent,
31-
NotebookContentProvider,
32-
NotebookDocument,
33-
NotebookEditor,
34-
NotebookKernel,
35-
NotebookOutputRenderer,
36-
NotebookOutputSelector,
3727
OpenDialogOptions,
3828
OutputChannel,
3929
Progress,
@@ -68,6 +58,18 @@ import {
6858
WorkspaceFolderPickOptions,
6959
WorkspaceFoldersChangeEvent
7060
} from 'vscode';
61+
import type {
62+
NotebookCellLanguageChangeEvent as VSCNotebookCellLanguageChangeEvent,
63+
NotebookCellMoveEvent as VSCNotebookCellMoveEvent,
64+
NotebookCellOutputsChangeEvent as VSCNotebookCellOutputsChangeEvent,
65+
NotebookCellsChangeEvent as VSCNotebookCellsChangeEvent,
66+
NotebookContentProvider,
67+
NotebookDocument,
68+
NotebookEditor,
69+
NotebookKernel,
70+
NotebookOutputRenderer,
71+
NotebookOutputSelector
72+
} from 'vscode-proposed';
7173
import * as vsls from 'vsls/vscode';
7274

7375
import { IAsyncDisposable, Resource } from '../types';

src/client/datascience/notebook/cellUpdateHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
'use strict';
55

6-
import { NotebookCell, NotebookDocument } from 'vscode';
6+
import type { NotebookCell, NotebookDocument } from 'vscode-proposed';
77
import { IDisposable } from '../../common/types';
88
import { traceError } from '../../logging';
99
import { ICell, INotebookModel } from '../types';

src/client/datascience/notebook/contentProvider.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import {
8-
CancellationToken,
9-
EventEmitter,
7+
import { CancellationToken, EventEmitter, Uri } from 'vscode';
8+
import type {
109
NotebookContentProvider as VSCodeNotebookContentProvider,
1110
NotebookData,
1211
NotebookDocument,
13-
NotebookDocumentEditEvent,
14-
Uri
15-
} from 'vscode';
12+
NotebookDocumentEditEvent
13+
} from 'vscode-proposed';
1614
import { INotebookStorageProvider } from '../interactive-ipynb/notebookStorageProvider';
1715
import { notebookModelToVSCNotebookData } from './helpers';
1816

src/client/datascience/notebook/executionService.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import { nbformat } from '@jupyterlab/coreutils';
77
import { inject, injectable } from 'inversify';
88
import { Subscription } from 'rxjs';
9-
import { CancellationToken, NotebookCell, NotebookCellRunState, NotebookDocument } from 'vscode';
10-
import { CancellationTokenSource } from 'vscode-jsonrpc';
9+
import { CancellationToken, CancellationTokenSource } from 'vscode';
10+
import type { NotebookCell, NotebookDocument } from 'vscode-proposed';
1111
import { ICommandManager } from '../../common/application/types';
1212
import { wrapCancellationTokens } from '../../common/cancellation';
1313
import '../../common/extensions';
@@ -26,6 +26,8 @@ import {
2626
updateCellWithErrorStatus
2727
} from './executionHelpers';
2828
import { INotebookExecutionService } from './types';
29+
// tslint:disable-next-line: no-var-requires no-require-imports
30+
const vscodeNotebookEnums = require('vscode') as typeof import('vscode-proposed');
2931

3032
/**
3133
* VSC will use this class to execute cells in a notebook.
@@ -138,7 +140,7 @@ export class NotebookExecutionService implements INotebookExecutionService {
138140
return;
139141
}
140142
deferred.resolve();
141-
cell.metadata.runState = NotebookCellRunState.Idle;
143+
cell.metadata.runState = vscodeNotebookEnums.NotebookCellRunState.Idle;
142144

143145
// Interrupt kernel only if original cancellation was cancelled.
144146
if (token.isCancellationRequested) {
@@ -147,7 +149,7 @@ export class NotebookExecutionService implements INotebookExecutionService {
147149
});
148150

149151
cell.metadata.runStartTime = new Date().getTime();
150-
cell.metadata.runState = NotebookCellRunState.Running;
152+
cell.metadata.runState = vscodeNotebookEnums.NotebookCellRunState.Running;
151153

152154
if (!findMappedNotebookCellModel(cell, model.cells)) {
153155
// tslint:disable-next-line: no-suspicious-comment
@@ -181,8 +183,8 @@ export class NotebookExecutionService implements INotebookExecutionService {
181183
},
182184
() => {
183185
cell.metadata.runState = wrappedToken.isCancellationRequested
184-
? NotebookCellRunState.Idle
185-
: NotebookCellRunState.Success;
186+
? vscodeNotebookEnums.NotebookCellRunState.Idle
187+
: vscodeNotebookEnums.NotebookCellRunState.Success;
186188
cell.metadata.lastRunDuration = stopWatch.elapsedTime;
187189
cell.metadata.statusMessage = '';
188190
deferred.resolve();

src/client/datascience/notebook/helpers.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
'use strict';
55

66
import { nbformat } from '@jupyterlab/coreutils';
7-
import {
7+
import type {
88
CellDisplayOutput,
99
CellErrorOutput,
10-
CellKind,
1110
CellOutput,
12-
CellOutputKind,
1311
CellStreamOutput,
1412
NotebookCellData,
15-
NotebookCellRunState,
1613
NotebookData
17-
} from 'vscode';
14+
} from 'vscode-proposed';
15+
// tslint:disable-next-line: no-var-requires no-require-imports
16+
const vscodeNotebookEnums = require('vscode') as typeof import('vscode-proposed');
1817
import { concatMultilineStringInput, concatMultilineStringOutput } from '../../../datascience-ui/common';
1918
import { MARKDOWN_LANGUAGE, PYTHON_LANGUAGE } from '../../common/constants';
2019
import { traceError, traceWarning } from '../../logging';
@@ -67,12 +66,13 @@ export function cellToVSCNotebookCellData(cell: ICell): NotebookCellData | undef
6766
}
6867

6968
return {
70-
cellKind: cell.data.cell_type === 'code' ? CellKind.Code : CellKind.Markdown,
69+
cellKind:
70+
cell.data.cell_type === 'code' ? vscodeNotebookEnums.CellKind.Code : vscodeNotebookEnums.CellKind.Markdown,
7171
language: cell.data.cell_type === 'code' ? PYTHON_LANGUAGE : MARKDOWN_LANGUAGE,
7272
metadata: {
7373
editable: true,
7474
executionOrder: typeof cell.data.execution_count === 'number' ? cell.data.execution_count : undefined,
75-
runState: NotebookCellRunState.Idle,
75+
runState: vscodeNotebookEnums.NotebookCellRunState.Idle,
7676
runnable: cell.data.cell_type === 'code',
7777
custom: {
7878
cellId: cell.id
@@ -157,7 +157,7 @@ function translateDisplayDataOutput(output: nbformat.IDisplayData): CellDisplayO
157157
] = `<div class='display' style="overflow:scroll;${divStyle}"><img src="${imgSrc}" ${imgStyle} ${height} ${width}></div>`;
158158
}
159159
return {
160-
outputKind: CellOutputKind.Rich,
160+
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
161161
data
162162
};
163163
}
@@ -177,14 +177,14 @@ function translateStreamOutput(output: nbformat.IStream): CellStreamOutput | Cel
177177
if (!hasAngleBrackets && !hasAnsiChars) {
178178
// Plain text output.
179179
return {
180-
outputKind: CellOutputKind.Text,
180+
outputKind: vscodeNotebookEnums.CellOutputKind.Text,
181181
text
182182
};
183183
}
184184

185185
// Format the output, but ensure we have the plain text output as well.
186186
const richOutput: CellDisplayOutput = {
187-
outputKind: CellOutputKind.Rich,
187+
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
188188
data: {
189189
['text/plain']: text
190190
}
@@ -212,7 +212,7 @@ export function translateErrorOutput(output: nbformat.IError): CellErrorOutput {
212212
return {
213213
ename: output.ename,
214214
evalue: output.evalue,
215-
outputKind: CellOutputKind.Error,
215+
outputKind: vscodeNotebookEnums.CellOutputKind.Error,
216216
traceback: output.traceback
217217
};
218218
}

src/client/datascience/notebook/notebookEditor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
'use strict';
55

6-
import { CellKind, ConfigurationTarget, Event, EventEmitter, NotebookDocument, Uri, WebviewPanel } from 'vscode';
6+
import { CellKind, ConfigurationTarget, Event, EventEmitter, Uri, WebviewPanel } from 'vscode';
7+
import type { NotebookDocument } from 'vscode-proposed';
78
import { IApplicationShell, ICommandManager, IVSCodeNotebook } from '../../common/application/types';
89
import { PYTHON_LANGUAGE } from '../../common/constants';
910
import { IConfigurationService } from '../../common/types';

src/client/datascience/notebook/notebookEditorProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import { Event, EventEmitter, NotebookDocument, Uri } from 'vscode';
7+
import { Event, EventEmitter, Uri } from 'vscode';
8+
import type { NotebookDocument } from 'vscode-proposed';
89
import { IExtensionSingleActivationService } from '../../activation/types';
910
import { IApplicationShell, ICommandManager, IVSCodeNotebook, IWorkspaceService } from '../../common/application/types';
1011
import '../../common/extensions';

src/client/datascience/notebook/notebookKernel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import { CancellationToken, NotebookCell, NotebookDocument, NotebookKernel as VSCNotebookKernel, Uri } from 'vscode';
7+
import { CancellationToken, Uri } from 'vscode';
8+
import type { NotebookCell, NotebookDocument, NotebookKernel as VSCNotebookKernel } from 'vscode-proposed';
89
import { INotebookExecutionService } from './types';
910

1011
/**

0 commit comments

Comments
 (0)