Skip to content

Commit f128b3d

Browse files
authored
Update to the latest Notebook API (#15416) (#15602)
1 parent 39d6a21 commit f128b3d

File tree

15 files changed

+1529
-1230
lines changed

15 files changed

+1529
-1230
lines changed

news/3 Code Health/15415.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated to used the latest Notebook (proposed) API.

news/3 Code Health/15573.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix failing smoke tests on CI.

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"theme": "dark"
2929
},
3030
"engines": {
31-
"vscode": "^1.51.0"
31+
"vscode": "^1.54.0"
3232
},
3333
"keywords": [
3434
"python",
@@ -2082,7 +2082,7 @@
20822082
"@types/tmp": "0.0.33",
20832083
"@types/untildify": "^3.0.0",
20842084
"@types/uuid": "^3.4.3",
2085-
"@types/vscode": "~1.51.0",
2085+
"@types/vscode": "~1.53.0",
20862086
"@types/webpack-bundle-analyzer": "^2.13.0",
20872087
"@types/winreg": "^1.2.30",
20882088
"@types/xml2js": "^0.4.2",

src/client/common/application/notebook.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
NotebookEditor,
1515
NotebookKernel,
1616
NotebookKernelProvider,
17+
window,
1718
} from 'vscode-proposed';
1819
import { UseProposedApi } from '../constants';
1920
import { IDisposableRegistry } from '../types';
@@ -34,7 +35,7 @@ export class VSCodeNotebook implements IVSCodeNotebook {
3435
}
3536
public get onDidChangeActiveNotebookEditor(): Event<NotebookEditor | undefined> {
3637
return this.canUseNotebookApi
37-
? this.notebook.onDidChangeActiveNotebookEditor
38+
? this.window.onDidChangeActiveNotebookEditor
3839
: new EventEmitter<NotebookEditor | undefined>().event;
3940
}
4041
public get onDidOpenNotebookDocument(): Event<NotebookDocument> {
@@ -56,7 +57,7 @@ export class VSCodeNotebook implements IVSCodeNotebook {
5657
return this.canUseNotebookApi ? this.notebook.notebookDocuments : [];
5758
}
5859
public get notebookEditors() {
59-
return this.canUseNotebookApi ? this.notebook.visibleNotebookEditors : [];
60+
return this.canUseNotebookApi ? this.window.visibleNotebookEditors : [];
6061
}
6162
public get onDidChangeNotebookDocument(): Event<NotebookCellChangedEvent> {
6263
return this.canUseNotebookApi
@@ -67,17 +68,24 @@ export class VSCodeNotebook implements IVSCodeNotebook {
6768
if (!this.useProposedApi) {
6869
return;
6970
}
70-
return this.notebook.activeNotebookEditor;
71+
return this.window.activeNotebookEditor;
7172
}
7273
private get notebook() {
7374
if (!this._notebook) {
7475
this._notebook = require('vscode').notebook;
7576
}
7677
return this._notebook!;
7778
}
79+
private get window() {
80+
if (!this._window) {
81+
this._window = require('vscode').window;
82+
}
83+
return this._window!;
84+
}
7885
private readonly _onDidChangeNotebookDocument = new EventEmitter<NotebookCellChangedEvent>();
7986
private addedEventHandlers?: boolean;
8087
private _notebook?: typeof notebook;
88+
private _window?: typeof window;
8189
private readonly canUseNotebookApi?: boolean;
8290
private readonly handledCellChanges = new WeakSet<VSCNotebookCellsChangeEvent>();
8391
constructor(

src/client/jupyter/languageserver/notebookConcatDocument.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { isEqual } from 'lodash';
2020
import { NotebookConcatTextDocument, NotebookCell, NotebookDocument } from 'vscode-proposed';
2121
import { IVSCodeNotebook } from '../../common/application/types';
2222
import { IDisposable } from '../../common/types';
23+
import { PYTHON_LANGUAGE } from '../../common/constants';
2324

2425
export const NotebookConcatPrefix = '_NotebookConcat_';
2526

@@ -44,7 +45,16 @@ export class NotebookConcatDocument implements TextDocument, IDisposable {
4445
}
4546

4647
public get languageId(): string {
47-
return this.notebook.languages[0];
48+
// eslint-disable-next-line global-require
49+
const { NotebookCellKind } = require('vscode');
50+
// Return Python if we have python cells.
51+
if (this.notebook.cells.some((item) => item.language.toLowerCase() === PYTHON_LANGUAGE.toLowerCase())) {
52+
return PYTHON_LANGUAGE;
53+
}
54+
// Return the language of the first available cell, else assume its a Python notebook.
55+
// The latter is not possible, except for the case where we have all markdown cells,
56+
// in which case the language server will never kick in.
57+
return this.notebook.cells.find((item) => item.cellKind === NotebookCellKind.Code)?.language || PYTHON_LANGUAGE;
4858
}
4959

5060
public get version(): number {

src/test/insiders/languageServer.insiders.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ suite('Insiders Test: Language Server', () => {
9797
expect(locations![0].uri.fsPath).to.contain(path.basename(notebookDefinitions));
9898

9999
// Insert a new cell
100-
const activeEditor = vscode.notebook.activeNotebookEditor;
100+
const activeEditor = vscode.window.activeNotebookEditor;
101101
expect(activeEditor).not.to.be.equal(undefined, 'Active editor not found in notebook');
102102
await activeEditor!.edit((edit) => {
103103
edit.replaceCells(0, 0, [
104104
{
105-
cellKind: vscode.CellKind.Code,
105+
cellKind: vscode.NotebookCellKind.Code,
106106
language: PYTHON_LANGUAGE,
107107
source: 'x = 4',
108108
metadata: {
@@ -125,7 +125,7 @@ suite('Insiders Test: Language Server', () => {
125125
edit.replaceCells(0, 1, []);
126126
edit.replaceCells(1, 0, [
127127
{
128-
cellKind: vscode.CellKind.Code,
128+
cellKind: vscode.NotebookCellKind.Code,
129129
language: PYTHON_LANGUAGE,
130130
source: 'x = 4',
131131
metadata: {

src/test/mockClasses.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export class MockOutputChannel implements vscode.OutputChannel {
3838
}
3939

4040
export class MockStatusBarItem implements vscode.StatusBarItem {
41+
backgroundColor: vscode.ThemeColor | undefined;
42+
accessibilityInformation?: vscode.AccessibilityInformation | undefined;
4143
public alignment!: vscode.StatusBarAlignment;
4244
public priority!: number;
4345
public text!: string;

src/test/mocks/vsc/extHostedTypes.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { vscUri } from './uri';
1414
import { generateUuid } from './uuid';
1515

1616
export namespace vscMockExtHostedTypes {
17-
export enum CellKind {
17+
export enum NotebookCellKind {
1818
Markdown = 1,
1919
Code = 2,
2020
}
@@ -544,6 +544,32 @@ export namespace vscMockExtHostedTypes {
544544
}
545545

546546
export class WorkspaceEdit implements vscode.WorkspaceEdit {
547+
appendNotebookCellOutput(
548+
_uri: vscode.Uri,
549+
_index: number,
550+
_outputs: vscode.NotebookCellOutput[],
551+
_metadata?: vscode.WorkspaceEditEntryMetadata,
552+
): void {
553+
// Noop
554+
}
555+
replaceNotebookCellOutputItems(
556+
_uri: vscode.Uri,
557+
_index: number,
558+
_outputId: string,
559+
_items: vscode.NotebookCellOutputItem[],
560+
_metadata?: vscode.WorkspaceEditEntryMetadata,
561+
): void {
562+
// Noop
563+
}
564+
appendNotebookCellOutputItems(
565+
_uri: vscode.Uri,
566+
_index: number,
567+
_outputId: string,
568+
_items: vscode.NotebookCellOutputItem[],
569+
_metadata?: vscode.WorkspaceEditEntryMetadata,
570+
): void {
571+
// Noop
572+
}
547573
replaceNotebookMetadata(_uri: vscode.Uri, _value: vscode.NotebookDocumentMetadata): void {
548574
//
549575
}
@@ -560,7 +586,7 @@ export namespace vscMockExtHostedTypes {
560586
replaceNotebookCellOutput(
561587
_uri: vscode.Uri,
562588
_index: number,
563-
_outputs: vscode.CellOutput[],
589+
_outputs: vscode.NotebookCellOutput[],
564590
_metadata?: vscode.WorkspaceEditEntryMetadata,
565591
): void {
566592
// Noop.

src/test/smoke/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function enableJedi(enable: boolean | undefined): Promise<void> {
4949
export async function openNotebook(file: string): Promise<vscode.NotebookEditor> {
5050
await verifyExtensionIsAvailable(JUPYTER_EXTENSION_ID);
5151
await vscode.commands.executeCommand('vscode.openWith', vscode.Uri.file(file), 'jupyter-notebook');
52-
const notebook = vscode.notebook.activeNotebookEditor;
52+
const notebook = vscode.window.activeNotebookEditor;
5353
assert(notebook, 'Notebook did not open');
5454
return notebook;
5555
}

src/test/smoke/datascience.smoke.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as vscode from 'vscode';
1010
import { JUPYTER_EXTENSION_ID } from '../../client/common/constants';
1111
import { openFile, setAutoSaveDelayInWorkspaceRoot, waitForCondition } from '../common';
1212
import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_SMOKE_TEST } from '../constants';
13-
import { noop, sleep } from '../core';
13+
import { sleep } from '../core';
1414
import { closeActiveWindows, initialize, initializeTest } from '../initialize';
1515
import { verifyExtensionIsAvailable } from './common';
1616

@@ -75,8 +75,8 @@ suite('Smoke Test: Datascience', () => {
7575
if (await fs.pathExists(outputFile)) {
7676
await fs.unlink(outputFile);
7777
}
78-
// Ignore exceptions (as native editor closes the document as soon as its opened);
79-
await openFile(file).catch(noop);
78+
79+
await vscode.commands.executeCommand('jupyter.opennotebook', vscode.Uri.file(file));
8080

8181
// Wait for 15 seconds for notebook to launch.
8282
// Unfortunately there's no way to know for sure it has completely loaded.

src/test/vscode-mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ mockedVSCode.QuickInputButtons = vscodeMocks.vscMockExtHostedTypes.QuickInputBut
106106
mockedVSCode.FileType = vscodeMocks.vscMock.FileType;
107107
mockedVSCode.UIKind = vscodeMocks.vscMock.UIKind;
108108
mockedVSCode.FileSystemError = vscodeMocks.vscMockExtHostedTypes.FileSystemError;
109-
(mockedVSCode as any).CellKind = vscodeMocks.vscMockExtHostedTypes.CellKind;
109+
(mockedVSCode as any).NotebookCellKind = vscodeMocks.vscMockExtHostedTypes.NotebookCellKind;
110110
(mockedVSCode as any).CellOutputKind = vscodeMocks.vscMockExtHostedTypes.CellOutputKind;
111111
(mockedVSCode as any).NotebookCellRunState = vscodeMocks.vscMockExtHostedTypes.NotebookCellRunState;
112112

0 commit comments

Comments
 (0)