Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Ack to ignore jQuery files?

Tags:

jquery

vim

ack

I'm using Vim + Ack.Vim and am flummoxed on how to ignore hits within Jquery files. I've got an .ackrc file defined (see below), but I'm stabbing in the dark.

--type-add=ruby=.haml,.rake,.rsel,.builder
--type-add=html=.html.erb,.html.haml
--type-add=js=.js.erb
--type-add=css=.sass
--type-set=cucumber=.feature
--type-add=jquery=jquery*.js
--ignore-dir=vendor
--ignore-dir=log
--ignore-dir=tmp
--ignore-dir=doc
--ignore-dir=coverage
--sort-files
--color
--follow
--group
--nojquery

How would seasoned ack + ack.vim users solve this issue?

like image 607
Dave Sanders Avatar asked Oct 17 '11 16:10

Dave Sanders


1 Answers

Interesting problem! I can think of a few approaches:

  1. patch ack to allow filtering with filename patterns (best: ack needs this feature)
  2. modify ack.vim to ignore certain filename patterns (not sure how you'd do this)
  3. filter output of ack, with a wrapper script/program (brittle/annoying to munge ack's output)
  4. filter input filelist given to ack, with a wrapper script/program (doable)
  5. patch ack to ignore jQuery files (kludgy, but works)

I got the last one working. Ack is written in Perl, so it's pretty easy to read and modify. Look for Ack.pm on your system. I use Ubuntu 11.10 and installed ack-grep to get ack; my Ack.pm is found at /usr/share/perl5/App/Ack.pm. If you installed the stand-alone version of ack, the file you'll edit is just called "ack". Look for the subroutine is_searchable(). Here's what I see:

sub is_searchable {
    my $filename = shift;

    # If these are updated, update the --help message
    return if $filename =~ /[.]bak$/;
    return if $filename =~ /~$/;
    return if $filename =~ m{^#.*#$}o;
    return if $filename =~ m{^core\.\d+$}o;
    return if $filename =~ m{[._].*\.swp$}o;

    return 1;
}

Add another line right after above return 1;:

    return if $filename =~ /^jquery/;

Again, going back to my first suggestion (patch ack to allow filtering with filename patterns) Andy might take a patch for this.

By the way, you probably already figured this out, but your use of --type-add doesn't appear to be valid syntax for the ack command line:

--type-add=jquery=jquery*.js

it just expects file extensions. Hope this helps!

like image 85
Adam Monsen Avatar answered Sep 18 '22 22:09

Adam Monsen