使用 cscope 通过 VIM 浏览 Python 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3718868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using cscope to browse Python code with VIM?
提问by Eli Bendersky
Has anyone managed successfully using cscopewith Python code? I have VIM 7.2 and the latest version of cscopeinstalled, however it doesn't get my code's tags correctly (always off by a couple of lines). I tried the pycscopescript but its output isn't supported by the modern version of cscope.
有没有人成功地使用cscopePython 代码?我已经安装了 VIM 7.2 和最新版本cscope,但是它没有正确获取我的代码标签(总是差几行)。我尝试了该pycscope脚本,但现代版本的cscope.
Any ideas? Or an alternative for browsing Python code with VIM? (I'm specifically interested in the extra features cscopeoffers beyond the simple tags of ctags)
有任何想法吗?或者使用 VIM 浏览 Python 代码的替代方法?(我cscope对简单标签之外的额外功能特别感兴趣ctags)
采纳答案by Eli Bendersky
- From a correspondence with the maintainer of
cscope, this tool isn't designed to work with Python, and there are no plans to implement that compatibility. Whatever works now, apparently works by mistake, and there is no promise whatsoever that it will keep working. - It appears I've been using an out-of-date version of
pycscope. The latest version 0.3 is supported by thecscopeDB. The author ofpycscopetold me that he figured out the output format for thecscopeDB from reading the source code ofcscope. That format isn't documented, on purpose, but nevertheless it currently works withpycsope0.3, which is the solution I'll be using.
- 根据与 维护者的通信
cscope,此工具并非设计用于与 Python 一起使用,并且没有计划实现该兼容性。无论现在有效,显然都是错误的,并且没有任何保证它会继续工作。 - 看来我一直在使用过时版本的
pycscope.cscopeDB支持最新版本 0.3 。的作者pycscope告诉我,他是cscope通过阅读cscope. 故意没有记录该格式,但它目前适用于pycsope0.3,这是我将使用的解决方案。
I'm going to accept this answer since unfortunately no other answer provided help even after bounty was declared. No answers are upvoted, so I honestly have no idea where the bounty will go.
我将接受这个答案,因为不幸的是,即使在宣布赏金之后,也没有其他答案提供帮助。没有答案被赞成,所以老实说,我不知道赏金会去哪里。
回答by chiggsy
EDIT: I'm going to run through the process step by step:
编辑:我将逐步完成整个过程:
Preparing the sources:
准备来源:
exhuberant ctags, has an option: -x
旺盛的 ctags,有一个选项:-x
Alternatively, ctags can generate a cross reference file which lists,
in human readable form, information about the various source objects
found in a set of language files.
This is the key to the problem:
这是问题的关键:
ctags -x $(ls **/*.py); # replace with find if no zsh
will give you your database of source objects in a known, format, described under
将以已知的格式为您提供源对象的数据库,如下所述
man ctags; # make sure you use exuberant ctags!
Gnu Global is not limited to only the "out of the box" type of files. Any regular file format will serve.
Gnu Global 不仅限于“开箱即用”类型的文件。将提供任何常规文件格式。
Also, you can use gtags-cscope, which comes with global as mentioned in section 3.7 of the manual, for a possible shortcut using gtags. You'll end up with an input of a ctags tabular file which Global/gtags can parse to get your objects, or you can use the source for pycscope together with your ctags file of known format to get an input for the vim cscope commands in
此外,您可以使用 gtags-cscope,它随手册第 3.7 节中提到的 global 提供,作为使用 gtags 的可能快捷方式。您最终会得到一个 ctags 表格文件的输入,Global/gtags 可以解析该文件以获取您的对象,或者您可以使用 pycscope 的源代码和已知格式的 ctags 文件来获取 vim cscope 命令的输入
if_cscope.txt.
if_cscope.txt。
Either way it's quite doable.
无论哪种方式,它都非常可行。
Perhaps you'd prefer idutils?
也许您更喜欢 idutils?
Definintely possible since
绝对有可能,因为
z3c.recipe.tags
z3c.recipe.tags
on pypi makes use of both ctags and idutils to create tag files for a buildout, which is a method I shall investigate in short while.
在 pypi 上同时使用 ctags 和 idutils 为构建创建标记文件,这是我将在短时间内研究的一种方法。
Of course, you could always use the greputils script below, it has support for idutils , we know idutils works with python, and if that fails, there is also something called vimentry from this year that also uses python, idutils and vim.
当然,你总是可以使用下面的 greputils 脚本,它支持 idutils ,我们知道 idutils 与 python 一起工作,如果失败,今年还有一个叫做 vimentry 的东西也使用 python、idutils 和 vim。
Reference links (not complete list):
参考链接(不完整列表):
- gtags vimscript, uses Gnu global. updated 2008
- greputils vimscript, contains support for the *id idutils, 2005
- lid vimscript, Ancient, but this guy is pretty good, his tag and buffer howtos are amazing 2002
- An updated version of pyscope, 2010
- gtags vimscript,使用 Gnu 全局。2008 年更新
- greputils vimscript,包含对 *id idutils 的支持,2005
- 盖子 vimscript,古老,但是这家伙很不错,他的标签和缓冲区操作方法很棒 2002
- pyscope 的更新版本,2010
Hopefully this helps you with your problem, I certainly helped me. I would have been quite sad tonight with a maggoty pycscope.
希望这可以帮助您解决问题,我当然帮助了我。今晚我会很伤心,因为有一只蛆虫。
回答by Joseph Salisbury
This seems to work for me:
这似乎对我有用:
Change to the top directory of your python code. Create a file called cscope.files:
切换到 Python 代码的顶级目录。创建一个名为 的文件cscope.files:
find . -name '*.py' > cscope.files
cscope -R
You may need to perform a cscope -bfirst if the cross references don't get built properly.
cscope -b如果没有正确构建交叉引用,您可能需要先执行。
回答by shengy
I got the same question you got, after browsing the internet, I found a way to fix this:
我遇到了同样的问题,在浏览互联网后,我找到了解决此问题的方法:
create a python script: cscope_scan.py
创建一个python脚本: cscope_scan.py
import os
codeRootDir = os.getcwd()
__revision__ = '0.1'
__author__ = 'lxd'
FILE_TYPE_LIST= ['py']
if __name__ == '__main__':
import os
f = open('cscope.files','w')
for root,dirs,files in os.walk(codeRootDir):
for file in files:
for file_type in FILE_TYPE_LIST:
if file.split('.')[-1] == file_type:
f.write('%s\n' %os.path.join(root,file))
f.close()
cmd = 'cscope -bk'
os.system(cmd)
excute this script under you code's root folder, this will generate the cscope.filesand then excute cscope -bI don't know what happens to my computer, the last two lines aren't working well, but I think manually type a cscope -bkis acceptable:)
在你代码的根文件夹下执行这个脚本,这将生成cscope.files然后执行cscope -b我不知道我的电脑会发生什么,最后两行工作不正常,但我认为手动键入 acscope -bk是可以接受的:)
回答by ZyX
There is a wonderful Python-mode-klenplugin. If you have it and rope (python refactoring library) installed, then going to the definition of a particular term is as simple as <C-c>gor <C-c>rag(first is filetype mapping, second is a global one). There are much more useful features, some useless for me. All of them are disableable. Features from list of questions found at cscope-intro:
有一个很棒的Python-mode-klen插件。如果您安装了它并安装了绳索(python 重构库),那么转到特定术语的定义就像<C-c>gor一样简单<C-c>rag(第一个是文件类型映射,第二个是全局映射)。还有更多有用的功能,有些对我没用。所有这些都是禁用的。在cscope-intro 中找到的问题列表中的功能:
- Where is this symbol used?<C-c>f. Rather confusing though, as results in quickfix list do show
-instead of the actual lines (though they point to the correct location). Maybe it will be fixed. - Where is it defined?, What is this global symbol's definition?, Where is this function in the source files?<C-c>g
- What is <...> global symbol's definition?<C-c>raj
- 这个符号用在什么地方?<C-c>f. 虽然相当令人困惑,因为 quickfix 列表中的结果确实显示
-而不是实际的行(尽管它们指向正确的位置)。也许它会被修复。 - 它在哪里定义?,这个全局符号的定义是什么?,这个函数在源文件中的什么地方?<C-c>g
- <...> 全局符号的定义是什么?<C-c>raj
Not very much, but I am not too experienced user of ropevim.
不是很多,但我对ropevim 的使用经验不是很丰富。

