All the available functions of js2-refactor is available on github. Spend about 5 minutes to familiarize yourself with it https://github.com/magnars/js2-refactor.el.
2.1 Tern.js basic
- Auto completion on variables and properties
- Function argument hints
- Querying the type of an expression
- Finding the definition of something
- Automatic refactoring
There is an online demo that you can try at this link.
To use it, you need to install Tern binary executable using npm
The next thing you need to do is to install Tern packages for Emacs. The auto completion feature uses auto-complete so make sure you are familiar with auto-complete.
Add this to your .emacs to initialize tern and tern-auto-complete
For the auto completion to work properly in your Node.js project, you need to do
some config. Open your project directory and create a file named
with the content like this
For more details on the
.tern-project file as web as how to include some
plugins like AngularJS or RequireJS, visit the
Tern Docs. In my case, just
the above file is enough.
By default, Tern binds
. after a variable will cause Tern to read that object’s
properties and show the suggestion. This can be a bit slow when you first open
the file since it have to start the Tern server but the next time when you
tern-ac-dot-complete, it will be much faster. A file named
will also be created automatically in your project’s root folder.
There are some other important keys that are bound already by default
M-.Jump to the definition of the thing under the cursor.
M-,Brings you back to last place you were when you pressed M-..
C-c C-rRename the variable under the cursor.
C-c C-cFind the type of the thing under the cursor.
C-c C-dFind docs of the thing under the cursor. Press again to open the associated URL (if any).
2.2 Fix error when Tern does not auto refresh
Sometimes when you have just added
.tern-project file or edit the file but
Tern does not auto reload, you need to manually kill Tern server. This little
piece of code does the trick
When you encounter that problem, just activate the command
M-x and then continue using as normal. Tern server will be launched
3. Emacs Tags with Exuberant Ctags
If you are happy with Tern.js, stick with it, you probably don’t need Ctags.
To build the tag for your project, activate ctags with
A file named TAGS will be created inside the project root folder containing
all the information about the project source code. Now open any .js file in your
project, put the cursor on any function or variable name and activate
etags-select-goto-tag to quickly jump to that function definition. However,
the default Emacs’ etag feature is not quite good. You should use etags provided
by helm (can be installed via package.el).
Instead of activating
helm-etags-select. If the
project is big, It take some time to load tag file. But when it is done, the
next search will be very fast.