Examples of how to implement syntax highlighting

I made a simple syntax highlighting extension for Jupyterlab 3 using the ICodeMirror token however now that Jupyterlab 4 has come out, it seems that the ICodeMirror token has been removed and replaced with a more formal interface with the IEditorLanguageRegistry token. Are there any examples or explanations on how to use this interface to create a syntax highlighter for code?

In my older extension for Jupyterlab 3 I made use of CodeMirror.defineMode, CodeMirror.defineMIME and CodeMirror.modeInfo which all seem to be removed from the new interface.

My code is available at Jason Nagy / unicode-snippets-ts · GitLab under src/syntaxhilighter.ts and src/algorithm.ts.

It seems like I need to create an object of type IEditorLanguage but I am not sure how I need to do this. Looking at examples for jupyter-lsp, they seem to use an extension factory which is hard to parse and overkill for my simple language. Similarly I tried to read through the robotmode syntax highlighter at jupyterlab_robotmode/src/mode.ts at 315703af261b9776f962740ee1523e0bc4986ed9 · bollwyvl/jupyterlab_robotmode · GitHub but I am having difficulty understanding what is happening.

Hi @jaysun_n! I am having an interest in the same problem of enabling syntax highlighting using the IEditorLanguageRegistry method. I followed the example from robotmode, but I can’t get it to work. Do you have any updates on this?

The jupyterlab_robotmode was not updated to JupyterLab 4.x and is using the old CodeMirror 5 rather than CodeMirror 6 - do not follow it.

For a recent thread about syntax highlighting see Does JupyterLab language registry's addLanguage support LRLanguage as input?

2 Likes

Thanks @krassowski for mentioning that thread. However, that seems to be intended for markdown highlighting, whereas I would be interested in code highlighting, namely for extracted cells from ipython notebooks. I was able to successfully integrate the extractor with jupyterlsp, however I am not sure what is the behavior for highlighting when it comes to custom languages with python magic introduction (like sql). Any pointers on that or should I create a post detailing all of this?

The highlighter for both use cases is the same.