Skip to content

Commit 97ed3fc

Browse files
Mikael Berthevim-scripts
Mikael Berthe
authored andcommitted
Version 1.8
Applied a patch from Jon Franklin: IM-Python menu improvements. Updated copyright and email address.
1 parent 7a5bb24 commit 97ed3fc

File tree

1 file changed

+98
-35
lines changed

1 file changed

+98
-35
lines changed

Diff for: plugin/python.vim

+98-35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
" -*- vim -*-
22
" FILE: python.vim
3-
" LAST MODIFICATION: 2003/07/25 19:00
4-
" (C) Copyright 2001-2003 Mikael Berthe <[email protected]>
5-
" Version: 1.7
3+
" LAST MODIFICATION: 2006-08-18 07:30
4+
" (C) Copyright 2001-2005 Mikael Berthe <[email protected]>
5+
" Version: 1.8
66

77
" USAGE:
88
"
@@ -279,54 +279,117 @@ function! PythonNextLine(direction)
279279
execute "normal ".ln."G"
280280
endfunction
281281

282-
283-
" Update the IM-Python menu, that holds Classes and Functions
284282
function! UpdateMenu()
283+
" delete menu if it already exists, then rebuild it.
284+
" this is necessary in case you've got multiple buffers open
285+
" a future enhancement to this would be to make the menu aware of
286+
" all buffers currently open, and group classes and functions by buffer
287+
if exists("g:menuran")
288+
aunmenu IM-Python
289+
else
290+
let g:menuran=1
291+
endif
285292
let restore_fe = &foldenable
286293
set nofoldenable
294+
" preserve disposition of window and cursor
287295
let cline=line('.')
288-
call MakeClassStructure ()
289-
call MakeFuncStructure ()
290-
execute "normal ".cline."Gzz"
296+
let ccol=col('.') - 1
297+
norm H
298+
let hline=line('.')
299+
" create the menu
300+
call MenuBuilder()
301+
" restore disposition of window and cursor
302+
exe "norm ".hline."Gzt"
303+
let dnscroll=cline-hline
304+
exe "norm ".dnscroll."j".ccol."l"
291305
let &foldenable = restore_fe
292306
endfunction
293307

294-
" Make a menu that holds all of the classes
295-
function! MakeClassStructure ()
296-
norm mpgg0
297-
while line(".") <= line("$")
298-
if match ( getline("."), '^\s*class\s\+' ) != -1
299-
norm ^w"nyw
300-
let name=@n
301-
"exe 'menu IM-Python.classes.'.name.' '.line(".").'gg15zo'
302-
exe 'menu IM-Python.classes.'.name.' :call <SID>JumpToAndUnfold('.line(".").')<CR>'
303-
endif
304-
if line(".") == line("$")
305-
return
306-
endif
308+
function! MenuBuilder()
309+
norm gg0
310+
let currentclass = -1
311+
let classlist = []
312+
let parentclass = ""
313+
while line(".") < line("$")
314+
" search for a class or function
315+
if match ( getline("."), '^\s*class\s\+[a-zA-Z].*:\|^\s*def\s\+[a-zA-Z].*:' ) != -1
316+
norm ^
317+
let linenum = line('.')
318+
let indentcol = col('.')
319+
norm "nye
320+
let classordef=@n
321+
norm w"nywge
322+
let objname=@n
323+
let parentclass = FindParentClass(classlist, indentcol)
324+
if classordef == "class"
325+
call AddClass(objname, linenum, parentclass)
326+
else " this is a function
327+
call AddFunction(objname, linenum, parentclass)
328+
endif
329+
call RebuildClassList(classlist, [objname, indentcol], classordef)
330+
endif " line matched
307331
norm j
308332
endwhile
309-
norm 'p
310333
endfunction
311334

312-
" Make a menu that holds all of the function definitions
313-
function! MakeFuncStructure ()
314-
norm mpgg0
315-
while line(".") <= line("$")
316-
if match ( getline("."), '^\s*def\s\+' ) != -1
317-
norm ^w"nyw
318-
let name=@n
319-
"exe 'menu IM-Python.functions.'.name.' '.line(".").'gg15zo'
320-
exe 'menu IM-Python.functions.'.name.' :call <SID>JumpToAndUnfold('.line(".").')<CR>'
335+
" classlist contains the list of nested classes we are in.
336+
" in most cases it will be empty or contain a single class
337+
" but where a class is nested within another, it will contain 2 or more
338+
" this function adds or removes classes from the list based on indentation
339+
function! RebuildClassList(classlist, newclass, classordef)
340+
let i = len(a:classlist) - 1
341+
while i > -1
342+
if a:newclass[1] <= a:classlist[i][1]
343+
call remove(a:classlist, i)
321344
endif
322-
if line(".") == line("$")
323-
return
345+
let i = i - 1
346+
endwhile
347+
if a:classordef == "class"
348+
call add(a:classlist, a:newclass)
349+
endif
350+
endfunction
351+
352+
" we found a class or function, determine its parent class based on
353+
" indentation and what's contained in classlist
354+
function! FindParentClass(classlist, indentcol)
355+
let i = 0
356+
let parentclass = ""
357+
while i < len(a:classlist)
358+
if a:indentcol <= a:classlist[i][1]
359+
break
360+
else
361+
if len(parentclass) == 0
362+
let parentclass = a:classlist[i][0]
363+
else
364+
let parentclass = parentclass.'\.'.a:classlist[i][0]
365+
endif
324366
endif
325-
norm j
367+
let i = i + 1
326368
endwhile
327-
norm 'p
369+
return parentclass
370+
endfunction
371+
372+
" add a class to the menu
373+
function! AddClass(classname, lineno, parentclass)
374+
if len(a:parentclass) > 0
375+
let classstring = a:parentclass.'\.'.a:classname
376+
else
377+
let classstring = a:classname
378+
endif
379+
exe 'menu IM-Python.classes.'.classstring.' :call <SID>JumpToAndUnfold('.a:lineno.')<CR>'
328380
endfunction
329381

382+
" add a function to the menu, grouped by member class
383+
function! AddFunction(functionname, lineno, parentclass)
384+
if len(a:parentclass) > 0
385+
let funcstring = a:parentclass.'.'.a:functionname
386+
else
387+
let funcstring = a:functionname
388+
endif
389+
exe 'menu IM-Python.functions.'.funcstring.' :call <SID>JumpToAndUnfold('.a:lineno.')<CR>'
390+
endfunction
391+
392+
330393
function! s:JumpToAndUnfold(line)
331394
" Go to the right line
332395
execute 'normal '.a:line.'gg'

0 commit comments

Comments
 (0)