I need to insert a new line next to my current line number in CodeMirror.
I viewed the documentation but didn't find anything about appending any content at the end of the line.
Please help. :(
Get the current line from the cursor position, and act on it. This should do it (not tested):
var doc = cm.getDoc();
var cursor = doc.getCursor(); // gets the line number in the cursor position
var line = doc.getLine(cursor.line); // get the line contents
var pos = { // create a new object to avoid mutation of the original selection
line: cursor.line,
ch: line.length - 1 // set the character position to the end of the line
}
doc.replaceRange('my new line of code\n', pos); // adds a new line
This should work:
function appendTo(editor, line, text) {
editor.replaceRange(text, CodeMirror.Pos(line));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With