Getting jsctags to work with taglist.vim

Note: There is now a better, less hacky solution available here.

ctags is a popular program for indexing function declarations of source files. It works great for many languages, but fails miserably on javascript. jsctags comes to its rescue: it knows how to parse javascript and gives a far more informative list of functions in the same ‘tags’ format.

taglist.vim is a popular vim plugin that leverages ctags to provide a list of function declarations in the current source file. Unfortunately, it doesn’t work very well with jsctags, because jsctags doesn’t support the same command line options as ctags and cannot have its output adjusted to the form expected by the plugin.

Here’s a quick workaround. Put the following Perl script in your $PATH:

javascripttags

#! /usr/bin/env perl

if (!grep { $_ eq "--language-force=javascript" } @ARGV)
    { system("ctags " . join(" ", @ARGV)); exit; }
system("jsctags -o taglist " . pop() . " > /dev/null");
open(F, "taglist");
while(<F>) {
    if ($_ =~ /!_TAG_/) { next; }
    else { $_ =~ s/(\S+)(.*)lineno:(\d+)\ttype:(\w+) function\(([^)]*)\)/\1(\5) \4\2line:\3/; }
    print $_;
}
`rm taglist`;

And add to your .vimrc:

let g:Tlist_Ctags_Cmd = "javascripttags"

Note 1: You should not have a file named ‘taglist’ in your working directory, because javascripttags uses it as a scratch file. (I can’t seem to get jsctags to print to stdout.)

Note 2: As of this post, Ubuntu packages version 0.1.97 of Node.js, which jsctags does not seem to like. jsctags seems to run fine with the latest stable copy from nodejs.org though.

Related Posts

Jez Ng 05 January 2011
blog comments powered by Disqus