Adding wise autofill to ace-editor: list of user-defined functions and variables (javascript language)

I want to add a list of user-defined functions and variables to the autostart editor. To do this, I want to study all the users of the code inserted into the document, find certain functions (and their arguments), certain variables and their scope, etc.

Main question

Is this data already calculated somewhere in the source code of the ace (or the language plugin) and can I just grab it?

What I want

for exapmle if the user inserted the code as follows:

var var0 = 'abcd'; function foo(var1, var2){ var var3 = 'efg'; } 

I want to add the 'foo' function to the autocomplete field with two parameters - var1 and var2. I want to also add var0 to the list of variables and add var3 only when the user writes to a specific area (in a function).

What I already know :

  • I know how to enable autocomplete and live automatically.
  • I know how to add a new kit.
  • I know that the built-in Basic auto-complete adds all the words in a document indiscriminately
  • I know about the ace-tern plugin , and I don't think I want to use it. At the moment, it is still hacked, not documented, and I cannot figure out how to enable it.
  • I know that Ace already has some data that I received after . For example, it warns when a variable is redefined if it is already defined in the same scope. Thus, he had a list of variables and their scope. I guess it uses jshint - but is there any way to grab it from there?
  • I read ace documation and find many useful methods that I can use to retrieve data if necessary. The question is whether I really need to do this.
+8
javascript autocomplete ace-editor
source share
1 answer

UPDATE: I meant that in my answer, but to clarify - Tern will do exactly what you ask in what I want . Below is a snippet below one of the problems associated with providing some context that you do not want to see even in the editor. See screenshots of your code used in Ace.Tern live demo

autocomplete for a function with two parameters inner scope of functions you have var0, var1, var2 and local var3

This is an assumption, but imo is the best option for adding autocomplete to ace Tern .

The turn accepts a typedef configuration parameter (described here: http://ternjs.net/doc/manual.html#typedef ), but more interestingly, it will accept your custom js object as a child, i.e.:

 var myContext = { name: 'myContext', obj: obj } 

Where obj is your js object. Then in the Tern configuration, you will use it as:

defs: ['underscore', myContext]

which will use both your custom object and the underline module for autocomplete.

Related to Tern ace.js config: (see https://github.com/sevin7676/Ace.Tern/blob/master/demo.html for comments on configuration settings)

  var myContext = { ... } var editor = ace.edit("editor"); editor.getSession().setUseWorker(true); ace.config.loadModule('ace/ext/tern', function () { editor.setOptions({ enableTern: { defs: ['browser', 'ecma5', myContext], plugins: { doc_comment: { fullDocs: true } }, useWorker: true, startedCb: function () { console.log('editor.ternServer:', editor.ternServer); }, }, enableSnippets: true, enableBasicAutocompletion: true, }); }); 
+3
source share

All Articles