Skip to content

Visual Studio Code, rebuilt in Rust

Rust
9.2 KB
Raw Blame

Notebooks

A notebook is a list of cells with outputs, opened by an extension's serializer, run by an extension's kernel, and drawn as a list of code editors over one webview. This file goes one level under ARCHITECTURE.md. Upstream is src/vs/workbench/contrib/notebook, contrib/interactive and contrib/replNotebook, with the mainThreadNotebook*.ts files of src/vs/workbench/api/browser. What each session ported and what differs from the reference is in port/notes.md, "Milestone 17: webviews and notebooks".

What is where

  • app/src/notebook.rs: the model. app/src/notebook.rs::Notebooks holds the open NotebookModels, the serializers, the kernels, one NotebookEditor per notebook tab, and what the host was last told. apply_edits is notebookTextModel.ts applyEdits. No GPUI.
  • app/src/notebook/kernels.rs: kernels, which kernel a notebook is bound to, cell executions. app/src/notebook/renderers.rs: contributed renderers and which mime type of an output shows. app/src/notebook/layout.rs: the height and top of every cell. app/src/notebook/outline.rs: outline entries and fold ranges. All four have no GPUI and carry their tests.
  • app/src/workbench/notebooks.rs: the main thread. handle_notebook_request answers the five MainThreadNotebook* proxies; open, save, backup, revert, the delta to the host, kernel execution.
  • app/src/workbench/notebook_editor.rs: the editor of a notebook tab: toolbar, cell list, the back layer webview, view state, undo. Its parts are in app/src/workbench/notebook_editor: cell toolbars and commands, outputs, find, folding, drag and drop, the overview ruler.
  • app/src/notebook/back_layer.html and app/src/notebook/webview_preloads.js: the page of the back layer webview. The preloads are upstream's webviewPreloads.ts.
  • app/src/workbench/interactive.rs and app/src/workbench/repl_notebook.rs: the interactive window and the REPL editor, both a notebook editor plus an input cell.

Who owns what

The extension host owns the file format and the execution. A serializer turns bytes into cells ($dataToNotebook) and cells into bytes ($notebookToData); the app never parses an .ipynb. A kernel runs cells and sends their outputs. A renderer's script runs in the webview, not in the host.

The app owns the notebook document: app/src/notebook.rs::NotebookModel is the truth for cells, metadata, outputs, the version and the dirty state, and the host holds a copy it learns of through events. The model lives in app/src/host_sync.rs::HostSync (notebooks), not in the workbench, because a cell's text change is a change of its notebook and the host must hear both from one place.

A cell's text is not in the model. Each cell has a workbench text model at its vscode-notebook-cell URI (app/src/notebook.rs::cell_uri: the notebook's URI with the cell's handle in the fragment). Cell has no source field. Code that needs a cell's text reads the text model (app/src/workbench/notebooks.rs::cell_text), and snapshot and added_data take a closure for it.

From a kernel to the view

  1. The kernel's extension calls MainThreadNotebookKernels.$updateExecution with outputs. app/src/workbench/notebooks.rs::update_execution turns the updates into cell edits.
  2. app/src/workbench/notebooks.rs::apply_notebook_edits applies them: apply_edits changes the model and returns an Applied with one event, the cells it added and removed, and the inverse edits. The function then creates and drops the cells' text models, sends $acceptModelChanged and the dirty state to ExtHostNotebookDocuments, and calls editors_changed.
  3. On the next render, app/src/workbench/notebook_editor.rs::sync_back_layer compares the model with what the webview was sent (BackLayer) and posts the difference: new outputs with their bytes, cleared outputs, markup previews, and the top of each.
  4. The webview renders, measures, and posts dimension. handle_notebook_message stores the heights, app/src/notebook/layout.rs::layout gives every cell its top, and the next view-scroll moves the outputs there.

An edit by the user (insert, delete, move, change language) and a workspace edit from an extension take the same step 2. A typed character in a cell is a text edit (editor.md); app/src/host_sync.rs::send_cell_content_change then bumps the notebook's version and tells the host.

Invariant: every change of cells goes through apply_notebook_edits. It is the one place that keeps the model, the cells' text models, the host and the running executions together. A cell pushed into cells directly has no text model and the host never hears of it.

Invariant: the host hears of a notebook before the documents of its cells. app/src/workbench/notebooks.rs::send_notebook_changes runs before send_host_changes in editors_changed, and sends a delta from the sent_* fields of Notebooks.

Invariant: apply_edits applies a batch from the last index to the first and sends one event with the next version id, as upstream's _doApplyEdits. Undo is the reverse: the inverse edits were computed one after another, so run_cells_element applies them one by one, and a redo is the original batch.

The editor and the code editors

A notebook tab is an editor with scheme vscode-notebook-editor whose path is the notebook's URI string. Its own text model is an empty placeholder (app/src/workbench/notebooks.rs::show_notebook).

Upstream gives every cell its own code editor widget. The port has one focused code editor per group (workbench.md, "Editor groups"), so the focused cell's text model is the one the group's code editor edits: sync_notebook_editors writes it into stacked_editors of app/src/editor.rs::EditorGroup. The other cells draw through app/src/workbench/editor_view.rs::embedded_code_editor. Typing, selections, find and language features in the focused cell are the editor's own code, with no notebook branch.

app/src/notebook.rs::CellList models the row templates of notebookCellList.ts: which cells in the render range hold a code editor. The host sees those cells, and an interactive window's input, as visible text editors (app/src/workbench/notebooks.rs::notebook_cells), which is what upstream's extensions and API tests observe.

Undo is the window's one service (editor.md). Cell edits are UndoElement::Cells in app/src/editor/edit_stack.rs, on the notebook's resource; a released notebook drops its elements.

A notebook has no diff editor: upstream's contrib/notebook/browser/diff is not ported.

The back layer webview

Outputs and markdown previews are HTML, so they draw in one webview per notebook editor that spans the list's content, as upstream's backLayerWebView.ts. GPUI draws the cell editors, toolbars and focus bars.

  • Only the active notebook editor has a webview. update_notebook_views drops the others' each render and makes it again on return, so everything the webview shows must be rebuildable from the model.
  • The webview is a native view over GPUI. It hides while a quick input, a context menu or the keybindings editor is open, and the find widget gets a hole in its clip. A GPUI element that must show over outputs needs one of the two.
  • List content coordinates are the webview's document coordinates. A cell's height depends on heights the webview measures and posts back, so a layout is final only after the webview answered.
  • Keystrokes the webview forwards are dispatched deferred (handle_notebook_webview_event): dispatching inside the handler updates the workbench while it is being updated.

Traps

  • A notebook that neither the host holds nor an editor shows is released, with its cells' text models and undo elements (release_notebooks). A dirty one stays. Code that keeps a notebook URI across frames must expect the model gone.
  • A scratchpad (an interactive window, or a REPL editor on an untitled notebook, without interactiveWindow.promptToSaveOnClose) is never dirty but is modified. Backups take a dirty notebook or a modified scratchpad; the close prompt reads dirty.
  • A REPL editor's last cell is its input box, and the list leaves it out: the list has one row fewer than cells. Go through app/src/workbench/notebook_editor.rs::notebook_rows.
  • The inverse of a replace needs the removed cells' text, which is not in the model. apply_edits names the cells in restore and apply_notebook_edits fills in their text before their text models go.
  • update_notebook_views, send_notebook_visible_ranges and render_notebook_cell_lists run in the workbench's render. Each must stay a comparison that does nothing when nothing changed (performance.md).

Check a change

  • port/tools/cargo-test notebook::: the model, layout, kernels, renderers, outline.
  • port/tools/cargo-test notebook_editor: undo, focus, find, drag and drop, view state.
  • port/tools/cargo-test the_interactive_window and port/tools/cargo-test a_dirty_notebook_backs_up: the interactive window and backups.
  • MOCHA_GREP="Notebook|ipynb|Interactive Window" port/tools/api-tests: the extension API against the port (testing.md).
  • port/tools/ui-pair with a step file of port/fixtures/ui-pair/m17, for example notebook-open.json or notebook-output-toolbar.json.