Notebook CodeMirror State Persistence

Hello, I am currently working on an extension that deals with saving and reusing code snippets, aiming for an improvement over Elyra Code Snippets in terms of more fluidity in dev workflow.

One feature that we are working on is having a code snippet be highlighted when it gets used in a notebook. This is done via extending CodeMirror and using Line Decorations. We are directly using their API and not the code mirror interface provided by the Jupyter Lab API since I could not find an interface within the API docs for decorations, specifically line decorations(CodeMirror Reference Manual).

Currently I am running into a problem where the coloring changes I made to the cell via CodeMirror do not persist. I am just wondering if there is anywhere in the API of JupyterLab that allows for the saving of a notebook state?

Whenever a code snippet from the library gets dragged into the notebook, I essentially grab the start and end line of the code snippet location and apply decorations to them via CodeMirror. I also attached attributes like snippet_start and snippet_end to their elements as well, as my original approach for having colors persist was going to query the DOM for the snippet_start and snippet_end tags and reapply decorations on every refresh.

However I found out that the CodeMirror is editing not the direct DOM but sort of like a shadow copy of itself. Every refresh the CodeMirror plugin gets reset, thus the edits made to the shadow DOM also get reset which is why the state does not persist.

Looking at the docs, another method I was thinking of was to somehow use the INotebookTracker from Jupyter Lab API so that:
1. We would uniquely assign ID’s to every cell for the notebook on it getting loaded.
2. Whenever a snippet instance was pasted in, the cell it got pasted in to would have an ID already associated with it, so we would save the ID of the cell to the snippet instance
3. On refresh, we simply query the notebook for the cells that are associated with the snippet instances by their cell IDs and reapply the colorings to them

This approach does seem a little complicated though and I was wondering if there was a way to save a previous snapshot of the notebook so that on reload I can reapply it.

Any guidance would be welcome.

Are you adding codemirror extensions via JupyterLab API? It is hard to guess what goes wrong without seeing your code.