【转】Android源码阅读利器 vim+cscope+ctags+taglist

源地址:
http://blog.163.com/wikijone@126/blog/static/1631871452010915105511957/
感谢原作者



Linux下的SourceInsight,不过是终端下的,效率更高,使用更流畅,提高效率更明显。她就是大名鼎鼎的:vim+cscope+ctags+taglist。(Ubuntu 10.04.1)
1.安装上述每个软件。
sudo apt-get install vim cscope ctags
下载taglist解压至相应的vim的配置文件夹中。关于环境配置的网络资源众多,不再此赘述。
2.重点是如何使用此环境和Android源码融合。
编辑getdb.sh文件,内容如下,其中需要在执行前设置环境变量ANDROID_SRC为你的android源码根目录。
#!/bin/bash                                                                
#file: mkcscope
#function: make cscope database and ctags database
#author: [email protected]
#date: 20110527
 
ignore="( -name .git -o -name test -o -name tests ) -prune -o"
type="( -name *.java -o -name *.cpp -o -name *.[ch] -o -name *.xml -o -name
*.aidl )"
android=$ANDROID_SRC
path="$android/frameworks \
    $android/packages \
    $android/external \
    $android/system \
    $android/bionic \
    $android/hardware \
    $android/dalvik \
    $android/prebuilt/linux-x86 \
    $android/kernel_imx \
    $android/bootable"
cscopedb=$ANDROID_SRC/cscope
prepare()
{
    if [ ! -d $ANDROID_SRC ]; then
        echo "no ANDROID_SRC set"
        echo "please set ANDROID_SRC at your .bashrc file"
        exit 1
    else
        echo "ANDROID_SRC:$ANDROID_SRC"
        if [ -d $cscopedb ]; then
            echo "$cscopedb exist already"
            if [ -e $cscopedb/cscope.files ]; then
                echo "file $cscopedb/cscope.files exist"
#               rm $cscopedb/cscope.files
            fi
        else
            mkdir $cscopedb
            echo "make directory $cscopedb"
        fi
    fi
    all_source | sort > $cscopedb/cscope.files
    lines=$(wc -l $cscopedb/cscope.files | awk '{ printf $1 }')
    echo "find $lines files totaly"
}
all_source()
{
    for src in $path
    do
    find -L $src $ignore $type -print
    done
}

docscope()
{
    echo "Now begin build cscope cross-index file"
    start=`date +%s`
    cscope -b -k -q -i $cscopedb/cscope.files -f $cscopedb/cscope.out
    end=`date +%s`
    let "elapse=$end-$start"
    if [ $? -eq 0 ]; then
        echo "make cscope database file with total time ($elapse) seconds"
        size=$(du $cscopedb/cscope.out -h | awk '{ printf $1 }')
        echo "($cscopedb/cscope.out):$size"
    fi
}
dotags()
{
    echo "Now begin build tags file"
    start=`date +%s`
    ctags --fields=+afiKlmnsSzt -L $cscopedb/cscope.files -f $cscopedb/tags
    end=`date +%s`
    let "elapse=$end-$start"
    if [ $? -eq 0 ]; then
        echo "make ctags database file with total time ($elapse) seconds"
        size=$(du $cscopedb/tags -h | awk '{ printf $1 }')
        echo "($cscopedb/tags):$size"
    fi
}

usage() 
{
    echo "Usages:"
    echo "$0    cscope  : make cscope database only"
    echo "      tags    : make ctags database only"
    echo "      all : make two databases"
}


case $1 in
    "cscope")
    prepare
    docscope
    ;;

    "tags")
    prepare
    dotags
    ;;  

    "all")
    prepare
    docscope
    dotags
    ;;
    "")
    usage
    ;;
esac



3.执行脚本,构建cscope库文件和ctags文件。
将上述文件放在你的android源码根目录下,确定有可执行树形设置。执行此文件。
./getdb.sh
执行此文件需要大概5分钟左右,和你的cpu速度有很大关系,呵呵,可以在一旁冲一杯咖啡。之后将是你畅游源码乐不思蜀的一个过程。对于Android如此庞大的源码,再也不用惧怕。
4.设置自动加载cscope库和ctags库在.vimrc文件中。
编辑个人目录下的.vimrc文件,加入如下内容。
set csto:0  
set cscopetag
set nocsverb                                            
cs add $ANDROID_SRC/cscope/cscope.out $ANDROID_SRC/cscope
set csverb                                              
set tags:./tags,tags,$ANDROID_SRC/cscope/tags

let mapleader="f"                                        
                                                         
nmap <leader>s : cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <leader>g : cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <leader>c : cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <leader>d : cs find d <C-R>=expand("<cword>")<CR><CR>
nmap <leader>t : cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <leader>e : cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <leader>f : cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <leader>i : cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>


"set cscopequickfix=s-,c-,d-,i-,t-,e-,f-,g-(提醒各位,这一项是不能设置的,否则你的cscope搜索将无法打开搜索文件结果窗口,非常郁闷。不需要设置即可,一般没有用的。)

5.使用vim在Android源码中畅游
相信对使用vim有一定经验的人都知道,使用mapleader可以映射任意的键值,给我们的工作带来极大的便利。如何使用?
使用vim打开你的一个在搜索目录之内的源码文件,移动光标到你感兴趣的方法或类名或任何的标号上,按下f,再按你需要的cscope搜索方式,瞬间打开,可以非常快速定位你需要的文件,打开。这里我们只要输入两个字母即可得出搜索结果,非常快捷。关键在于.vimrc文件中的:
let mapleader="f"
读者可以设置自己任意个性化的键值,再加上后面的固定cscope搜索方式键,即刻到达,畅游Android源码无阻当。
ps:之用CTRL+T或者CTRL+O可以回跳,使用TAB键或者CTRL+I可是向反方向跳,方便实用。在此,使用CTRL+]对于搜索ctags库文件依然有效,输出结果比cscope更详细,能够更方便的确定标签的有效性。

呵呵,发现vim配置学问太深,今有所发现,不敢独享,与众分享。

猜你喜欢

转载自ccsosnfs.iteye.com/blog/1661880