Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/3 Code Health/12025.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure we can use proposed VS Code API with `ts-node`.
49 changes: 26 additions & 23 deletions src/client/common/application/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
// Licensed under the MIT License.

import { inject, injectable } from 'inversify';
import {
Disposable,
Event,
EventEmitter,
GlobPattern,
import { Disposable, Event, EventEmitter, GlobPattern, TextDocument, window } from 'vscode';
import type {
notebook,
NotebookContentProvider,
NotebookDocument,
NotebookEditor,
NotebookKernel,
NotebookOutputRenderer,
NotebookOutputSelector,
TextDocument,
window
} from 'vscode';
NotebookOutputSelector
} from 'vscode-proposed';
import { UseProposedApi } from '../constants';
import { IDisposableRegistry } from '../types';
import {
Expand All @@ -36,13 +31,13 @@ export class VSCodeNotebook implements IVSCodeNotebook {
| NotebookCellLanguageChangeEvent
>();
public get onDidOpenNotebookDocument(): Event<NotebookDocument> {
return notebook.onDidOpenNotebookDocument;
return this.notebook.onDidOpenNotebookDocument;
}
public get onDidCloseNotebookDocument(): Event<NotebookDocument> {
return notebook.onDidCloseNotebookDocument;
return this.notebook.onDidCloseNotebookDocument;
}
public get notebookEditors() {
return notebook.visibleNotebookEditors;
return this.notebook.visibleNotebookEditors;
}
public get onDidChangeNotebookDocument(): Event<
| NotebookCellsChangeEvent
Expand All @@ -64,36 +59,44 @@ export class VSCodeNotebook implements IVSCodeNotebook {
}
// Temporary, currently VSC API doesn't work well.
// `notebook.activeNotebookEditor` is not reset when opening another file.
if (!notebook.activeNotebookEditor) {
if (!this.notebook.activeNotebookEditor) {
return;
}
// If we have a text editor opened and it is not a cell, then we know for certain a notebook is not open.
if (window.activeTextEditor && !this.isCell(window.activeTextEditor.document)) {
return;
}
// Temporary until VSC API stabilizes.
if (Array.isArray(notebook.visibleNotebookEditors)) {
return notebook.visibleNotebookEditors.find((item) => item.active && item.visible);
if (Array.isArray(this.notebook.visibleNotebookEditors)) {
return this.notebook.visibleNotebookEditors.find((item) => item.active && item.visible);
}
return notebook.activeNotebookEditor;
return this.notebook.activeNotebookEditor;
}
private addedEventHandlers?: boolean;
private _notebook?: typeof notebook;
private get notebook() {
if (!this._notebook) {
// tslint:disable-next-line: no-require-imports
this._notebook = require('vscode').notebook;
}
return this._notebook!;
}
constructor(
@inject(UseProposedApi) private readonly useProposedApi: boolean,
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry
) {}
public registerNotebookContentProvider(notebookType: string, provider: NotebookContentProvider): Disposable {
return notebook.registerNotebookContentProvider(notebookType, provider);
return this.notebook.registerNotebookContentProvider(notebookType, provider);
}
public registerNotebookKernel(id: string, selectors: GlobPattern[], kernel: NotebookKernel): Disposable {
return notebook.registerNotebookKernel(id, selectors, kernel);
return this.notebook.registerNotebookKernel(id, selectors, kernel);
}
public registerNotebookOutputRenderer(
id: string,
outputSelector: NotebookOutputSelector,
renderer: NotebookOutputRenderer
): Disposable {
return notebook.registerNotebookOutputRenderer(id, outputSelector, renderer);
return this.notebook.registerNotebookOutputRenderer(id, outputSelector, renderer);
}
public isCell(textDocument: TextDocument) {
return (
Expand All @@ -109,16 +112,16 @@ export class VSCodeNotebook implements IVSCodeNotebook {
}
this.disposables.push(
...[
notebook.onDidChangeCellLanguage((e) =>
this.notebook.onDidChangeCellLanguage((e) =>
this._onDidChangeNotebookDocument.fire({ ...e, type: 'changeCellLanguage' })
),
notebook.onDidChangeCellOutputs((e) =>
this.notebook.onDidChangeCellOutputs((e) =>
this._onDidChangeNotebookDocument.fire({ ...e, type: 'changeCellOutputs' })
),
notebook.onDidChangeNotebookCells((e) =>
this.notebook.onDidChangeNotebookCells((e) =>
this._onDidChangeNotebookDocument.fire({ ...e, type: 'changeCells' })
),
notebook.onDidMoveNotebookCell((e) =>
this.notebook.onDidMoveNotebookCell((e) =>
this._onDidChangeNotebookDocument.fire({ ...e, type: 'moveCell' })
)
]
Expand Down
22 changes: 12 additions & 10 deletions src/client/common/application/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ import {
InputBoxOptions,
MessageItem,
MessageOptions,
NotebookCellLanguageChangeEvent as VSCNotebookCellLanguageChangeEvent,
NotebookCellMoveEvent as VSCNotebookCellMoveEvent,
NotebookCellOutputsChangeEvent as VSCNotebookCellOutputsChangeEvent,
NotebookCellsChangeEvent as VSCNotebookCellsChangeEvent,
NotebookContentProvider,
NotebookDocument,
NotebookEditor,
NotebookKernel,
NotebookOutputRenderer,
NotebookOutputSelector,
OpenDialogOptions,
OutputChannel,
Progress,
Expand Down Expand Up @@ -68,6 +58,18 @@ import {
WorkspaceFolderPickOptions,
WorkspaceFoldersChangeEvent
} from 'vscode';
import type {
NotebookCellLanguageChangeEvent as VSCNotebookCellLanguageChangeEvent,
NotebookCellMoveEvent as VSCNotebookCellMoveEvent,
NotebookCellOutputsChangeEvent as VSCNotebookCellOutputsChangeEvent,
NotebookCellsChangeEvent as VSCNotebookCellsChangeEvent,
NotebookContentProvider,
NotebookDocument,
NotebookEditor,
NotebookKernel,
NotebookOutputRenderer,
NotebookOutputSelector
} from 'vscode-proposed';
import * as vsls from 'vsls/vscode';

import { IAsyncDisposable, Resource } from '../types';
Expand Down
2 changes: 1 addition & 1 deletion src/client/datascience/notebook/cellUpdateHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

'use strict';

import { NotebookCell, NotebookDocument } from 'vscode';
import type { NotebookCell, NotebookDocument } from 'vscode-proposed';
import { IDisposable } from '../../common/types';
import { traceError } from '../../logging';
import { ICell, INotebookModel } from '../types';
Expand Down
10 changes: 4 additions & 6 deletions src/client/datascience/notebook/contentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
'use strict';

import { inject, injectable } from 'inversify';
import {
CancellationToken,
EventEmitter,
import { CancellationToken, EventEmitter, Uri } from 'vscode';
import type {
NotebookContentProvider as VSCodeNotebookContentProvider,
NotebookData,
NotebookDocument,
NotebookDocumentEditEvent,
Uri
} from 'vscode';
NotebookDocumentEditEvent
} from 'vscode-proposed';
import { INotebookStorageProvider } from '../interactive-ipynb/notebookStorageProvider';
import { notebookModelToVSCNotebookData } from './helpers';

Expand Down
14 changes: 8 additions & 6 deletions src/client/datascience/notebook/executionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import { nbformat } from '@jupyterlab/coreutils';
import { inject, injectable } from 'inversify';
import { Subscription } from 'rxjs';
import { CancellationToken, NotebookCell, NotebookCellRunState, NotebookDocument } from 'vscode';
import { CancellationTokenSource } from 'vscode-jsonrpc';
import { CancellationToken, CancellationTokenSource } from 'vscode';
import type { NotebookCell, NotebookDocument } from 'vscode-proposed';
import { ICommandManager } from '../../common/application/types';
import { wrapCancellationTokens } from '../../common/cancellation';
import '../../common/extensions';
Expand All @@ -26,6 +26,8 @@ import {
updateCellWithErrorStatus
} from './executionHelpers';
import { INotebookExecutionService } from './types';
// tslint:disable-next-line: no-var-requires no-require-imports
const vscodeNotebookEnums = require('vscode') as typeof import('vscode-proposed');

/**
* VSC will use this class to execute cells in a notebook.
Expand Down Expand Up @@ -138,7 +140,7 @@ export class NotebookExecutionService implements INotebookExecutionService {
return;
}
deferred.resolve();
cell.metadata.runState = NotebookCellRunState.Idle;
cell.metadata.runState = vscodeNotebookEnums.NotebookCellRunState.Idle;

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

cell.metadata.runStartTime = new Date().getTime();
cell.metadata.runState = NotebookCellRunState.Running;
cell.metadata.runState = vscodeNotebookEnums.NotebookCellRunState.Running;

if (!findMappedNotebookCellModel(cell, model.cells)) {
// tslint:disable-next-line: no-suspicious-comment
Expand Down Expand Up @@ -181,8 +183,8 @@ export class NotebookExecutionService implements INotebookExecutionService {
},
() => {
cell.metadata.runState = wrappedToken.isCancellationRequested
? NotebookCellRunState.Idle
: NotebookCellRunState.Success;
? vscodeNotebookEnums.NotebookCellRunState.Idle
: vscodeNotebookEnums.NotebookCellRunState.Success;
cell.metadata.lastRunDuration = stopWatch.elapsedTime;
cell.metadata.statusMessage = '';
deferred.resolve();
Expand Down
22 changes: 11 additions & 11 deletions src/client/datascience/notebook/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
'use strict';

import { nbformat } from '@jupyterlab/coreutils';
import {
import type {
CellDisplayOutput,
CellErrorOutput,
CellKind,
CellOutput,
CellOutputKind,
CellStreamOutput,
NotebookCellData,
NotebookCellRunState,
NotebookData
} from 'vscode';
} from 'vscode-proposed';
// tslint:disable-next-line: no-var-requires no-require-imports
const vscodeNotebookEnums = require('vscode') as typeof import('vscode-proposed');
import { concatMultilineStringInput, concatMultilineStringOutput } from '../../../datascience-ui/common';
import { MARKDOWN_LANGUAGE, PYTHON_LANGUAGE } from '../../common/constants';
import { traceError, traceWarning } from '../../logging';
Expand Down Expand Up @@ -67,12 +66,13 @@ export function cellToVSCNotebookCellData(cell: ICell): NotebookCellData | undef
}

return {
cellKind: cell.data.cell_type === 'code' ? CellKind.Code : CellKind.Markdown,
cellKind:
cell.data.cell_type === 'code' ? vscodeNotebookEnums.CellKind.Code : vscodeNotebookEnums.CellKind.Markdown,
language: cell.data.cell_type === 'code' ? PYTHON_LANGUAGE : MARKDOWN_LANGUAGE,
metadata: {
editable: true,
executionOrder: typeof cell.data.execution_count === 'number' ? cell.data.execution_count : undefined,
runState: NotebookCellRunState.Idle,
runState: vscodeNotebookEnums.NotebookCellRunState.Idle,
runnable: cell.data.cell_type === 'code',
custom: {
cellId: cell.id
Expand Down Expand Up @@ -157,7 +157,7 @@ function translateDisplayDataOutput(output: nbformat.IDisplayData): CellDisplayO
] = `<div class='display' style="overflow:scroll;${divStyle}"><img src="${imgSrc}" ${imgStyle} ${height} ${width}></div>`;
}
return {
outputKind: CellOutputKind.Rich,
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
data
};
}
Expand All @@ -177,14 +177,14 @@ function translateStreamOutput(output: nbformat.IStream): CellStreamOutput | Cel
if (!hasAngleBrackets && !hasAnsiChars) {
// Plain text output.
return {
outputKind: CellOutputKind.Text,
outputKind: vscodeNotebookEnums.CellOutputKind.Text,
text
};
}

// Format the output, but ensure we have the plain text output as well.
const richOutput: CellDisplayOutput = {
outputKind: CellOutputKind.Rich,
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
data: {
['text/plain']: text
}
Expand Down Expand Up @@ -212,7 +212,7 @@ export function translateErrorOutput(output: nbformat.IError): CellErrorOutput {
return {
ename: output.ename,
evalue: output.evalue,
outputKind: CellOutputKind.Error,
outputKind: vscodeNotebookEnums.CellOutputKind.Error,
traceback: output.traceback
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/client/datascience/notebook/notebookEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

'use strict';

import { CellKind, ConfigurationTarget, Event, EventEmitter, NotebookDocument, Uri, WebviewPanel } from 'vscode';
import { CellKind, ConfigurationTarget, Event, EventEmitter, Uri, WebviewPanel } from 'vscode';
import type { NotebookDocument } from 'vscode-proposed';
import { IApplicationShell, ICommandManager, IVSCodeNotebook } from '../../common/application/types';
import { PYTHON_LANGUAGE } from '../../common/constants';
import { IConfigurationService } from '../../common/types';
Expand Down
3 changes: 2 additions & 1 deletion src/client/datascience/notebook/notebookEditorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
'use strict';

import { inject, injectable } from 'inversify';
import { Event, EventEmitter, NotebookDocument, Uri } from 'vscode';
import { Event, EventEmitter, Uri } from 'vscode';
import type { NotebookDocument } from 'vscode-proposed';
import { IExtensionSingleActivationService } from '../../activation/types';
import { IApplicationShell, ICommandManager, IVSCodeNotebook, IWorkspaceService } from '../../common/application/types';
import '../../common/extensions';
Expand Down
3 changes: 2 additions & 1 deletion src/client/datascience/notebook/notebookKernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
'use strict';

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

/**
Expand Down
3 changes: 2 additions & 1 deletion src/client/datascience/notebook/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

'use strict';

import { CancellationToken, NotebookCell, NotebookDocument } from 'vscode';
import type { CancellationToken } from 'vscode';
import type { NotebookCell, NotebookDocument } from 'vscode-proposed';

export const INotebookExecutionService = Symbol('INotebookExecutionService');
export interface INotebookExecutionService {
Expand Down
15 changes: 9 additions & 6 deletions src/test/datascience/notebook/contentProvider.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

import { assert } from 'chai';
import { anything, instance, mock, when } from 'ts-mockito';
import { CellKind, NotebookCellRunState, NotebookContentProvider as VSCodeNotebookContentProvider, Uri } from 'vscode';
import { Uri } from 'vscode';
// tslint:disable-next-line: no-var-requires no-require-imports
const vscodeNotebookEnums = require('vscode') as typeof import('vscode-proposed');
import type { NotebookContentProvider as VSCodeNotebookContentProvider } from 'vscode-proposed';
import { MARKDOWN_LANGUAGE, PYTHON_LANGUAGE } from '../../../client/common/constants';
import { INotebookStorageProvider } from '../../../client/datascience/interactive-ipynb/notebookStorageProvider';
import { NotebookContentProvider } from '../../../client/datascience/notebook/contentProvider';
import { CellState, INotebookModel } from '../../../client/datascience/types';

// tslint:disable: no-any
suite('Data Science - NativeNotebook ContentProvider', () => {
let storageProvider: INotebookStorageProvider;
let contentProvider: VSCodeNotebookContentProvider;
Expand Down Expand Up @@ -57,29 +60,29 @@ suite('Data Science - NativeNotebook ContentProvider', () => {
assert.deepEqual(notebook.languages, [PYTHON_LANGUAGE, MARKDOWN_LANGUAGE]);
assert.deepEqual(notebook.cells, [
{
cellKind: CellKind.Code,
cellKind: (vscodeNotebookEnums as any).CellKind.Code,
language: PYTHON_LANGUAGE,
outputs: [],
source: 'print(1)',
metadata: {
editable: true,
executionOrder: 10,
runState: NotebookCellRunState.Idle,
runState: (vscodeNotebookEnums as any).NotebookCellRunState.Idle,
runnable: true,
custom: {
cellId: 'MyCellId1'
}
}
},
{
cellKind: CellKind.Markdown,
cellKind: (vscodeNotebookEnums as any).CellKind.Markdown,
language: MARKDOWN_LANGUAGE,
outputs: [],
source: '# HEAD',
metadata: {
editable: true,
executionOrder: undefined,
runState: NotebookCellRunState.Idle,
runState: (vscodeNotebookEnums as any).NotebookCellRunState.Idle,
runnable: false,
custom: {
cellId: 'MyCellId2'
Expand Down
Loading