Using cscope to browse Python code with VIM?
Asked Answered
S

6

19

Has anyone managed successfully using cscope with Python code? I have VIM 7.2 and the latest version of cscope installed, however it doesn't get my code's tags correctly (always off by a couple of lines). I tried the pycscope script but its output isn't supported by the modern version of cscope.

Any ideas? Or an alternative for browsing Python code with VIM? (I'm specifically interested in the extra features cscope offers beyond the simple tags of ctags)

Sessile answered 15/9, 2010 at 14:55 Comment(5)
Have you considered simply fixing pycscope? I haven't looked at what needs to be done, but I can't imagine it being too difficult. Also, you may want to update your question to mention that, as your blog post says, cscope is thrown off by Python comments.Sprawl
@ssokolow: The problem with fixing pycscope is that the "DB" format cscope uses isn't documented, and according to the maintainer of cscope this is by designSessile
Ahh. That WOULD be unpleasant. I'd have to think about what solution I'd attempt in that situation.Sprawl
Even trying to modify cscope is a disaster. I can find the code used to identify comments, but I think it would be a mess to try and include # and """ in the comment code. Perhaps the easiest way would be to preprocess the source files and strip the comments (replacing them with blank lines). But again, that's not very fun because cscope doesn't work with standard input.Fluter
Doxygen natively supports only C and Java, but through PREFILTER option it is able to process any programming language files. Filtering app needs to be written for the language. I also submitted (as patch #3413344), prefilter option to ctags. Maybe it would be nice to have it in cscope also.Flatter
S
12
  1. 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.
  2. It appears I've been using an out-of-date version of pycscope. The latest version 0.3 is supported by the cscope DB. The author of pycscope told me that he figured out the output format for the cscope DB from reading the source code of cscope. That format isn't documented, on purpose, but nevertheless it currently works with pycsope 0.3, which is the solution I'll be using.

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.

Sessile answered 23/9, 2010 at 15:20 Comment(2)
shrugs You wanted an alternative to cscope, which would be idutils. They perform similar tasks.Formalism
@chiggsy: AFAIU idutils is more a replacement for grep than for cscopeSessile
F
18

EDIT: I'm going to run through the process step by step:

Preparing the sources:

exhuberant ctags, has an option: -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.

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

if_cscope.txt.

Either way it's quite doable.

Perhaps you'd prefer idutils?

Definintely possible since

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.

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.

Reference links (not complete list):

Hopefully this helps you with your problem, I certainly helped me. I would have been quite sad tonight with a maggoty pycscope.

Formalism answered 20/9, 2010 at 16:37 Comment(4)
gnu global doesn't support Python, grep utils obviously isn't what I'm looking for... why do you link to pycscope's source code?Sessile
Why do you use $(ls **/*.py) instead of **/*.py on its own? AFAIK in zsh they are equivalent, and will both expand to the list of matching files in all subdirectories.Signally
I've decided to grant you the bounty since you've done a solid amount of research trying to help me solve the problem. Keep up the good work on SOSessile
"something called vimentry" - is this referring to exVim, which has your_project.vimentry files? That's all I could find from googling.Immobility
A
18

This seems to work for me:

Change to the top directory of your python code. Create a file called cscope.files:

find . -name '*.py' > cscope.files

cscope -R

You may need to perform a cscope -b first if the cross references don't get built properly.

Apospory answered 23/11, 2010 at 16:28 Comment(1)
Great Tip! Works with PHP too, for me: ("PHP and cscope in vim"): find . -name '*.php' > cscope.files && cscope -Rb -- -b for saving the results (in cscope.out and using them in vim).Scrivenor
S
12
  1. 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.
  2. It appears I've been using an out-of-date version of pycscope. The latest version 0.3 is supported by the cscope DB. The author of pycscope told me that he figured out the output format for the cscope DB from reading the source code of cscope. That format isn't documented, on purpose, but nevertheless it currently works with pycsope 0.3, which is the solution I'll be using.

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.

Sessile answered 23/9, 2010 at 15:20 Comment(2)
shrugs You wanted an alternative to cscope, which would be idutils. They perform similar tasks.Formalism
@chiggsy: AFAIU idutils is more a replacement for grep than for cscopeSessile
B
2

There is a wonderful Python-mode-klen plugin. 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>g or <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:

  1. 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.
  2. Where is it defined?, What is this global symbol's definition?, Where is this function in the source files? <C-c>g
  3. What is <...> global symbol's definition? <C-c>raj

Not very much, but I am not too experienced user of ropevim.

Bushy answered 26/5, 2012 at 18:41 Comment(2)
I can find <C-c>g in their documentation, but not <C-c>f. Could you please provide a reference, or the actual command that is called when pressing <C-c>f?Flong
@YoniZohar github.com/klen/python-mode/blob/…, then github.com/klen/python-mode/blob/…. I do not recall where I got this mapping in the first place though, git log --patch doc/pymode.txt shows that it was never in documentation.Bushy
M
0

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

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.files and then excute cscope -b I don't know what happens to my computer, the last two lines aren't working well, but I think manually type a cscope -bk is acceptable:)

Missie answered 30/3, 2012 at 7:46 Comment(0)
I
0

This hack also seems to force cscope to go through Python files:

cscope -Rb -s *

If you accept that cscope is apparently not designed to work with Python.

Superset any language any tool question: How to find all occurrences of a variable in Vim?

Icebound answered 22/4, 2020 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.