资讯专栏INFORMATION COLUMN

`Leaderf gtags`:模糊匹配与最强静态符号索引工具的完美结合

hsluoyz / 1990人阅读

摘要:也就是,是一个非常强大的源码符号索引工具。韦大的中符号索引篇对有比较详细的介绍,本文再做一些补充。的最新版本已经是,该版本了韦大文中提到的下面文件名大小写的。

Gtags

Gtags也就是GNU GLOBAL,是一个非常强大的源码符号索引工具。它通过建立索引数据库,不但可以查找函数的定义,还可以查找函数的所有引用(被调用的地方);而且它还可以增量地更新索引数据库,当代码有所改变时,它可以在很短的时间内更新索引数据库,保持索引数据库和代码同步。
韦大的 Vim 8 中 C/C++ 符号索引:GTags 篇 对 gtags 有比较详细的介绍,本文再做一些补充。

GLOBAL-6.6.3 released

Gtags的最新版本已经是6.6.3,该版本 fix了韦大文中提到的 Windows下面文件名大小写的 bug。

在 Linux上,不配置let $GTAGSCONF = "/path/to/share/gtags/gtags.conf"也可以正常工作。

当项目文件的路径包含非ASCII字符时,使用pygments会报UnicodeEncodeError: "latin-1" codec can"t encode characters in position 5-8: ordinal not in range(256)

自动生成Gtags索引数据库

LeaderF 可以自己管理 gtags 数据库(GTAGS,GRTAGS,GPATH),它不会在你的项目目录下生成任何额外的文件或目录。gtags 数据库文件存储在$HOME/.LfCache/gtags/%PATH%OF%YOUR%PROJECT/下面, %PATH%OF%YOUR%PROJECT 是把你项目路径中的 / 替换成 %
只要设置let g:Lf_GtagsAutoGenerate = 1, LeaderF 就会在打开第一个文件时自动生成 gtags 数据库。当代码有更改并且已经有 gtags 数据库生成时,更改的代码会自动同步到 gtags 数据库(即使g:Lf_GtagsAutoGenerate是0)。
只有在项目根目录下有g:Lf_RootMarkers(默认值是[".git", ".hg", ".svn"])里面指定的文件或目录时,LeaderF 才会自动生成 gtags 数据库;否则只能手动生成 gtags 数据库:Leaderf gtags --update,但是当代码有更改时,gtags 数据库依然可以自动更新。

Leaderf gtags 使用介绍

具体使用方法可以用:Leaderf gtags -h来查看。

</>复制代码

  1. usage:
  2. Leaderf[!] gtags [-h] [--remove] [--recall]
  3. Leaderf[!] gtags --update [--gtagsconf ] [--gtagslabel
  4. [--skip-unreadable] [--skip-symlink []] [--gtagslibpath [ ...]]
  5. Leaderf[!] gtags [--current-buffer | --all-buffers | --all] [--result ] [COMMON_OPTIONS]
  6. Leaderf[!] gtags -d [--auto-jump []] [-i] [--literal] [--path-style ] [-S ]
  7. [--append] [--match-path] [--gtagsconf ] [--gtagslabel
  8. Leaderf[!] gtags -r [--auto-jump []] [-i] [--literal] [--path-style ] [-S ]
  9. [--append] [--match-path] [--gtagsconf ] [--gtagslabel
  10. Leaderf[!] gtags -s [-i] [--literal] [--path-style ] [-S ] [--append]
  11. [--match-path] [--gtagsconf ] [--gtagslabel
  12. Leaderf[!] gtags -g [-i] [--literal] [--path-style ] [-S ] [--append]
  13. [--match-path] [--gtagsconf ] [--gtagslabel
  14. Leaderf[!] gtags --by-context [-i] [--literal] [--path-style ] [-S ] [--append]
  15. [--match-path] [--gtagsconf ] [--gtagslabel
  16. [COMMON_OPTIONS]: [--reverse] [--stayOpen] [--input | --cword]
  17. [--top | --bottom | --left | --right | --belowright | --aboveleft | --fullScreen]
  18. [--nameOnly | --fullPath | --fuzzy | --regexMode] [--nowrap]
  19. optional arguments:
  20. -h, --help show this help message and exit
  21. specific arguments:
  22. --update Create tag files if tag files do not exist, update the tag files otherwise.
  23. --remove Remove the tag files generated.
  24. --accept-dotfiles Accept files and directories whose names begin with a dot. By default, gtags
  25. ignores them.
  26. --skip-unreadable Skip unreadable files.
  27. --gtagsconf Set environment variable GTAGSCONF to .
  28. --gtagslabel
  29. --skip-symlink []
  30. Skip symbolic links. If type is "f" then skip only symbolic links for file, else
  31. if "d" then skip only symbolic links for directory. The default value of type is
  32. "a" (all symbolic links).
  33. --gtagslibpath [ ...]
  34. Specify the paths to search for library functions.
  35. -d , --definition
  36. Show locations of definitions.
  37. -r , --reference
  38. Show reference to a symbol which has definitions.
  39. -s , --symbol
  40. Show reference to a symbol which has no definition.
  41. -g , --grep
  42. Show all lines which match to the .
  43. --by-context Decide tag type by context at cursor position. If the context is a definition of
  44. the pattern then use -r, else if there is at least one definition of the pattern
  45. then use -d, else use -s. Regular expression is not allowed for pattern.
  46. -i, --ignore-case Ignore case distinctions in the pattern.
  47. --literal Execute literal search instead of regular expression search.
  48. --path-style
  49. Show path names using , which may be one of: `relative`, `absolute`,
  50. `shorter`, `abslib` or `through`. `relative` means relative path. `absolute`
  51. means absolute path. `shorter` means the shorter one of relative and absolute
  52. path. `abslib` means absolute path for libraries (GTAGSLIBPATH) and relative path
  53. for the rest. `through` means the relative path from the project root directory
  54. (internal format of GPATH). The default is `relative`.
  55. -S , --scope
  56. Show only tags which exist under directory.
  57. --recall Recall last search. If the result window is closed, reopen it.
  58. --match-path Match the file path when fuzzy searching.
  59. --append Append to the previous search results.
  60. --current-buffer Show tags in current buffer.
  61. --all-buffers Show tags in all listed buffers.
  62. --all Show tags in the whole project.
  63. --result Show result using format, which may be one of: `ctags`(default), `ctags-x`,
  64. `ctags-mod`.
  65. --auto-jump [] Jump to the tag directly when there is only one match. can be "h", "v" or
  66. "t", which mean jump to a horizontally, vertically split window, or a new tabpage
  67. respectively. If is omitted, jump to a position in current window.
  68. common arguments:
  69. --reverse show results in bottom-up order
  70. --stayOpen don"t quit LeaderF after accepting an entry
  71. --input specifies INPUT as the pattern inputted in advance
  72. --cword current word under cursor is inputted in advance
  73. --top the LeaderF window is at the top of the screen
  74. --bottom the LeaderF window is at the bottom of the screen
  75. --left the LeaderF window is at the left of the screen
  76. --right the LeaderF window is at the right of the screen
  77. --belowright the LeaderF window is at the belowright of the screen
  78. --aboveleft the LeaderF window is at the aboveleft of the screen
  79. --fullScreen the LeaderF window takes up the full screen
  80. --nameOnly LeaderF is in NameOnly mode by default
  81. --fullPath LeaderF is in FullPath mode by default
  82. --fuzzy LeaderF is in Fuzzy mode by default
  83. --regexMode LeaderF is in Regex mode by default
  84. --nowrap long lines in the LeaderF window won"t wrap
  85. --next Jump to the next result.
  86. --previous Jump to the previous result.
  87. If [!] is given, enter normal mode directly.

注意:如果:Leaderf后面有感叹号,会直接进入normal模式;如果没有感叹号,则是输入模式,此时可以输入字符来进行模糊匹配过滤。可以用tab键在两个模式间来回切换。

手动生成gtags数据库

</>复制代码

  1. Leaderf[!] gtags --update [--gtagsconf ] [--gtagslabel
  2. [--skip-unreadable] [--skip-symlink []] [--gtagslibpath [ ...]]

此命令完全异步,不会卡住你的UI。

--gtagsconf

用来指定 gtags.conf 文件的路径,一般情况下不需要指定,默认值就可以很好地工作。对于Windows上,如果相对于gtags.exe所在路径有../share/gtags/gtags.conf,也不需要指定该选项。如果需要用户自己特有的针对 gtags 的配置,可以指定用户的配置文件。

也可以在vimrc里设置g:Lf_Gtagsconf达到同样的目的。

--gtagslabel

用来指定gtagslabel,如果不指定,默认值是 "default"是 gtags.conf 中的:

default
使用内置parser,只支持 6 种语言(C,C++,Java,PHP4,Yacc,汇编)。

ctags
使用exuberant-ctags作为语言parser,支持 40+ 种语言,只能生成定义索引不能生成引用索引。

new-ctags
使用universal-ctags作为语言parser,支持 100+ 种语言,只能生成定义索引不能生成引用索引。虽然貌似universal-ctags已经支持生成引用tags,但是依然不能配合gtags工作(见这里),我也试了各种操作都没成功,也许是因为这个PR没有被merge。

pygments
使用pygments作为语言parser,号称支持300+种语言。

native-pygments
对于原生支持的6种语言使用内置parser,其他语言使用pygments作为parser。

等等

</>复制代码

  1. 也可以在`vimrc`里设置`g:Lf_Gtagslabel`达到同样的目的。

--gtagslibpath [ ...]

用来指定项目所用 library 的 Paths,这样就可以生成 library 的索引,查找定义或引用时可以跳转到 library 代码中去。后面指定的路径还可以是一个或多个其他项目路径,跳转时可以跳到其他项目中的文件。

查找tags

</>复制代码

  1. Leaderf[!] gtags [--current-buffer | --all-buffers | --all] [--result ] [COMMON_OPTIONS]

此命令可以列出当前buffer、所有打开的buffer或者整个项目的tags。

Leaderf[!] gtags等同于Leaderf[!] gtags --all,列出整个项目的tags。

--result 指定显示格式,可以是ctags(default), ctags-x或者ctags-mod

ctags格式

ctags-x格式

ctags-mod格式

查找定义、引用

</>复制代码

  1. Leaderf[!] gtags -d [--auto-jump []] [-i] [--literal] [--path-style ] [-S ]
  2. [--append] [--match-path] [--gtagsconf ] [--gtagslabel
  3. Leaderf[!] gtags -r [--auto-jump []] [-i] [--literal] [--path-style ] [-S ]
  4. [--append] [--match-path] [--gtagsconf ] [--gtagslabel

可以是正则表达式。

--auto-jump [] 意思是如果只有一个结果直接跳过去。

其他

Leaderf[!] gtags -g 功能已被Leaderf rg包含。

Leaderf gtags --nextLeaderf gtags --previous相当于quickfix的:cnext:cprevious命令,在LeaderF结果窗口关闭的情况下也可以使用。

更多内容请参考:Leaderf gtags -h 和 doc。

使用示例

</>复制代码

  1. let g:Lf_GtagsAutoGenerate = 1
  2. let g:Lf_Gtagslabel = "native-pygments"
  3. noremap fr :=printf("Leaderf! gtags -r %s --auto-jump", expand(""))
  4. noremap fd :=printf("Leaderf! gtags -d %s --auto-jump", expand(""))
  5. noremap fo :=printf("Leaderf! gtags --recall %s", "")
  6. noremap fn :=printf("Leaderf gtags --next %s", "")
  7. noremap fp :=printf("Leaderf gtags --previous %s", "")

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/25115.html

相关文章

  • 让人相见恨晚vim插件:模糊查找神器LeaderF

    摘要:是什么是一个用写的插件,可以在成千上万数十万个文件中,通过模糊查找的方式,快速找到目标文件。它还有很多衍生功能快速打开或定位某个最近使用的文件包括函数类变量等命令历史文件中的某一行的等等。友情链接,也許是最好的模糊查詢插件 提到vim的模糊查找插件,很多人第一反应是ctrlp.vim,ctrlp知名度很高,但跟其它的同类插件相比,它的唯一优点是用vimL编写(这让它的性能是所有同类插件...

    Near_Li 评论0 收藏0
  • vim模糊查找插件LeaderF新功能介绍

    摘要:前言上一篇文章让人相见恨晚的插件模糊查找神器概括性的介绍了的基本功能,本篇对的新功能做一下介绍。空格分隔的每个子串都是对整个字符串的模糊匹配,而不是精确匹配。 前言 上一篇文章《让人相见恨晚的vim插件:模糊查找神器LeaderF》概括性的介绍了LeaderF的基本功能,本篇对LeaderF的新功能做一下介绍。 统一的命令接口 随着LeaderF功能的不断增多,LeaderF的命令也在...

    junnplus 评论0 收藏0
  • 众里寻她千百度--正则表达式

    摘要:如果经过一系列输入,最终如果能达到状态,则输入内容一定满足正则表达式。正则表达式可以转换为,已经有成熟的算法实现这一转换。不过有时候转换为可能导致状态空间的指数增长,因此直接用识别正则表达式。 原文地址 先来看一个让人震撼的小故事,故事来自知乎问题PC用户的哪些行为让你当时就震惊了? 同学在一个化妆品公司上班,旁边一个大妈(四十多岁)发给他一个exl表,让他在里面帮忙找一个经销商的资料...

    golden_hamster 评论0 收藏0
  • Java面试 32个核心必考点完全解析

    摘要:如问到是否使用某框架,实际是是问该框架的使用场景,有什么特点,和同类可框架对比一系列的问题。这两个方向的区分点在于工作方向的侧重点不同。 [TOC] 这是一份来自哔哩哔哩的Java面试Java面试 32个核心必考点完全解析(完) 课程预习 1.1 课程内容分为三个模块 基础模块: 技术岗位与面试 计算机基础 JVM原理 多线程 设计模式 数据结构与算法 应用模块: 常用工具集 ...

    JiaXinYi 评论0 收藏0

发表评论

0条评论

hsluoyz

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<