I'm trying to write a plugin to highlight text in the CodeMirror editor. I have borrowed the base code from another plugin and have the plugin working, apart from ONE thing. Every time I update a line in the editor, it resets the cursor to the start of the editor. It doesn't matter if the inserted text is before or after the location of the cursor, or even on a different line.
I have read all the CodeMirror forums and this is a known problem which the admin of CodeMirror refuses to fix in the core, saying that it can be worked around, but I cannot find any examples of what that workaround is.
Would those with knowledge of CodeMirror please give me a steer if you have an answer.
I have provided a stripped down version of the module without the bulk of the plugin code here -
module.exports = {
default: function(context: any) {
const plugin = function(CodeMirror) {
CodeMirror.defineOption('highlighter', false, function(cm, value, prev) {
if (!value) return;
cm.on('inputRead', async function (cm1, change) {
var line = cm.getCursor().line;
var activeLine = change.from.line;
var lineTxt = cm.getLine(change.from.line);
const start = {line: change.from.line, ch: 0};
const finish = {line: change.from.line, ch: 10};
var pos = { // create a new object to avoid mutation of the original selection
line: change.from.line,
ch: 35 // set the character position to the end of the line
}
cm.replaceRange("TEXT TO INSERT", start, finish, pos);
// I've also tried setting the cursor here to a new position here
cm.focus();
cm.setCursor(pos);
});
});
};
return {
plugin: plugin,
codeMirrorOptions: {
'highlighter': true,
}
}
}
}