Partial views of documents

In codemirror 5 you can use linkedDoc to link two documents together so that changes to one document are reflected in the linked document automatically. You can also provide a start and end line to linkedDoc which causes the linked document to only display the content between the provided lines.

Is there a way to achieve this behaviour in codemirror 6?
I am currently trying to extend the Split View example and starting each EditorView with a state generated with view.doc.slice(...) but I’m unsure how to modify changesets so they can be applied to the partial views. Is this the method you would recommend or are there better ways to achieve this behaviour?

1 Like

I haven’t concretely tried something like that, but the idea would be to do it very differently from the CM5 approach. The split view example already points in the right direction. To provide only a subview of a document, you’d have to filter the changes so that only the parts that apply to the sub-range are applied to that view. In the process, you’d have to adjust your information about the line number where the subview starts, when the changes affect that. Actually changing the numbering of lines in a document isn’t going to be supported, but you can configure the line number gutter to show different numbers via the formatNumber option, and you could reconfigure that when the base number changes.

1 Like

I am working on a Jupyter-like notebook learning environment powered by CodeMirror 6. I wonder if the lines can be numbered across editors, so I search online and found this thread.

It turns out that if we restrict the partial view boundaries to whole lines, it is not hard to enhance the split view example that resembles the linkedDoc functionality of CodeMirror 5. Code for a day and this is my result: a primary editor with subviews that split the text document at specified line boundaries. Edits are synchronized between both kind of views.

Sample usage: Suppose we want to split a document into 3 pieces:

const linkedEditors = new LinkedEditors({
  doc: `this is block 1
this
is
block 2
this is
block 3`
}, [1, 3, 2])
// mount linkedEditors.subviews[0..3] and linkedEditors.globalView to see the result

I have not wired up the selections and effects, but their behaviors are highly implementation-dependent. Not knowing how far it will go.