21

I'm using ACE Editor within a Chrome extension. I'm using ACE's Autocomplete feature but I want to be able to completely define a list of static strings to use for the autocomplete, instead of any local strings or snippets. (In the future I might be using something more sophisticated than a static list, but for now static is fine.)

Can anyone provide some instruction on how to accomplish this? I already have autocomplete enabled and snippets off, but I'm having trouble defining a static list of strings to use.

All I have so far is:

var editor = ace.edit('propertiesText');
editor.getSession().setMode('ace/mode/properties');
var langTools = ace.require('ace/ext/language_tools');

// code here to define custom strings?

editor.setOptions({
    enableBasicAutocompletion: true
});
Xan
  • 74,770
  • 16
  • 179
  • 206
russtuck91
  • 575
  • 2
  • 4
  • 16

2 Answers2

44

you need to add a completer like this

var staticWordCompleter = {
    getCompletions: function(editor, session, pos, prefix, callback) {
        var wordList = ["foo", "bar", "baz"];
        callback(null, wordList.map(function(word) {
            return {
                caption: word,
                value: word,
                meta: "static"
            };
        }));

    }
}

langTools.setCompleters([staticWordCompleter])
// or 
editor.completers = [staticWordCompleter]
//UPDATE: consider adding to completers list to not remove ace's already added completers
editor.completers.push(staticWordCompleter)
Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
a user
  • 23,300
  • 6
  • 58
  • 90
  • Does that actually check/use the prefix anyhow? Magic happening in `callback`? Or does it simply dump the whole word list regardless of the entered prefix? – Xan May 05 '15 at 08:27
  • 1
    Noramlly ace autocompleter filters the list afterwards, https://github.com/ajaxorg/ace/blob/v1.1.9/lib/ace/autocomplete.js#L427. Prefix is passed for more complex completers that need to give different completions based on context. – a user May 05 '15 at 08:53
  • Thanks for the clarification. It's a pity this does not seem to be documented anywhere. – Xan May 05 '15 at 08:54
  • Any way we can disable that filter? – Romeo Mihalcea Jun 12 '17 at 00:32
  • no, try creating feature request for it on ajaxorg/ace github repository – a user Jun 12 '17 at 11:58
  • I am assuming this gets triggered when you press control + space, is there any way to make the autocomplete appear even as you type like Android Studio? – PirateApp Dec 07 '17 at 07:36
  • @PirateApp, try adding "enableLiveAutocompletion: true" to the editor.setOptions() call (in addition to enableBasicAutocompletion). I believe this should work, not positive. – Vern Jensen Jan 17 '18 at 00:25
  • I am unable to view "static" meta word in my auto completion box. Can anyone guide me to resolve my issue. – Yash Vekaria Jun 11 '18 at 10:46
  • 2
    try using `addCompleter()` instead of `setCompleters()`, so yours will not replace the system ones – Bernardo Ramos Oct 08 '20 at 04:56
5

If you want to persist the old keyword list and want to append a new list

var staticWordCompleter = {
    getCompletions: function(editor, session, pos, prefix, callback) {
        var wordList = ["foo", "bar", "baz"];
        callback(null, [...wordList.map(function(word) {
            return {
                caption: word,
                value: word,
                meta: "static"
            };
        }), ...session.$mode.$highlightRules.$keywordList.map(function(word) {
        return {
          caption: word,
          value: word,
          meta: 'keyword',
        };
      })]);

    }
}

langTools.setCompleters([staticWordCompleter])
// or 
editor.completers = [staticWordCompleter]