Vim命令在PHP源文件中查找所有在其主体中包含字符串的函数名


Vim command to find all function names that contain a string in their bodies, in a PHP source file

如何在VIM中从PHP源文件中生成函数名称列表,这些函数的主体中包含特定字符串?

您应该检查ctags。为源文件(或整个项目)创建一个标记文件,并使用例如:ts /hello来查找名称中包含hello的所有函数(或类)。

python程序怎么样?请注意,您应该将其中一些替换为PLY。。。。

#!/usr/bin/env python
'''
Project: Python parser to search php functions for regex
Author: Spencer Rathbun
Date: 1/17/2012
Summary: Search glob files (assumed to be php source) for input regex, and list function and line numbers where found.
'''
import re, argparse
from glob import glob
def main(regex, infiles):
    theRegex = re.compile(regex)
    currFunction = ''
    for globFile in infiles:
        for f in glob(globFile):
            with open(f,'rb') as currFile:
                for lineNum, line in enumerate(currFile.readlines()):
                    if line.split(' ')[0] == 'function':
                        currFunction = line.split(' ')[1].split('(')[0]
                    if theRegex.search(line) != None:
                        print("In function {0}'n'tLine {1}: {2}".format(currFunction, lineNum+1, line))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Search php source code for regex', version='%(prog)s 1.0')
    parser.add_argument('regex', type=str, default='', help='Regular expression')
    parser.add_argument('infiles', nargs='+', type=str, help='list of input files')
    args = parser.parse_args()
    main(args.regex, args.infiles)

将其添加到您的~/bin并使其可执行。然后在vim中,您可以用:!phpSearch.py yourRegex yourGlobPat来调用它。