How do I get Ack to ignore jQuery files?
Asked Answered
T

1

11

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?

Trull answered 17/10, 2011 at 16:42 Comment(2)
A possible workaround: Put all JS "library" files into a different directory than your project's JS files and use --ignore-dir.Lotta
Yeah, that thought crossed my mind. Was hoping for a more elegant answer, but I'm game with that too. Will leave the question open a little longer to see if anyone else has AckMagic to share.Trull
R
5

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!

Rakes answered 5/11, 2011 at 4:17 Comment(2)
Thanks, that looks great. I'll try it later today, but very thorough answer! Thanks!Trull
Did any changes to ack ever come of this?Vibrato

© 2022 - 2024 — McMap. All rights reserved.