我可以将范围信息添加到在 exuberant ctags 中使用“--regex-<LANG>”生成的标签吗?

Chr*_*her 5 ctags exuberant-ctags tagbar

从技术上讲,我在 vim 中使用Tagbar来查看文件的标签,但这个问题应该普遍适用于 exuberant ctags,v5.8。

假设我有以下 python 文件,调用它foo.py

class foo:
    def bar(baz):
        print(baz)
Run Code Online (Sandbox Code Playgroud)

让我们运行ctags一下:ctags foo.py。生成的tags文件如下所示:

!_ some ctags version / formatting stuff not worth pasting
bar foo.py  /^    def bar(baz):$/;" m   class:foo
foo foo.py  /^class foo:$/;"    c
Run Code Online (Sandbox Code Playgroud)

我感兴趣的是第二行的最后一个字段class:foo。这就是函数的范围bar()。如果我在 vim 中使用 tagbar,它会相应地将函数嵌套在类中。

现在假设我在我的~/.ctags. 事实上,我正在添加对此木偶文件的支持:

class foo {
    include bar
}
Run Code Online (Sandbox Code Playgroud)

假设我使用以下~/.ctags参数。“导入”正则表达式很丑陋(呃……对于正则表达式来说很丑陋),但对于这个例子来说它已经完成了足够的工作:

--langdef=puppet
--langmap=puppet:.pp
--regex-puppet=/^class[ \t]*([:a-zA-Z0-9_\-]+)[ \t]*/\1/c,class,classes/
--regex-puppet=/^\ \ \ \ include[ \t]*([:a-zA-Z0-9_\-]+)/\1/i,include,includes/
Run Code Online (Sandbox Code Playgroud)

这会在我的文件中生成以下标记tags

bar foo.pp  /^    include bar$/;"   i
foo foo.pp  /^class foo {$/;"   c
Run Code Online (Sandbox Code Playgroud)

请注意,这两行都不包含范围信息。我的问题是:我是否可以构造--regex-puppet参数或--regex-<LANG>一般行来收集有关标签范围的信息?也许可以声明满足标准 A 的标签始终是满足标准 B 的标签的范围父级?

man ctags建议没有明确的方法来添加任意范围信息,但我可能会忽略另一个解决方案(为了强调而稍微剪掉):

--regex-<LANG>=/regexp/replacement/[kind-spec/][flags]

        Unless modified by flags, regexp is interpreted as a Posix extended regular expression. The replacement should expand for all matching lines  to  a  non-empty  string  of
        characters,  or  a  warning message will be reported. An optional kind specifier for tags matching regexp may follow replacement, which will determine what kind of tag is
        reported in the "kind" extension field (see TAG FILE FORMAT, below). The full form of kind-spec is in the form of a single letter, a comma, a  name  (without  spaces),  a
        comma, a description, followed by a separator, which specify the short and long forms of the kind value and its textual description (displayed using --list-kinds). Either
        the kind name and/or the description may be omitted. If kind-spec is omitted, it defaults to "r,regex". Finally, flags are one or more single-letter characters having the
        following effect upon the interpretation of regexp:

           b   The pattern is interpreted as a Posix basic regular expression.

           e   The pattern is interpreted as a Posix extended regular expression (default).

           i   The regular expression is to be applied in a case-insensitive manner.
Run Code Online (Sandbox Code Playgroud)

Jan*_*res 4

不,不幸的是,ctags 中的正则表达式模式支持不可能实现这一点。让 ctags 生成正确范围的唯一方法是用 C 语言编写一个解析器作为附加模块。如果有时间的话,我想为 ctags 添加更好地处理新语言的支持,但到目前为止还没有解决了,我也仍然不确定最好的方法。

如果您对 Tagbar 支持最感兴趣,那么还有另一种方法:Tagbar 支持任意标记生成程序,只要它们的输出与 ctags 兼容,因此您可以使用 Python 编写一个简单的解析器并配置 Tagbar使用它。如果这对您来说是一个选择,请看一下:h tagbar-extend(尤其是最后一小节“编写您自己的标签生成程序”)。