|
1 | 1 | " -*- vim -*-
|
2 | 2 | " 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 |
6 | 6 |
|
7 | 7 | " USAGE:
|
8 | 8 | "
|
@@ -279,54 +279,117 @@ function! PythonNextLine(direction)
|
279 | 279 | execute "normal ".ln."G"
|
280 | 280 | endfunction
|
281 | 281 |
|
282 |
| - |
283 |
| -" Update the IM-Python menu, that holds Classes and Functions |
284 | 282 | 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 |
285 | 292 | let restore_fe = &foldenable
|
286 | 293 | set nofoldenable
|
| 294 | + " preserve disposition of window and cursor |
287 | 295 | 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" |
291 | 305 | let &foldenable = restore_fe
|
292 | 306 | endfunction
|
293 | 307 |
|
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 |
307 | 331 | norm j
|
308 | 332 | endwhile
|
309 |
| - norm 'p |
310 | 333 | endfunction
|
311 | 334 |
|
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) |
321 | 344 | 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 |
324 | 366 | endif
|
325 |
| - norm j |
| 367 | + let i = i + 1 |
326 | 368 | 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>' |
328 | 380 | endfunction
|
329 | 381 |
|
| 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 | + |
330 | 393 | function! s:JumpToAndUnfold(line)
|
331 | 394 | " Go to the right line
|
332 | 395 | execute 'normal '.a:line.'gg'
|
|
0 commit comments