Using tern.js to power completion in a REPL

39 views
Skip to first unread message

Nikhil Kothari

unread,
Mar 10, 2015, 7:44:07 AM3/10/15
to tern...@googlegroups.com
I am trying to figure out how I might be able to add autocomplete support to a REPL I am building.

To get started, what I did was convert each snippet executed in the REPL to a file (type = full, text = snippet content), and issued a completions request with a query and the resulting set of files. This worked fine - got the completions I expected.

Next I hoped to be able to call addFile for each snippet as it was executed in the REPL, rather than each time I wanted to get completions. My new completions request only specified the query bit, with a reference to one of the files added via addFile. However this didn't work - got back 0 completions for the same snippets.


So questions ... trying to understand if I am thinking about this right, and what is the right  usage of tern to enable the scenario.

- What is the way to use tern in these incremental/REPL scenarios?
- I am assuming I can continue calling addFile with unique file names for each snippet entered, and have these factored in producing the completions list. Is that right?
- What is the right use of addFile and having completions work off of those?

Happy to provide more detail in any part that was unclear.

Thanks
Nikhil

Marijn Haverbeke

unread,
Mar 12, 2015, 5:42:38 PM3/12/15
to Nikhil Kothari, tern-dev
If you use a single server, files added will persist across requests,
So yes, calling addFile for every snippet should be good, assuming you
don't have a plugin like the node.js one loaded, which isolates each
file into its own scope.
> --
> You received this message because you are subscribed to the Google Groups
> "tern-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to tern-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nikhil Kothari

unread,
Mar 12, 2015, 7:02:19 PM3/12/15
to tern...@googlegroups.com, nikhi...@gmail.com
Ah ... so that probably explains it ... I do have the nodejs plugin loaded, and its behavior of isolation makes sense generally.

However, in my particular scenario, I am running each snippet of code entered into the REPL against a shared context object using node's vm module, so the scoping doesn't work the same way.
Is there a way to not have the nodejs plugin do its scoping, but just add the type definitions associated with node?

Would it make sense to have nodejs APIs defined in a separate JSON file like the defs for ecma5, browser etc. if all one needed was the API definitions?
Or maybe a flag in the nodejs plugin configuration to have it not do the scope-based isolation... so it can be used to reflect the semantics of vm.runInContext?

The other way to go about all this would be to concatenate every thing entered in the REPL and use that as the file content each time, but that doesn't seem wise ... thinking lots of wasteful work re-analyzing code that has already been executed... so I didn't pursue that path.

Thanks, Nikhil

Marijn Haverbeke

unread,
Mar 13, 2015, 2:06:18 AM3/13/15
to Nikhil Kothari, tern-dev
I doubt anybody is going to enter kilobytes of code at a REPL, so
including previous code wouldn't be too problematic. Just keep reusing
the same filename, so that it replaces the code submitted earlier.

Nikhil Kothari

unread,
Mar 13, 2015, 3:05:54 AM3/13/15
to tern...@googlegroups.com, nikhi...@gmail.com
Likely true in _many_ cases, but maybe not _most_ cases.
FWIW, I'm building a javascript version of ipython, so the code size is going to be easily much greater than what one enters in a scratchpad-style REPL, but admittedly less than code in a full-blown library.
Reply all
Reply to author
Forward
0 new messages