gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:31:48 25 4
gpt4 key购买 nike

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

我查看了文档,但没有找到任何关于在行尾附加任何内容的信息。

请帮忙。 :(

最佳答案

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

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

关于codemirror - 如何在 CodeMirror 中以编程方式添加新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22609868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com