<p>旺盛的标记(带有<code>--extra=+f</code>)为python文件名(例如<code>my_module.py</code>)生成标记,而不是为模块名(例如<code>my_module</code>)生成标记。我最终创建了<a href="https://github.com/enthought/Python-2.7.3/blob/master/Tools/scripts/ptags.py" rel="nofollow noreferrer">ptags</a>脚本的修改版本。将以下内容保存到路径中名为<code>ptags</code>的文件中,并使其可执行:</p>
<pre><code>#! /usr/bin/env python
# ptags
#
# Create a tags file for Python programs, usable with vi.
# Tagged are:
# - functions (even inside other defs or classes)
# - classes
# - filenames
# Warns about files it cannot open.
# No warnings about duplicate tags.
import sys, re, os
import argparse
tags = [] # Modified global variable!
def main():
for root, folders, files in os.walk(args.folder_to_index):
for filename in files:
if filename.endswith('.py'):
full_path = os.path.join(root, filename)
treat_file(full_path)
if not args.recursive:
break
if tags:
fp = open(args.ctags_filename, 'w')
tags.sort()
for s in tags: fp.write(s)
expr = '^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]'
matcher = re.compile(expr)
def treat_file(filename):
try:
fp = open(filename, 'r')
except:
sys.stderr.write('Cannot open %s\n' % filename)
return
base = os.path.basename(filename)
if base[-3:] == '.py':
base = base[:-3]
s = base + '\t' + filename + '\t' + '1\n'
tags.append(s)
while 1:
line = fp.readline()
if not line:
break
m = matcher.match(line)
if m:
content = m.group(0)
name = m.group(2)
s = name + '\t' + filename + '\t/^' + content + '/\n'
tags.append(s)
if __name__ == '__main__':
p = argparse.ArgumentParser()
p.add_argument('-f', '--ctags-filename', type=str, default='tags')
p.add_argument('-R', '--recursive', action='store_true')
p.add_argument('folder_to_index', type=str, default='.')
args = p.parse_args()
main()
</code></pre>
<p>现在运行以下命令,通过递归处理当前目录生成标记文件:</p>
<p><code>ptags -R -f tags_file_to_create /path/to/index</code></p>