How to trigger two different custom auto completer using (.) period and Ctrl_space in ace editor

孤人 提交于 2021-01-04 04:48:43

问题


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

回答1:


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>


来源:https://stackoverflow.com/questions/57410925/how-to-trigger-two-different-custom-auto-completer-using-period-and-ctrl-spa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!