有没有一个 vim 插件可以让 Moose 属性显示在 Tag_List 中?

发布于 2024-08-20 10:36:57 字数 669 浏览 10 评论 0原文

我正在编辑使用 Moose 的包,我想知道是否有一个插件可以使 Moose 属性显示在标签列表中。

例如,在以下代码中,属性 options 不会显示在 Tag_List 中,但 print_out_site 会显示:

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");
}

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");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2024-08-27 10:36:57

将这一行添加

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

到 ~/.ctags 中,它应该会显示出来。您可能需要调整正则表达式以避免其他文件中的虚假匹配或适应其他文件中属性声明的不同格式。

这扩展了 ctags,以便在解析 perl 文件时根据正则表达式检测另一种类型的标记。

然后,您需要通过将以下内容添加到 vimrc 文件来告诉 taglist 插件有关新标签类型的信息:

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

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'
明媚如初 2024-08-27 10:36:57

杰夫,我尝试了你的代码,但它对我使用的语法不起作用。这可能是版本问题吗?我正在使用 exuberant ctags 版本 5.8。
我还对正则表达式进行了一些修改,因为引号是可选的,并且您可能希望在“has”关键字之前允许空格(但没有其他内容)。
这对我有用。
我创建了一个 $HOME/.ctags 文件(还没有,否则只需添加到它),其中包含以下行:

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

然后按照您的建议在 .vimrc 中添加了该行

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

现在它列出了 Moose 模块中的我的属性。

此外,我发现在标签列表中显示有关父类、角色和使用的模块的信息也很有用,所以这是我完整的 $HOME/.ctags 文件:

--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/

这就是我在 .vimrc 中的内容(您可以只需更改 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

由于存在附加内容,我发现使用 Tlist_Show_One_File 选项很有用,该选项强制标签列表仅显示当前所选文件的标签。
要暂时隐藏某些标签,您可以随时将光标移动到标签名称并点击“zc”(和“zo”以重新打开)折叠。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文