How to define new CodeMirror mode in JLab 4?

I asked this question for JLab 3.x before and need to ask again for JLab 4.x due to API change to @jupyterlab/codemirror. More specifically, I used to get hold of a CodeMirror instance through

import {  ICodeMirror } from '@jupyterlab/codemirror';

define

cm: ICodeMirror;

and call

cm.CodeMirror.define_mode(...)
cm.CodeMirror.modeInfo.push(...)

How can I do this for JLab 4.x? The only information I can find is

Mode has been removed. You can instead request the token IEditorLanguageHandler. That provides similar API: - Mode.registerModeInfo ->

from the migration guide but I could not find any example code or source code for IEdirotLangaugeHandler.

1 Like

Here you go:

The second part defines a language mode which is an extension of markdown supporting latex.

1 Like

Except that downstream you would use requires: [IEditorLanguageRegistry], not provides.

So everything is CodeMirror 6 now? I have a CM5 style language module that was imported using CodeMirror.defineMode(...) but this does not appear to be supported by the IEditorLanguageRegistry.

I think I need to adapt the mode to CM6 style, publish it separately as a npm package, and import it into the extension, but is there an easier way to do this?

No, the CM5/6 divide is quite absolute. There may be some some applicable patterns in legacy-modes, if the extension happened to be using simple-mode, but for the most part, it’s full-rewrite time.

Here’s a branch which started doing that for a simple-mode one, but the changes are still quite large.

1 Like

Thanks for the answer. I read more and realized just that. It took me a lot of time to write the language module for CM5 and I have to do it again… but at least now I know what I need to do now.

1 Like

I got SQL syntax highlighting working for JupyterLab 4, so sharing in case it’s helpful for anyone.

This extension adds SQL syntax highlighting to cells that begin with %%sql, it’ll be part of JupySQL (but I haven’t released it yet)

@edublancas this is super useful, I think we should do that for magic in core ipython mode of JupyterLab. Now, to make it robust is should substitute the language definition in IEditorLanguageRegistry rather than playing with IEditorExtensionRegistry directly, to ensure it is only applied to Python notebooks.

1 Like