如何在ace编辑器中使用(。)期间和Ctrl_space触发两个不同的自定义自动完成器

时间:2019-08-08 10:52:47

标签: ace-editor

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

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);```

1 个答案:

答案 0 :(得分:0)

您可以在完成程序中检查该行的文本,以确定它是否在点之后。

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>