如何在 ace 编辑器中使用 (.) 句点和 Ctrl_space 触发两个不同的自定义自动完成器 [英] How to trigger two different custom auto completer using (.) period and Ctrl_space in ace editor

查看:28
本文介绍了如何在 ace 编辑器中使用 (.) 句点和 Ctrl_space 触发两个不同的自定义自动完成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个完成者.一个用于数学自定义函数,另一个用于字符串自定义函数.我想使用 (.)period 和 ctrl_space 为数学函数触发字符串函数.现在,如果我按 (.) 或 ctrl_space,它会显示所有功能.但我想区分其中的两个.谢谢..

I have two completer. one for Math custom functions and another for string custom functions. i want to trigger string functions using (.)period and for ctrl_space for math function. Now if i press (.) or ctrl_space, it showing all the functions. but i want differentiate between two of them.Thanks..

var stringFunctions = ["Trim","Length","ToLower","ToUpper","ToNumber","ToString"]
var mathFunctions = ["Abs","Ceil","Exp","Floor","Log","ln","Pow","Round","Sqrt","cos","sin","tan","cosh","sinh","tanh","acos","asin","atan","Max","Min","Sum","Std","Var","Average","Norm","Median"]
var myCompleter1 = {
  // identifierRegexps: [/[a-zA-Z_0-9\.\$\-\u00A2-\uFFFF]/],
    getCompletions: function(editor, session, pos, prefix, callback) {
        console.info("myCompleter prefix:", this.prefix);
        callback(
            null,
            myList.filter(entry=>{
                return entry.includes(this.prefix);
            }).map(entry=>{
                return {
                    value: entry
                };
            })
        );
    }
}
var myCompleter2 = {
  // identifierRegexps: [/[a-zA-Z_0-9\.\$\-\u00A2-\uFFFF]/],
    getCompletions: function(editor, session, pos, prefix, callback) {
        console.info("myCompleter prefix:", this.prefix);
        callback(
            null,
            myList.filter(entry=>{
                return entry.includes(this.prefix);
            }).map(entry=>{
                return {
                    value: entry
                };
            })
        );
    }
}
langTools.addCompleter(myCompleter1);
langTools.addCompleter(myCompleter2);```

推荐答案

您可以在完成器中检查该行的文本来决定它是否在点之后

You can check the text of the line in the completer to decide if it is after dot or not

var stringFunctions = ["Trim", "Length", "ToLower", "ToUpper", "ToNumber", "ToString"]
var mathFunctions = ["Abs", "Ceil", "Exp", "Floor", "Log", "ln", "Pow", "Round",
  "Sqrt", "cos", "sin", "tan", "cosh", "sinh", "tanh", "acos", "asin", "atan",
  "Max", "Min", "Sum", "Std", "Var", "Average", "Norm", "Median"
]
var myCompleter1 = {
  getCompletions: function(editor, session, pos, prefix, callback) {
    var line = session.getLine(pos.row)
    var lineStart = line.slice(0, pos.column - prefix.length)
    var myList = !/\.\s*$/.test(lineStart) ? mathFunctions : stringFunctions;
    callback(null, myList.map(entry => {
      return {
        value: entry,
        score: 1000
      };
    }));
  }
}

var langTools = ace.require("ace/ext/language_tools")
langTools.addCompleter(myCompleter1);

var editor = ace.edit("editor", {
  maxLines: 20,
  minLines: 5,
  enableBasicAutocompletion: true,
  enableSnippets: true,
  enableLiveAutocompletion: false,
})
// automatically open popup after dot or word characters
var doLiveAutocomplete = function(e) {
  var editor = e.editor;
  var hasCompleter = editor.completer && editor.completer.activated;

  if (e.command.name === "insertstring") {
    // Only autocomplete if there's a prefix that can be matched
    if (/[\w.]/.test(e.args)) {
      editor.execCommand("startAutocomplete")
    }
  }
};

editor.commands.on('afterExec', doLiveAutocomplete);

<script src=https://ajaxorg.github.io/ace-builds/src-noconflict/ace.js></script>
<script src=https://ajaxorg.github.io/ace-builds/src-noconflict/ext-language_tools.js></script>

<div id=editor></div>

这篇关于如何在 ace 编辑器中使用 (.) 句点和 Ctrl_space 触发两个不同的自定义自动完成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆