从技术上讲,我在 vim 中使用Tagbar来查看文件的标签,但是这个问题应该普遍适用于 exuberant ctags,v5.8。
假设我有以下 python 文件,调用它foo.py
:
class foo:
def bar(baz):
print(baz)
让我们运行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
我感兴趣的是第二行的最后一个字段,class:foo
. 这就是bar()
函数的范围。如果我在 vim 中使用 tagbar,它会相应地将函数嵌套在类中。
现在假设我在我的~/.ctags
. 事实上,我正在添加对这个 puppet 文件的支持:
class foo {
include bar
}
假设我使用以下~/.ctags
论点。'import' 正则表达式很丑陋(errr...对于正则表达式来说很丑陋),但对于这个例子来说它已经完成了足够的工作:
--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/
tags
这会在我的文件中生成以下标签:
bar foo.pp /^ include bar$/;" i
foo foo.pp /^class foo {$/;" c
请注意,这两行都不包含范围信息。我的问题是:无论如何,我是否可以构建--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.