Programmatically change current line number (CM 6)

How do you programmatically change current line number in codemirror 6?

You mean move the selection? (view.dispatch({selection: {anchor: somePosition}}) can do that)

Ah, very good, thanks!

I have line numbers from the source, need to translate them to positions. From the Reference Manual I see that Line.from and will do that. How do I call these methods if I have an editor object?

You can find a line with editor.state.doc.line(N) (where N is 1-based).

This is fantastic! I can click on the rendered text (for an experimental markup language). Then codemirror highlights (makes current?) the corresponding line in source text.

One more step: how do I make codemirror scroll to that line?

Got it! editor.scrollPosIntoView(loc.from) --- where loc.from` is a valid position.

In the example I gave for setting the selection, you can include scrollIntoView: true in the object passed to dispatch to scroll the selection into view.

(But going forward, I suggest you spend some time with the docs rather than asking here about every step.)

1 Like