Ctags: 如何从 ctags -R -x 的输出中解析标记名和种类

创建于 2019-03-12  ·  8评论  ·  资料来源: universal-ctags/ctags


解析器名称:任意一个

用于运行 ctags 的命令行:

$ ctags --options=NONE -R -x

输入文件内容:

$ git clone https://github.com/liuchengxu/vista.vim
$ cd vista.vim
$ ctags --options=NONE -R -x

您不满意的标签输出:

我不知道如何解析ctags -R -x的输出。 我想从ctags -R -x的输出中过滤标记名和范围,但不知道如何正确操作。

您期望的标签输出:

如果ctags -R也有特定格式是可行的:

{tagname}<Tab>{tagfile}<Tab>{tagaddress}[;"<Tab>{tagfield}..]

ctags的版本:

$ ctags --version
Universal Ctags 0.0.0(1208e45), Copyright (C) 2015 Universal Ctags Team
Universal Ctags is derived from Exuberant Ctags.
Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Mar 11 2019, 17:01:27
  URL: https://ctags.io/
  Optional compiled features: +wildcards, +regex, +iconv, +option-directory, +xpath, +case-insensitive-filenames, +packcc

你如何获得 ctags 二进制文件:

brew install ctags在 macOS 上

所有8条评论

我不明白你的问题。

您对ctags -R的输出满意吗? 如果是,为什么不使用-x选项?
我想更多地了解用例的背景来回答您的问题。

我不知道 vim 脚本。 但是,就读取 ctags 的 vim 解析器 vim.c 而言,它不会填充范围字段。 如果您认为这是一个错误,请告诉我们 vim 解析器填充范围字段以获取什么样的输入。

抱歉没有说清楚。

为什么使用-x选项?

虽然ctags -R可以满足我的需要,但我不想生成标签文件,所以我使用了-x选项。

我的用例。

目前 vista.vim 只支持显示某个文件中的标签,但有些用户希望显示一个项目中的所有标签(https://github.com/liuchengxu/vista.vim/issues/14)。 我尝试添加ctags -R支持,但不知道如何正确提取这些输出。

总之,如何分别提取vista#util#Trimfunction18autoload/vista/util.vimvista#util#Trim(str)

ctags -R -x :

...
vista#util#Trim  function     18 autoload/vista/util.vim function! vista#util#Trim(str)
vista#util#Truncate function     13 autoload/vista/util.vim function! vista#util#Truncate(msg) abort
vista#util#Warning function     90 autoload/vista/util.vim function! vista#util#Warning(msg) abort
vista#viewer#Display function     90 autoload/vista/viewer.vim function! vista#viewer#Display(data) abort
vista#viewer#Render function     86 autoload/vista/viewer.vim function! vista#viewer#Render(data) abort
vista#viewer#prefixes function     94 autoload/vista/viewer.vim function! vista#viewer#prefixes() abort

使用-o -选项,ctags 将输出发送到标准输出,如:

[yamato@slave]/tmp/vista.vim% u-ctags -R -o - | head 
/   ftplugin/vista.vim  /^nnoremap <buffer> <silent> \/    :<c-u>call vista#finder#fzf#Run()<CR>$/;"    m
<CR>    ftplugin/vista.vim  /^nnoremap <buffer> <silent> <CR> :<c-u>call vista#cursor#FoldOrJump()<CR>$/;"  m
Commands    README.md   /^### Commands$/;"  S
Contributing    README.md   /^## Contributing$/;"   s
Features    README.md   /^## Features$/;"   s
Installation    README.md   /^## Installation$/;"   s
Introduction    README.md   /^## Introduction$/;"   s
License README.md   /^## License$/;"    s
NeoVim  README.md   /^#### NeoVim$/;"   t
Options README.md   /^### Options$/;"   S

您可以使用--_xformat=选项自定义输出:

 u-ctags -R -x --_xformat='TAGNAME:%N @@@ KIND:%K !!! LINE:%n +++ INPUT-FILE:%F === PATTERN:%P' | head
TAGNAME:/ @@@ KIND:map !!! LINE:38 +++ INPUT-FILE:ftplugin/vista.vim === PATTERN:/^nnoremap <buffer> <silent> \/    :<c-u>call vista#finder#fzf#Run()<CR>$/
TAGNAME:<CR> @@@ KIND:map !!! LINE:37 +++ INPUT-FILE:ftplugin/vista.vim === PATTERN:/^nnoremap <buffer> <silent> <CR> :<c-u>call vista#cursor#FoldOrJump()<CR>$/
TAGNAME:Commands @@@ KIND:subsection !!! LINE:101 +++ INPUT-FILE:README.md === PATTERN:/^### Commands$/
TAGNAME:Contributing @@@ KIND:section !!! LINE:164 +++ INPUT-FILE:README.md === PATTERN:/^## Contributing$/
TAGNAME:Features @@@ KIND:section !!! LINE:37 +++ INPUT-FILE:README.md === PATTERN:/^## Features$/
TAGNAME:Installation @@@ KIND:section !!! LINE:69 +++ INPUT-FILE:README.md === PATTERN:/^## Installation$/
TAGNAME:Introduction @@@ KIND:section !!! LINE:25 +++ INPUT-FILE:README.md === PATTERN:/^## Introduction$/
TAGNAME:License @@@ KIND:section !!! LINE:168 +++ INPUT-FILE:README.md === PATTERN:/^## License$/
TAGNAME:NeoVim @@@ KIND:subsubsection !!! LINE:90 +++ INPUT-FILE:README.md === PATTERN:/^#### NeoVim$/
TAGNAME:Options @@@ KIND:subsection !!! LINE:113 +++ INPUT-FILE:README.md === PATTERN:/^### Options$/

请参阅http://docs.ctags.io/en/latest/news.html?highlight=--_xformat#customizing -xref-output 。

如果您不想编写解析器,可以选择--output-format=json

u-ctags -R --output-format=json | head
{"_type": "tag", "name": "/", "path": "ftplugin/vista.vim", "pattern": "/^nnoremap <buffer> <silent> \\/    :<c-u>call vista#finder#fzf#Run()<CR>$/", "kind": "map"}
{"_type": "tag", "name": "<CR>", "path": "ftplugin/vista.vim", "pattern": "/^nnoremap <buffer> <silent> <CR> :<c-u>call vista#cursor#FoldOrJump()<CR>$/", "kind": "map"}
{"_type": "tag", "name": "Commands", "path": "README.md", "pattern": "/^### Commands$/", "kind": "subsection"}
{"_type": "tag", "name": "Contributing", "path": "README.md", "pattern": "/^## Contributing$/", "kind": "section"}
{"_type": "tag", "name": "Features", "path": "README.md", "pattern": "/^## Features$/", "kind": "section"}
{"_type": "tag", "name": "Installation", "path": "README.md", "pattern": "/^## Installation$/", "kind": "section"}
{"_type": "tag", "name": "Introduction", "path": "README.md", "pattern": "/^## Introduction$/", "kind": "section"}
{"_type": "tag", "name": "License", "path": "README.md", "pattern": "/^## License$/", "kind": "section"}
{"_type": "tag", "name": "NeoVim", "path": "README.md", "pattern": "/^#### NeoVim$/", "kind": "subsubsection"}
{"_type": "tag", "name": "Options", "path": "README.md", "pattern": "/^### Options$/", "kind": "subsection"}

谢谢! 我会尝试--_xformat=选项,因为 brew 安装的 $# ctags --output-format=json ,无论是brew install ctags还是brew install --HEAD universal-ctags/universal-ctags/universal-ctags

@KazuakiM ,是否可以将 ctags 链接到 libjansson.so.4?
u-ctags 有 json 输出模式。 如果 u-ctags 链接到 libjansson,则它可用。
告诉我是否应该将此作为 homebrew-universal-ctags 回购的新问题报告。

@liuchengxu ,标签文件,ctags的原生格式可以用readtags.c中的函数解析。
readtags 命令和 python-ctags3,python 库使用 readtags.c。

@masatake
嗨,homebrew-universal-ctags 支持 jansson。

$ brew info universal-ctags
universal-ctags/universal-ctags/universal-ctags: HEAD
Maintained ctags implementation
https://github.com/universal-ctags/ctags
Conflicts with:
  ctags (because this formula installs the same executable as the ctags formula)
/usr/local/Cellar/universal-ctags/HEAD-1b747b3 (325 files, 4.3MB) *
  Built from source on 2019-02-02 at 16:45:17
From: https://github.com/universal-ctags/homebrew-universal-ctags/blob/master/universal-ctags.rb
==> Dependencies
Build: autoconf ✔, automake ✔, pkg-config ✔
Optional: jansson ✔, libyaml ✔
==> Options
--with-jansson
        Build with jansson support
--with-libyaml
        Build with libyaml support
--without-doc
        Compile without man pages
--without-xml
        Compile without libxml2
--HEAD
        Install HEAD version
==> Caveats
Under some circumstances, emacs and ctags can conflict. By default,
emacs provides an executable `ctags` that would conflict with the
executable of the same name that ctags provides. To prevent this,
Homebrew removes the emacs `ctags` and its manpage before linking.
However, if you install emacs with the `--keep-ctags` option, then
the `ctags` emacs provides will not be removed. In that case, you
won't be able to install ctags successfully. It will build but not
link.

@KazuakiM很好,我错过了帮助。 JSON 支持需要选项--with-jansson

$ brew reinstall --with-jansson universal-ctags/universal-ctags/universal-ctags
此页面是否有帮助?
0 / 5 - 0 等级

相关问题

fommil picture fommil  ·  19评论

jayceekay picture jayceekay  ·  13评论

trevordmiller picture trevordmiller  ·  9评论

songouyang picture songouyang  ·  15评论

EvanCarroll picture EvanCarroll  ·  14评论