Javascript 替换代码镜像令牌?

Javascript 替换代码镜像令牌?,javascript,codemirror,Javascript,Codemirror,我正在尝试通过替换CodeMirror v3中选择的令牌 var obj = editor.getTokenAt(currLine); var currLine = editor.getCursor(true); 但似乎唯一的选择就是这样做 replaceRange 它考虑了新字符串和起始位置,但是当新字符串比原始字符串短或长时,会发生奇怪的事情 有什么更好的方法 currLinereplace 似乎对我不起作用 谢谢 我必须显示自定义提示列表来替换select上的连接令牌。 var

我正在尝试通过替换CodeMirror v3中选择的令牌

var obj = editor.getTokenAt(currLine);
var currLine = editor.getCursor(true);  
但似乎唯一的选择就是这样做

replaceRange
它考虑了新字符串和起始位置,但是当新字符串比原始字符串短或长时,会发生奇怪的事情

有什么更好的方法

currLinereplace 
似乎对我不起作用

谢谢

我必须显示自定义提示列表来替换select上的连接令牌。
var pos = editor.getCursor() // or {line , ch };
var tok = editor.getTokenAt(pos);
editor.replaceRange("string", {line: pos.line , ch:tok.start},{line:pos.line , ch:tok.end});
使用正确的参数调用replaceRange方法是不够的。它粘贴了新的标记,但根据光标位置在初始标记的内部。如果我们有自定义提示选项,我们还需要在提示选项中指定起始位置和结束位置,以使replaceRange正常工作,例如:

const options = {
    hint: () => ({
      from: token.start, // without this replaceRange didn't work not correctly
      to: token.end, // without this replaceRange didn't work not correctly
      list: ['left join', 'right join', 'inner join', 'outer join'] // custom list of options
    })
  };
  cm.showHint(options); // this is to show custom hint
我必须显示自定义提示列表来替换select上的join令牌。 使用正确的参数调用replaceRange方法是不够的。它粘贴了新的标记,但根据光标位置在初始标记的内部。如果我们有自定义提示选项,我们还需要在提示选项中指定起始位置和结束位置,以使replaceRange正常工作,例如:

const options = {
    hint: () => ({
      from: token.start, // without this replaceRange didn't work not correctly
      to: token.end, // without this replaceRange didn't work not correctly
      list: ['left join', 'right join', 'inner join', 'outer join'] // custom list of options
    })
  };
  cm.showHint(options); // this is to show custom hint

作为附录,editor.replaceRangenewString,{line:currLine.line,ch:obj.start},{line:currLine.line,ch:obj.end};如果新闻字符串的大小小于或等于原始字符串的大小,则可以使用。这是一个很老的问题,但是今天我花了很多时间来研究如何使它工作,并且在遇到自定义提示的情况下,我找到了一个解决方案,我在下面贴了一个答案:作为附录,editor.replaceRangenewString,{line:currLine.line,ch:obj.start},{line:currLine.line,ch:obj.end};如果新闻字符串的大小小于或等于原始字符串的大小,则可以使用。否则会发生奇怪的事情。这是一个非常古老的问题,但今天我花了很多时间来了解如何使其工作,如果有自定义提示,我找到了一个解决方案,我发布了一个答案如下: