如何在ace编辑器中添加我自己的完成器

Zan*_*cat 2 ace-editor

现在,我正在为数据库后端开发一个基于Web的简单编辑器。我发现ace带有自动完成程序,如果我只需要使用SQL关键字完成操作,该如何添加自己的规则?

小智 6

首先,如您所述激活enableLiveAutocompletion,还必须确保已将其enableBasicAutocompletion定义并设置为true(请参见下文)。

editor.session.setMode("ace/mode/sql");
editor.setOptions({
    enableBasicAutocompletion: true,
    enableSnippets: true,
    enableLiveAutocompletion: true
});
Run Code Online (Sandbox Code Playgroud)

To add a new completer, do what eemp mentioned on github (here).

let langTools = ace.require('ace/ext/language_tools');
Run Code Online (Sandbox Code Playgroud)

Then use the addCompleter method to add the completions as defined below:

var customCompleter = {
  getCompletions: function(editor, session, pos, prefix, callback) {
       // your code
       /* for example
        * let TODO = ...;
        * callback(null, [{name: TODO, value: TODO, score: 1, meta: TODO}]);
        */
  }

 }
langTools.addCompleter(customCompleter);
Run Code Online (Sandbox Code Playgroud)

You can also go have a look at the following:

Ace docs on Completers.

  • `langTools.addCompleter(customCompleter)` 可能会创建一个_global_完成器。要将完成器附加到 ace 编辑器本身,请使用“editor.completers = [customCompleter]”。 (2认同)