Save Ukraine

Emacs: using CTags for JavaScript and Rails

Christian Kruse,

In a recent job I write a lot of JavaScript code; therefore my needs for quick code navigation within a big JS project increased. I use CTags for my Ruby on Rails projects all the time, and it was time to get it working with JavaScript. So I did some googling and found two solutions.

jsctags

jsctags is a part of DoctorJS, a toolset for static code analysis for JS. It indexes modern JS in a ctags compatible manner. Sounds good, but I didn't get it working, especially I was not able to create a TAGS file containing both the „normal” ctags index for my rails project and the ctags for the JS part of the project. I would be amazed if one could give me a hint how to do this.

ctags with --langdef

The second solution I was able to find were some --langdef regexes in a blog post from early 2012: ctags is able to „learn” new languages by giving it some patterns. This works for me fairly well. I just created a ~/.ctags file with the following content:

--langdef=js
--langmap=js:.js
--regex-js=/([A-Za-z0-9.$]+)[ \t][:=][ \t]{/\1/,object/
--regex-js=/([A-Za-z0-9.$()]+)[ \t][:=][ \t]function[ \t](/\1/,function/
--regex-js=/function[ \t]+([A-Za-z0-9._$]+)[ \t](([^)]))/\1/,function/
--regex-js=/([A-Za-z0-9._$]+)[ \t][:=][ \t][/\1/,array/
--regex-js=/([^= ]+)[ \t]=[ \t][^"]'[^']/\1/,string/
--regex-js=/([^= ]+)[ \t]=[ \t][^']"[^"]/\1/,string/

--exclude=*.min.js --exclude=.git

Now emacs' `find-tag' jumps to the right location most of the time. Yay :-)