ACE editor autocompletion remove local variables

2019-03-15 04:02发布

问题:

I'm using ace editor and I've enabled basicautocompletion and I'm pulling data from another server to get the suggestions. Everything works fine, but I want to remove the local suggestions from the suggestion box.

Here is my relevant code:

var langTools = ace.require("ace/ext/language_tools");
var editor = ace.edit("editor");

editor.setTheme("ace/theme/xcode");
var TextMode = require("ace/mode/text").Mode;
editor.getSession().setUseWrapMode(true);

editor.setOptions({
  enableBasicAutocompletion: true,
});

var qtags = {
getCompletions: function(editor, session, pos, prefix, callback) {
  $.getJSON(jsonURL,
    function(tagList) {
      callback(null, tagList.map(function(qtag) {
        return {name: qtag.name, value: "#"+qtag.name+"() ", meta: "qtag"}
      }));
    })
  }
}
langTools.addCompleter(qtags);

Here is how it looks like:

I've tried this line to remove all completers before adding my qtag completer, but that doesn't remove the local variables

langTools.completers = [];

Any input would be appreciated, I'm trying to avoid having to modify ace/ext/language_tools.js if possible, but at this point I'm open even to that option.

回答1:

The trick is to call langTools.setCompleters([]) before calling editor.setOptions({enableBasicAutocompletion: true});. May be its a good idea to set this option explicit to false before setting or adding the Completers. There is no need to change the sourcecode of the language_tools.

Example:

var langTools = ace.require("ace/ext/language_tools");
var editor = ace.edit("editor");

editor.setTheme("ace/theme/xcode");
var TextMode = require("ace/mode/text").Mode;
editor.getSession().setUseWrapMode(true);

var qtags = {
  getCompletions: function(editor, session, pos, prefix, callback) {
    $.getJSON(jsonURL,
      function(tagList) {
        callback(null, tagList.map(function(qtag) {
          return {name: qtag.name, value: "#"+qtag.name+"() ", meta: "qtag"}
      }));
    })
  }
}

langTools.setCompleters([qtags]);

editor.setOptions({
  enableBasicAutocompletion: true,
});


回答2:

you can use langTools.setCompleters

langTools = require("ace/ext/language_tools")
langTools.setCompleters([langTools.snippetCompleter, langTools.textCompleter])

or editor.completers = [langTools.snippetCompleter, langTools.textCompleter, qtags]