问题 获取ace编辑器的令牌字符串


我只是看了厨房水槽演示,看到有一个选项“显示令牌信息”,显示鼠标的文本类型(变量,功能等)

我想创建类似的东西,可以获取当前光标位置的单词的当前标记字符串。有谁知道怎么做?

谢谢!


8552
2017-12-05 14:13


起源



答案:


通过这种方式:

editor.on('mousemove', function(e) {
    var position = e.getCursorPosition();
    var token = editor.session.getTokenAt(position.row, position.column);

});

它将返回一个Object:

token = {
  type: "paren.rparen",
  value: "}",
  index: 0,
  start: 0
} 

14
2017-12-06 22:41



正是我所寻找的,谢谢! - amitdar
谢谢伙计,正是我所寻找的 - amitdar
很高兴我帮助过你;) - koMah
谢谢,正是我需要的!看起来好像检索位置的函数已经改变: e.getCursorPosition()。 - CoDEmanX
请注意 老鼠 位置不同于 光标 位置。你可能应该使用 editor.onCursorChange 而不是 mousemove 事件。 (除非你想检测鼠标指针所在的那个标记,在这种情况下我很难过!) - Joseph Humfrey


答案:


通过这种方式:

editor.on('mousemove', function(e) {
    var position = e.getCursorPosition();
    var token = editor.session.getTokenAt(position.row, position.column);

});

它将返回一个Object:

token = {
  type: "paren.rparen",
  value: "}",
  index: 0,
  start: 0
} 

14
2017-12-06 22:41



正是我所寻找的,谢谢! - amitdar
谢谢伙计,正是我所寻找的 - amitdar
很高兴我帮助过你;) - koMah
谢谢,正是我需要的!看起来好像检索位置的函数已经改变: e.getCursorPosition()。 - CoDEmanX
请注意 老鼠 位置不同于 光标 位置。你可能应该使用 editor.onCursorChange 而不是 mousemove 事件。 (除非你想检测鼠标指针所在的那个标记,在这种情况下我很难过!) - Joseph Humfrey