Tuesday, February 28, 2012

Re: Vim Script that displays site-packages source python

Colin Wood, Tue 2012-02-28 @ 17:44:25-0800:
> Maybe I am not saying it right. Ill try again. I am looking to be able
> to get source of a file that is installed in the site-packages
> directory. Instead of opening the file and finding where it is in the
> package I want to be able to do :PySource
> django.views.generic.DetailView and it open a new buffer or window
> with the source of the DetialView. Or something similar?

It's quick and dirty, and would probably fail in some corner case, but
would something like this work?

function! s:PySource(module)
let path = substitute(a:module, '\v(\w+)\.', '\1/', 'g')
execute 'view /usr/lib/python2.7/site-packages/' .
\ substitute(path, '/\w\+$', '', '') .
\ '.py'
execute '/\%(class\|def\)' substitute(path, '\v.*/(\w+)$', '\1', '')
endfunction
command! -nargs=1 PySource call s:PySource('<args>')

It may require tweaking to work on your particular system (though it
works on my machine), but this should at least give you an idea of how
you might approach it.

No comments: