V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
skywind3000
V2EX  ›  Vim

[gutentags_plus] Vim 中使用 gtags 的正确方法

  •  
  •   skywind3000 · 2018-05-22 12:57:30 +08:00 · 9866 次点击
    这是一个创建于 2137 天前的主题,其中的信息可能已经有所发展或是发生改变。

    GNU Global / Gtags 支持 50+ 种编程语言,可以在没有 LSP 或者 LSP 支持不到位的时候为 50+ 种语言提供准确的定义 /引用查找。

    插件地址:

    https://github.com/skywind3000/gutentags_plus

    插件说明:

    Gutentags 提供了后台无缝更新 gtags 数据库的功能,而 gutentags_plus 提供了无缝切换 gtags 数据库的功能:

    • 提供一个 GscopeFind 命令,使用 cscope 接口,通过 gtags-cscope 程序进行符号搜索。
    • 在搜索前自动链接当前项目的 gtags 数据库,并且断开无关项目的数据库。
    • 如果当前项目没有变,那么复用以前的 gtags 数据库链接( gtags 数据库更新后不需要像 cscope 一样复位链接)。

    Gutentags 也提供自动链接 gtags 数据库的功能,但是它会把当前编辑的所有项目的数据库全部添加到 vim 的 cscope 接口中,那么我同时打开多个工程时,查询一个符号,会得到来自所有工程的结果,基本没法看了。所以搜索前必须断开非当前项目的 gtags 数据库链接。

    将 gutentags 搭配 gutentags_plus 一起使用,前者提供 gtags 数据库的无缝更新,后者提供数据库无缝切换。使用的时候你只管在任何时候查询符号的定义和引用即可,数据库的更新和切换对你来说完全透明,再也不用操心了,就和使用其他 IDE 一样。

    使用方法:

    Plug 'ludovicchabant/vim-gutentags'
    Plug 'skywind3000/gutentags_plus'
    
    " enable gtags module
    let g:gutentags_modules = ['ctags', 'gtags_cscope']
    
    " config project root markers.
    let g:gutentags_project_root = ['.root']
    
    " generate datebases in my cache directory, prevent gtags files polluting my project
    let g:gutentags_cache_dir = expand('~/.cache/tags')
    
    " forbid gutentags adding gtags databases
    let g:gutentags_auto_add_gtags_cscope = 0
    

    命令说明:

    :GscopeFind {querytype} {name}
    

    通过 cscope 接口操纵 gtags-cscope 程序进行符号查询,并且在查询前处理好数据库切换问题,只保持当前项目的数据库。

    {querytype} corresponds to the actual cscope line interface numbers as well as default nvi commands:

    0 or s: Find this symbol
    1 or g: Find this definition
    2 or d: Find functions called by this function
    3 or c: Find functions calling this function
    4 or t: Find this text string
    6 or e: Find this egrep pattern
    7 or f: Find this file
    8 or i: Find files #including this file
    9 or a: Find places where this symbol is assigned a value
    

    详细见 github README

    5 条回复    2019-01-09 15:27:31 +08:00
    Yggdroot
        1
    Yggdroot  
       2018-05-24 17:43:48 +08:00
    还没用过 gtags,相对于 ctags 有什么不同和优势?
    SpaceVim
        2
    SpaceVim  
       2018-05-25 11:28:22 +08:00 via Android
    ctags 不支持查引用,只能查定义处
    skywind3000
        3
    skywind3000  
    OP
       2018-05-25 12:20:36 +08:00
    @Yggdroot gtags 可以用来搜索引用,ctags 只能查定义。
    ivechan
        4
    ivechan  
       2018-06-24 12:08:49 +08:00
    尝试使用 gtags,目前感觉良好。
    xiaocang
        5
    xiaocang  
       2019-01-09 15:27:31 +08:00
    麻烦请教一下,使用该插件,可以在头文件中搜索相关函数吗。如 `#include <sys/socket.h>` 中的 `socket` 函数
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   961 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:16 · PVG 05:16 · LAX 14:16 · JFK 17:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.