Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ace editor and autocomplete with custom functions

Tags:

ace-editor

I'm using ACE editor and I enable autocomplete with the code below but this seems like it's giving me autocomplete just by any word in the document. This seems strange. I would expect it to act more like Visual Studio in that it only lists functions/variables vs just any word that exists in the doc. Is autocomplete the same as intellisense? I'm looking to be able to, in real-time, have possible function names listed with their parameters and not functions that are in the file, but functions I define from an external library that I'm using from within Lua. Is ACE able to do that?

editor.setOptions({
                enableBasicAutocompletion: true,
                enableSnippets: true,
                enableLiveAutocompletion: false
            });
like image 300
user441521 Avatar asked Oct 31 '22 08:10

user441521


1 Answers

You can set your own autocompleter as defined here

<html>
<body>
  <div id="editor" style="height: 500px; width: 800px">Type in a word like "will" below and press ctrl+space or alt+space to get "rhyme completion"</div>
  <div id="commandline" style="position: absolute; bottom: 10px; height: 20px; width: 800px;"></div>
</body>
  <script src="https://rawgithub.com/ajaxorg/ace-builds/master/src/ace.js" type="text/javascript" charset="utf-8"></script>
  <script src="https://rawgithub.com/ajaxorg/ace-builds/master/src/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
  <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
    var langTools = ace.require("ace/ext/language_tools");
    var editor = ace.edit("editor");
    editor.setOptions({enableBasicAutocompletion: true, enableLiveAutocompletion: true});
    // uses http://rhymebrain.com/api.html
    var rhymeCompleter = {
        getCompletions: function(editor, session, pos, prefix, callback) {
            if (prefix.length === 0) { callback(null, []); return }
            $.getJSON(
                "http://rhymebrain.com/talk?function=getRhymes&word=" + prefix,
                function(wordList) {
                    // wordList like [{"word":"flow","freq":24,"score":300,"flags":"bc","syllables":"1"}]
                    callback(null, wordList.map(function(ea) {
                        return {name: ea.word, value: ea.word, score: ea.score, meta: "rhyme"}
                    }));
                })
        }
    }
    langTools.addCompleter(rhymeCompleter);
</script>
</html>
like image 89
Doug Avatar answered Jan 04 '23 15:01

Doug