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

FredFrin said:

Took me a while to get this working. Reason was that I already had .js files mapped to language 'JavaScript', which was taking prescedence over the above definition ... hence the regexes were not used. I removed the langdef & replaced the language refs as shown below - works beautifully now! Thanks (also to unbad)!!

--langmap=JavaScript:.js

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

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

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

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

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

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

# November 4, 2009 5:16 AM

Does ctags for windows support .conf files Drija said:

Pingback from  Does ctags for windows support .conf files Drija

# November 12, 2010 7:54 AM

??????vim???js????????????????????????????????? « ????????? said:

Pingback from  ??????vim???js????????????????????????????????? « ?????????

# November 30, 2010 6:28 AM

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

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

Respect weblogs.asp.net

# December 5, 2010 11:57 PM

artm said:

another suggestion:

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

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

these expressions strip off quotes and ".prototype" from tag names, which makes for a better display in e.g. vim's taglist plugin.

as far as I can see the whole parentheses business isn't necessary, so I omitted it.

# December 10, 2010 7:37 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

RapSmump said:

buy a <a href="www.burberryoutlettt.com/">burberry outlet</a>  with confident

burberryoutlettt821

# August 20, 2012 10:22 PM

Siseerex said:

click to view <a href="www.discount-burberry-outlet.com/">burberry store</a>  for gift  

# November 27, 2012 7:53 AM

Mitolors said:

must look at this <a href="www.discount-burberry-outlet.com/">burberry discount outlet</a>  at my estore  

# November 27, 2012 8:06 AM

Nixbeedo said:

check this link, <a href="www.discount-burberry-outlet.com/">burberry outlet</a>   to get new coupon  

# November 27, 2012 8:18 AM

RapSmump said:

purchase <a href="www.discount-burberry-outlet.com/">burberry outlet store locations</a>  online  

# November 27, 2012 8:30 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)