如何在CodeMirror中以编程方式添加新行?

Mel*_*vin 3 codemirror

我需要在CodeMirror中的当前行号旁边插入一个新行.

我查看了文档,但没有找到任何关于在行末添加任何内容的内容.

请帮忙.:(

Eli*_*lka 8

从光标位置获取当前行,并对其进行操作.这应该做(没有测试):

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
Run Code Online (Sandbox Code Playgroud)


Mar*_*ijn 5

这应该工作:

function appendTo(editor, line, text) {
    editor.replaceRange(text, CodeMirror.Pos(line));
}
Run Code Online (Sandbox Code Playgroud)