8

OK, so here's the deal:

  • I'm using Ace Editor
  • The app the editor is integrated in, is written Objective-C/Cocoa
  • I need AutoCompletion (for a given set of keywords)

Now, here's the catch :

  • I know AutoCompletion is not yet natively supported
  • I know of some attempts by others (e.g. Codiad IDE, Gherkin, Alloy-UI), some making use of Jquery UI Autocomplete - but I still cannot figure out how this could be adapted to an existing Ace setup
  • I'm still not sure if I should go for a JS-oriented solution or just use Objective-C/Cocoa for that

Any help would be more than appreciated.

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • possible duplicate of [Autocompletion in ACE editor](http://stackoverflow.com/questions/13545433/autocompletion-in-ace-editor) – endorama Nov 12 '14 at 14:00

3 Answers3

19

AutoCompletion can be achieved in ace editor..

Code :

    var editor = ace.edit('editor');
    editor.setTheme("ace/theme/eclipse");
    editor.getSession().setMode("ace/mode/java");
    editor.setShowInvisibles(true);
    editor.setDisplayIndentGuides(true);
    editor.getSession().setUseWrapMode(true);    
    var jsonUrl = "JSON/Components/proce.json";
    //the url where the json file with the suggestions is present
    var langTools = ace.require("ace/ext/language_tools");
    editor.setOptions({enableBasicAutocompletion: true});
    var rhymeCompleter = {
        getCompletions: function(editor, session, pos, prefix, callback) {
            if (prefix.length === 0) { callback(null, []); return }
            $.getJSON(jsonUrl, function(wordList) {
                callback(null, wordList.map(function(ea)  {           
                    return {name: ea.word, value: ea.word, meta: "optional text"}
                }));
            })
        }
    }

    langTools.addCompleter(rhymeCompleter);

Json file format :

   [ {"word":"hello"}, 
   {"word":"good morning"},
   {"word":"suggestions"},
   {"word":"auto suggest"},
   {"word":"try this"}]

Reference/Demo :

http://plnkr.co/edit/6MVntVmXYUbjR0DI82Cr?p=preview

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
Sairam Venkata
  • 356
  • 5
  • 12
5

To add Live Auto Completion in Ace nowadays: In your HTML include the ace/ext-language_tools.js. The . call does not work well yet, so you may have to enter ctrl-space or alt-space for that, but standard syntax stuff such as writting function, will now show. Then:

var editor = ace.edit("editor");

ace.require("ace/ext/language_tools");
editor.setOptions({
    enableBasicAutocompletion: true,
    enableSnippets: true,
    enableLiveAutocompletion: true
});
mesosteros
  • 1,491
  • 2
  • 18
  • 31
  • Didn't work for me when embedded in a application wrapper. I had to use `editor.setOptions("enableBasicAutocompletion", true);`. I think it's a bug in the application wrapper proxy interface and not in Ace. – 1.21 gigawatts Jul 31 '15 at 15:04
1

The hard part of autocompletion is figuring out the keywords the rest is easy to do.

  1. you need a popup, and listView to display completions, it might be better to use Cocoa based popup.
  2. some filtering function, simple startsWith check will do, but you can use nicer flex match like sublime
  3. trivial call to editor.session.replace to insert selected completion

For 2-3 you should comment at https://github.com/ajaxorg/ace/issues/110 about your specific usecase since there is a work to get native support for AutoCompletion.

a user
  • 23,300
  • 6
  • 58
  • 90