George V. Reilly's Technical Blog

Exuberant Ctags and JavaScript

Exuberant Ctags

Exuberant Ctags is an essential complement to Vim: it generates an index of symbol names (tags) for a set of source files. In Vim, just place the cursor on a function name and type C-] to go to its definition.

Ctags works well for most of the languages that I deal with, but falls down badly on modern JavaScript. Its built-in parser simply doesn't handle declarations like these:

Sizzle.selectors.filters.animated = function(elem) { // ...
ajaxSetup: function( settings ) {

I came across Unbad's workaround earlier tonight. His code didn't work for me, so I hacked on it until it did:

--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/

Simply add the above to ~/.ctags or $HOME/ctags.cnf.

Comments

Rick Harding said:

Thanks, some nice stuff in there. I am not getting the form

--regex-js=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*\(([^)])\)/\1/,function/

to work for me though. It looks like it should work, but in testing against this function definition:

function makeZebraTable () {

it won't pick it up. I had another function with the same format and moved it to

working = function() {

and then it was picked up, so I know it's reading the file and that the earlier function definition format is working.

Any ideas on what I'm missing in the regex? Sure seems it should work.

# July 15, 2009 3:10 PM

powentan said:

I have the same problem that

--regex-js=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*\(([^)])\)/\1/,function/

doesn't work.

I find the last part of the regular expression "\(([^)])\)" :

For the "(" symbol, it doen't need "\" to escape.

So the exact expression is  "([^)])"

When I replace the regular expression to :

--regex-js=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*([^)])/\1/,function/

It works well to detect function pattern like :

function makeZebraTable () {

By the way, thanks for this article to give me a guide how to use ctags with self define pattern :D

# August 27, 2009 1:02 AM

fvwnet said:

--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\{/\1/o,object/

--regex-JavaScript=/([A-Za-z0-9._$'"()]+)[ \t]*[:][ \t]*function[ \t]*\([^)]*\)/\1/f,function/

--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\[/\1/a,array/

--regex-JavaScript=/([^= ]+)[ \t]*=[ \t]*'[^']*'/\1/s,string/

--regex-JavaScript=/([^= ]+)[ \t]*=[ \t]*"[^"]*"/\1/s,string/

I think he miss a *

regex link egrep () is for groub \1 \2

and \( \) is literally ( )

# September 9, 2009 12:46 AM

ШОКИРУЮЩЕЕ said:

Благодарность за материалы! :)

Respect weblogs.asp.net

# December 5, 2010 11:57 PM

Lorrie said:

Hey hey hey, take a gdnear at what' you've done

# June 15, 2012 10:15 PM

Thawanya said:

Sorry it’s taken so long to respond; I’ve been kind of absnet (read: work projects got BUSY).I didn’t actuadally try it, but it should work just to change the escaped slashes.Examadple:    var pattern:RegExp = /(\)/g;    var fixed: String = path.replace(pattern,a0“/”);

# June 18, 2012 12:08 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)