views:

171

answers:

2

I am editing packages that use Moose, and I was wondering if there were a plugin for making Moose attributes show up in the Tag List.

For example, in the following code, the attribute options does not show up in Tag_List, but print_out_site does:

use Moose;
use MooseX::AttributeHelpers;

...

has 'options' => (
    metaclass => 'Collection::Hash',
    isa       => 'HashRef[Str]',
    is        => 'ro',
    provides  => {
        exists => 'exists',
        get    => 'get',
        set    => 'set',
    },
);

...

sub print_out_site {
    my $self = shift;
    my $key  = shift;
    $self->fasta_out_fh->print(">", $key, "\n");
    $self->fasta_out_fh->print($self->sites->{$key}, "\n");
}
+2  A: 

Add the line

--regex-perl=/has '(.*)' => \(/\1/a,attribute,moose attributes/

to ~/.ctags and it should show up. You may need to tweak the regular expression to avoid spurious matches in other files or to accommodate different formatting for the attribute declarations in other files.

This extends ctags so that it detects another type of tag based on the regular expression when parsing perl files.

Then you need to tell the taglist plugin about the new tag type by adding this to your vimrc file:

let tlist_perl_settings='perl;c:constant;l:label;p:package;s:subroutine;a:attribute'
Geoff Reedy
This works if I run ctags from the command line, but TagList doesn't seem to be reading the file. Any suggestions? I'm using Windows, if that's a clue.
daotoad
@daotoad I've added the needed settings for vim and updated the ctags configuration to assign a specific tag kind for these entries. This should get everything working.
Geoff Reedy
Thanks. I couldn't get it to work on my computer, so I tried it out in a new virtual machine and it worked fine. Now that I know your code works fine, I know where to look next. Thanks again so much!
molecules
NOTE TO FUTURE READERS: Be sure that "filetype on" appears in your .vimrc file. Also some Debian users may need to change the text in taglist.vim from "exuberant-ctags" to "ctags-exuberant".
molecules
+1  A: 

Geoff, I tried your code but it didn't work for me with the syntax you use. Could this be a version problem? I'm using exuberant ctags version 5.8.
I also modified the regex a bit because the quotes are optional and you might want to allow spaces (but nothing else) preceeding the 'has' keyword.
Here is what worked for me. I created a $HOME/.ctags file (didn't have one yet, otherwise just add to it) with the following line:

--regex-perl=/^\s*has\s+['"]?([0-9a-zA-Z_]+)/\1/a,attribute/

Then added the line in .vimrc as you suggested

let tlist_perl_settings='perl;c:constant;l:label;p:package;s:subroutine;a:attribute'

Now it lists my attributes in Moose modules.

In addition, I find it useful to also have information about the parent class, roles and used modules show up in the taglist, so here is my complete $HOME/.ctags file:

--regex-perl=/^\s*has\s+['"]?([0-9a-zA-Z_]+)/\1/a,attribute/
--regex-perl=/^\s*with\s+(['"])(.+)\1/\2/r,role/
--regex-perl=/^\s*extends\s+(['"])(.+)\1/\2/e,extends/
--regex-perl=/^\s*use\s+([^ ;]+)/\1/u,use/

and this is what I have in .vimrc (you can change the order of tags in the taglist simply by changing the order in the tlist_par_settings):

let tlist_perl_settings='perl;u:use;p:package;r:role;e:extends;c:constant;a:attribute;s:subroutine;l:label'
let Tlist_Show_One_File = 1

Because of the additional content I find it useful to use the Tlist_Show_One_File option, which forces the taglist to show only the tags of the currently selected file.
To temporarily hide some of the tags you can always move the cursor to the tag name and hit "zc" (and "zo" to reopen) the fold.

tospo