[实践Ok]CTags PHP5 函数查找及一些快捷键介绍,特别是Grep用-P的Perl正则查找函数原型的使用。。

jackxiang 2009-2-19 10:28 | |


一:CTags PHP5 函数查找及一些快捷键介绍
ctags 常用快捷键 :

{ 转到上一个空行
} 转到下一个空行
gd 转到当前光标所指的局部变量的定义
* 转到当前光标所指的单词下一次出现的地方
# 转到当前光标所指的单词上一次出现的地方


tags内不包含调用点,只包含函数定义的位置信息,所以不能查找所有调用。


二:Grep用-P参数的Perl正则查找函数原型的使用示例:

这个在linux下使用shell命令:大写P是正则的意思,否则匹配会有问题,n是行号。
find . -name "*.php"|xargs grep -Pn 'setPlayCache\s*\('
实践查找smodifyData函数原型:

进一步过滤去掉jack备份目录,并只在models下找(因为有可能其它目录也有相同函数):

显示行数-i:
find . -name "*.php"|xargs grep -Pin "function\smodifyData\s\("|grep models|grep -v jack
./application/models/play.php:70:    public function setPlayCache($cacheKey, $data = array())


实践如下:

例外情况,有时不是\s能查到的,是任何字符才能查,如下:
find . -name "*.php"|xargs grep  -Pir ".*Token.*"

$app = new Token('user_token_'.$this->user_id);
$app = new Token('hostw');
——————————————————————————————————————————————
vim里查找函数原型,和平时查找一样的,只是多了个\s,(也不用像grep那样转意:
vi index.php 输入左斜杠开始查找:

查找某个文件里的函数原型:
cat session.php |grep -P function

查看被调用的位置,就需要使用cscope了




如果grep时出现下面问题,解决方法如下:
[root@iZ25dcp92ckZ]# grep -rinP  "checkPass" ./
grep: this version of PCRE is compiled without UTF support
Linux 查看内存的插槽数,已经使用多少插槽。每条内存多大,已使用内存多大
# dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range
查看内存支持的最大内存容量
# dmidecode|grep -P 'Maximum\s+Capacity'
查看内存的频率
dmidecode|grep -A16 "Memory Device"|grep 'Speed'
开始查询遇到这个错误:
grep: this version of PCRE is not compiled with PCRE_UTF8 support
看来是PCRE扩展没有开启支持UFT8格式,重新下载个新版http://iweb.dl.sourceforge.net/project/pcre/pcre/8.36/pcre-8.36.tar.gz 编译,参数./configure --enable-utf8。
装完后还是不行,因为grep也要重新编译。。。
好吧继续下grep:
http://ftp.gnu.org/gnu/grep/grep-2.21.tar.xz
来自:http://www.shentou.org/hacker/166.html


附录:
其实最简单的就是:把光标移动到某个元素上,CTRL+],就会跳转到对应的定义啦。CTRL+o可以回退到原来的地方。
Howto patch ctags 5.6 to work with php5 class member variable features (private, public, protected). ??
早些时候下载了 GVIM Taglist 插件,用来显示 PHP 文件的结构,Taglist 需要 Ctags 的支持,但是 Ctags 目前尚不支持 PHP5 的类限制符(Public/Protected/Private),所以无法识别类中的方法。问题暴露出来了,总会有人去摆平它的,经过一番搜索,找到了一个老外放出的支持 PHP5 的 Ctags Patch,但只能在 Linux 下面使用,Windows 的用户需要自己下载源码重新编译 ctags.exe 文件,所幸的是这个东东也有人编译好了,还是最新的 Ctags 5.6 版本。所以善用搜索,几乎没有解决不了的问题哈,呵呵 :)
Ctrl+]的返回应该是Ctrl+t
Ctrl-O是返回前一个打开的文件,所以看起来结果相同。

下载地址:windows版本的
http://untidy.net/blog/2006/10/04/ctags-php-5-support/
http://blog.chrisramsay.co.uk/2006/10/24/ctags-with-vim-for-php5/

安装位置如下:
cp ctags /usr/local/bin/ctags  &&  chmod 755 /usr/local/bin/ctags
cp ./ctags.1 /usr/local/share/man/man1/ctags.1  &&  chmod 644 /usr/local/share/man/man1/ctags.1
[root@localhost ctags-5.8]# whereis ctags
ctags: /usr/bin/ctags /usr/local/bin/ctags


Linux 用如下方法:

One of the features I have missed since starting using Vim as my main source code editor is the ability to navigate a source file by its classes, properties and methods. I am aware of the existence of Ctags as a program for mapping source files and creating indexes accessible for use by text editors, but my experience has been up until recently that Ctags cannot correctly index PHP5 files - Ctags does not seem to understand the Private, Protected and Public precursors to the function arguments. However I have come across a patch -  provided by Christian Stocker at bITFLUX that fixes this problem.

Patching and Installing Ctags

Christian says on his site that his patch works for Ctags 5.5.4 - I have tried it against version 5.6 and found no problems. Below is a brief run through of installing and patching Ctags on an Linux or Cygwin machine.

Firstly download and untar Ctags and Christian Stocker’s patch

# wget http://kent.dl.sourceforge.net/sourceforge/ctags/ctags-5.6.tar.gz
# tar -xvf ctags-5.6.tar.gz
# wget http://svn.bitflux.ch/repos/public/misc/ctags-php5.patch
# mv ctags-php5.patch ctags-5.6

Next, it’s time to apply the patch to the Ctags php.c file.

# cd ctags-5.6
# patch php.c ctags-php5.patch

Next, configure the build and make install. Note the optional --prefix="/" - this is because I ran this particular example in Cygwin.

# ./configure --prefix="/usr/local/ctags"
# make
# make install
# make clean
删除掉原来的不支持PHP的ctags,然后再做软链接到现在支持PHP的ctags:
rpm -ql ctags
/usr/bin/ctags
/usr/share/doc/ctags-5.8
/usr/share/doc/ctags-5.8/COPYING
/usr/share/doc/ctags-5.8/EXTENDING.html
/usr/share/doc/ctags-5.8/FAQ
/usr/share/doc/ctags-5.8/NEWS
/usr/share/doc/ctags-5.8/README
/usr/share/man/man1/ctags.1.gz

/usr/bin/ctags
If everything made ok, try running the following:

rpm -e ctags

替换原来的ctags。
cp /usr/local/bin/ctags /usr/bin

不用作软连接的办法:
vi /etc/profile 加上:
export PATH=/usr/local/ctags/bin/ctags:$PATH :
单独文件放profile.d文件夹里新建一文件,和上面一样的道理,在登录时会去刷这个文件夹:
cat /etc/profile.d/ctags.sh
export PATH=/usr/local/ctags/bin/ctags:$PATH
在vim里加载ctags:
ctags -R

在用-R直接生成的tags的时候,如果无关的文件太多,tags生成的会非常而且慢,比如cocos2d-x的项目。
解决的方法比较简单,参考了cscope生成 cscope.files的思路:

find . -name "*.h" -o -name "*.c" -o -name "*.cpp" -o -name "*.m" -o -name "*.mm" -o -name "*.java" -o -name "*.py" > src.files
ctags -R --c++-kinds=+px --fields=+iaS --extra=+q -L src.files

find . -name "*.h" -o -name "*.c" -o -name "*.cpp" -o -name "*.m" -o -name "*.mm" -o -name "*.java" -o -name "*.py" > src.files
ctags -R --c++-kinds=+px --fields=+iaS --extra=+q -L src.files
这样生成的速度就会快很多啦

自动加载ctags配置,vim 打开PHP文件后自动加载ctags,使用如下命令:
Set tags=tags的文件路径
Set tags=/data/htdocs/XXX.com/tags

vi ~/.vimrc    //vim打开就加载tags。
set tags=/data/htdocs/XXX.com/tags;
https://jackxiang.com/post/1635/


之后,才是使用前面的快捷键。



方案2 (这个是对这个ctags作脚本替换,并没有直接像方案1这样编译进去,所以,这个方法不是太好,我没有试,再就是这个.ctags能不能自动运行都是个问题。)
1. $ vim ~/.ctags
增加
alias phptags='/usr/local/ctags/ctags --langmap=php:.engine.inc.module.theme.php  --php-kinds=cdf  --languages=php'

2. $ vim ~/.ctags
写入
--regex-php=/^[ \t]*[(private|public|static)( \t)]*function[ \t]+([A-Za-z0-9_]+)[ \t]*\(/\1/f, function, functions/
--regex-php=/^[ \t]*[(private|public|static)]+[ \t]+\$([A-Za-z0-9_]+)[ \t]*/\1/p, property, properties/
--regex-php=/^[ \t]*(const)[ \t]+([A-Za-z0-9_]+)[ \t]*/\2/d, const, constants/

3. $ phptags -R

方案二来自:http://web.hanzheng.info/lamp/tool-lamp/vim/vim-%E8%A7%A3%E5%86%B3ctags%E5%AF%B9php%E7%9A%84%E6%94%AF%E6%8C%81



# ctags --help

You’ll soon see that Ctags is a very powerful tool - there are many available arguments, though I’ll just use one of the two most commonly used commands here - the (-R) recursive command. There are several strategies for generating and using Ctags in projects, particularly when it comes to multi level directory hierarchies. You’ll find some help here on the Ctags Sourceforge FAQ here. I am using the ‘local tag file in each directory only for the source files in that directory’ method.

# cd /myprojects/project_a
# ctags -R

Once Ctags has finished generating the tags file, you are in business and ready to start editing in Vim. So fire up Vim and open a PHP file. To see what Ctags has generated for the file, type TlistToggle in command mode and hit CTRL + w twice to change focus to the Ctags window. You can then navigate up and down the tree and hit enter to go straight to that point in the source file.

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/1638/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2019-1-11 18:19
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]