Commit 2a9a59c5 authored by Phil Jones's avatar Phil Jones

Fix highlighting not updating on new notebooks.

parent e9b69d8e
{ {
"name": "@pjj56/finesse-syntax-jupyterlab", "name": "finesse-syntax-jupyterlab",
"version": "0.1.7", "version": "0.1.8",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
......
{ {
"name": "finesse-syntax-jupyterlab", "name": "finesse-syntax-jupyterlab",
"version": "0.1.7", "version": "0.1.8",
"description": "Syntax highlighting for kat script within JupyterLab.", "description": "Syntax highlighting for kat script within JupyterLab.",
"keywords": [ "keywords": [
"jupyter", "jupyter",
......
...@@ -7,7 +7,7 @@ import { ...@@ -7,7 +7,7 @@ import {
} from '@jupyterlab/notebook'; } from '@jupyterlab/notebook';
import { import {
Cell, CodeCell Cell, CodeCell, ICellModel
} from '@jupyterlab/cells'; } from '@jupyterlab/cells';
...@@ -24,17 +24,30 @@ function activate(app: JupyterFrontEnd, tracker: INotebookTracker) { ...@@ -24,17 +24,30 @@ function activate(app: JupyterFrontEnd, tracker: INotebookTracker) {
tracker.widgetAdded.connect(activate_finesse) tracker.widgetAdded.connect(activate_finesse)
}; };
function check_all(sender: Notebook): void { function check_all(sender: NotebookPanel): void {
sender.widgets.forEach(cell => { sender.content.widgets.forEach(cell => {
if (cell instanceof CodeCell && cell.model.mimeType != "text/x-finesse-python") { if (cell instanceof CodeCell && cell.model.mimeType != "text/x-finesse-python") {
cell.model.mimeType = "text/x-finesse-python"; cell.model.mimeType = "text/x-finesse-python";
} }
}); });
}; };
function set_mime(model: ICellModel) {
// Jupyter seems to have some weirdness related to setup of the
// mime type of each cell. To avoid having timeouts everywhere,
// everytime we select a new cell, we attach a function that checks the
// mime type on each content change and sets it if required, otherwise
// disconnects itself.
if (model.mimeType == "text/x-finesse-python") {
model.contentChanged.disconnect(set_mime);
} else {
model.mimeType = "text/x-finesse-python";
}
}
function check_cell(sender: Notebook, cell: Cell): void { function check_cell(sender: Notebook, cell: Cell): void {
if (cell instanceof CodeCell) { if (cell instanceof CodeCell) {
cell.model.mimeType = "text/x-finesse-python"; cell.model.contentChanged.connect(set_mime);
} }
} }
...@@ -44,7 +57,7 @@ function activate_finesse(sender: INotebookTracker, panel: NotebookPanel): void ...@@ -44,7 +57,7 @@ function activate_finesse(sender: INotebookTracker, panel: NotebookPanel): void
// I ain't proud of this, but it's the sanest way I could find to get // I ain't proud of this, but it's the sanest way I could find to get
// the syntax highlighting to change on startup without selecting a // the syntax highlighting to change on startup without selecting a
// different cell // different cell
setTimeout(() => {check_all(panel.content)}, 500); setTimeout(() => {check_all(panel)}, 500);
}; };
export default plugin; export default plugin;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment