如何在 ACE 编辑器中将光标移动到行尾?

7

在我的Javascript文件中,我有以下代码行:

editor.focus(); // To focus the ace editor
var n = editor.getSession().getValue().split("\n").length - 2;
editor.gotoLine(n); 

这将把焦点放在ACE编辑器上,移动到最后一行并向上移动两行。当它向上移动这两行时,它也会将光标移动到该行的前面。模拟按下Home键的操作。

问:如何将光标移动到行末?或者模拟按下End键的操作?

2个回答

15

gotoline 需要两个参数,一个用于行数,另一个用于列数。

var row = editor.session.getLength() - 1
var column = editor.session.getLine(row).length // or simply Infinity
editor.gotoLine(row + 1, column)

请注意,gotoLine 函数会以动画的形式将所选内容滚动到视图中。如果您不需要这个动画效果,可以使用。
editor.selection.moveTo(row, column)

使用以下方法精确模拟用户按下 end 键并处理:

editor.execCommand("gotolineend")

1
第一个 gotoLine 有一个错别字,其他的都拼写正确。 - grandinero

8

直接从API获取:

该方法

你需要使用方法navigateLineEnd()。所以在你的情况下:

editor.focus(); // To focus the ace editor
var n = editor.getSession().getValue().split("\n").length - 2;
editor.gotoLine(n); 
editor.navigateLineEnd() // Navigate to end of line

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接