editing Python files -- how to keep track of class membership?

1 view
Skip to first unread message

Reckoner

unread,
Jul 24, 2009, 5:23:52 PM7/24/09
to vim_use

When editing Python files, I have a hard time keeping track of the
class in which I am changes. This is because the class definition is
somewhere way up there in the file that I have scrolled past.

Anybody have any advice for dealing with this? I am currently just
splitting the current file into two windows so I can keep track of the
class definition in one window. It's ugly, but it works.

I'm sure somebody has done something better, however.

Any advice appreciated.

Thanks in advance.

Pete Johns

unread,
Jul 24, 2009, 9:13:40 PM7/24/09
to vim_use
On Jul 25, 7:23 am, Reckoner <recko...@gmail.com> wrote:
> When editing Python files, I have a hard time keeping track of the
> class in which I am changes. This is because the class definition is
> somewhere way up there in the file that I have scrolled past.
>
> Anybody have any advice for dealing with this? I am currently just
> splitting the current file into two windows so I can keep track of the
> class definition in one window. It's ugly, but it works.
>
Personally, I find having lots of classes in one source file ugly. But
I sympathise, we can't always edit the code we're reading.


> I'm sure somebody has done something better, however.
>
I'm not sure about 'better', but the following mapping should help
you:

:nmap <Leader>c :?^\s*class? mark c\| echo getline("'c")<cr>

This will search back to the last class definition, mark it with 'c,
and echo that line at the bottom of the window without changing your
current cursor position.


> Any advice appreciated.
>
See the following help for a fuller explanation:

:he :nmap
:he <Leader>
:he :?
:he mark
:he echo
:he getline


> Thanks in advance.
>
Hope this helps;


--paj

Reckoner

unread,
Jul 27, 2009, 9:18:01 AM7/27/09
to vim_use
Thanks! Big help!

Is there a way to put this on the statusline?

thanks again.

John Beckett

unread,
Jul 27, 2009, 6:37:28 PM7/27/09
to vim...@googlegroups.com
Please bottom post on this list. Quote a small (relevant) part
of the message you are replying to, and put your text underneath.

See the list guidelines:
http://groups.google.com/group/vim_use/web/vim-information

Pete Johns

unread,
Jul 29, 2009, 6:23:42 PM7/29/09
to vim_use
On Jul 27, 11:18 pm, Reckoner <recko...@gmail.com> wrote:
> Thanks! Big help!
>
Excellent.


> Is there a way to put this on the statusline?
>
Undoubtedly! This is Vim, after all!

Of course, this is a bit more complicated as you want to ensure that
it only displays a match if you are actually within a class. My
previous method involved some user intelligence. For instance, if you
are at the top of a Python file, you don't want to search backwards
and find the last class definition in the file, the user calling the
mapping would know this. Also if you are in a global function beneath
a class definition, you wouldn't want that class definition being
displayed as this would be misleading.

This sounded like an interesting challenge to me and I'd never played
with the statusline before so I delved into the help-files and off I
went. This is what I came up with in a few minutes:

function! GetLastClass()
let l:retval = "[No class]"

let l:last_line_declaring_a_class = search('^\s*class', 'bnW')
let l:last_line_starting_with_a_word_other_than_class = search('^\
(\<\)\@=\(class\)\@!', 'bnW')

if l:last_line_starting_with_a_word_other_than_class <
l:last_line_declaring_a_class
let l:retval = getline(l:last_line_declaring_a_class)
endif

return l:retval
endfunction

setlocal statusline=[%n]\ %{GetLastClass()}\ %<%f%h%m%r%=%b\ 0x%B\ \
%l,%c%V\ %P

This is in my .vim/ftplugin/python.vim.

There are some obvious shortcomings around nested classes but I shall
leave those as an exercise for the reader. I reckon any Python
developer can write Vimscript.

Here are the relevant help pages, which should explain how this little
lot works.

:he 'statusline'
:he /multi " \@= and \@! atoms were not obvious to me
at first glance
:he /ordinary-atom "For \< . In fact the whole of pattern.txt
is essential reading for any Vim user.
:he :function
:he :endfunction
:he :getline
:he :let
:he :return
:he :search
:he :setlocal
:he ftplugin
:he local-variable

> thanks again.
>
You're welcome. I hope this satisfies your requirements. If you solve
the problems with nested classes, I would very much like to see the
solution.

Cheers;

--paj

Yura Machnovskii

unread,
Aug 2, 2009, 4:48:36 AM8/2/09
to vim...@googlegroups.com
I think that next plugin can solve almost your problem:

http://vim.sourceforge.net/scripts/script.php?script_id=435

It has some known problem but as I see it was updated recently. I didn't try new version.

Best regards.
Reply all
Reply to author
Forward
0 new messages