ranjeet kumar
ranjeet kumar

Reputation: 271

How to enable autocomplete on each key event in ace editor?

I am able to get the autocompletion list on (ctrl+space) key event in ace editor but I want to achieve the same behavior on user key event. Is there any way to do the same?

Upvotes: 4

Views: 1638

Answers (1)

Harsha pps
Harsha pps

Reputation: 2208

For adding custom autocomplete you need to bind the key, with addCommand, and then call the auto-complete

editor.commands.addCommand({
        name: "myCommand",
        bindKey: { win: "$", mac: "$" },
        exec: function (editor) {
              autocomplete();
        }
 });

Once the user trigger's the key you can call your autocomplete function, I'm adding a sample autocomplete function here, make the changes as you require.

autocomplete: function () {
        staticWordCompleter = {
            var getWordList = function(editor, session, pos, prefix, callback, isRHSEditor) {
            var wordList = ["Java","Javascript","Python"]; // add your words to this list

            callback(null, wordList.map(function(word) {
            return {
                 caption: word,
                value: word
            };
   }));
  editor.completers = [staticWordCompleter];
}

To always use autocomplete you can try this:

editor.commands.on("afterExec", function (e) {
    if (e.command.name == "insertstring" && /^[\w.]$/.test(e.args)) {
        editor.execCommand("startAutocomplete");
    }
});

Or you can bind the change event and call the autocomplete, which would trigger the autocomplete on each click

editor.on("change", function () {
      autocomplete();
});

Upvotes: 2

Related Questions