You are not logged in.

#1 2010-11-22 16:41:42

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

[solved] vim-fu: create super call in python

When it comes to scripting vim, I'm absolutely pathetic. When it comes to finding vim script that do what I need, I'm brilliant. But I've failed on this particular task. I'd like to have an abbreviation to create super calls in Python 2. Basically, I need to know what the previous class definition was so that I can insert it into the current location.

Here's what a python class looks like (whitespace matters):

class SomeClassIdentifier(SuperClassIdentifier):  # or just class SomeClassIdentifier:
    def someMethod(self, somearg, someotherarg):
        super(SomeClassIdentifier, self).someMethod(somearg, someotherarg)

There can be any number of lines between the class definition and the super calls, and there may be other lines between the method definition and the super call.

Here's what I want to do:
Given the following lines of code,

class SomeClassIdentifier(SuperClassIdentifier):  # or just class SomeClassIdentifier:
    # possibly some lines
    def someMethod(self, somearg, someotherarg):
        # possibly some lines
        | <--- cursor is here

If I type the characters sup<space>, I want a vim script that:
* goes to the most recent class definition (ie: class SomeClassIdentifier)
* extracts the class name as a variable <class_name>
* go to the most recent method definition (ie: def someMethod)
* extract the method name as a variable <method_name>
* inserts the string "super(<class_name>, self).<method_name>(" at the current cursor.

Does anyone feel up to creating this?

Thanks,
Dusty

Last edited by Dusty (2010-11-22 19:25:12)

Offline

#2 2010-11-22 17:36:56

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: [solved] vim-fu: create super call in python

Adding this to my vimrc "works" (for your test case anyway) but it's ugly and would be better done in a script:

let @q = "maasuper(^[?^\\s*class ?e^Ml\"cyw'aA^Rc, self).^[?^\\s*def ?e^Ml\"myw'aA^Rm("
iabbr sup <ESC>@q

The only caveat is that because I used an abbreviation, the final ( will have a space after it.  It also steps on some registers: a, c, m, and q although three of these are necessary as far as I can tell.  The macro is stored in q, position stored in a, class in c and method in m.  You could have the method and class use the same register though.

* Note that ^[, ^M and ^R must be entered as control characters (<C-v><C-X>), not as text.

Edit:  If you are using snipMate this actually might be quite simple to implement as a snippet.

Edit 2:  Here are a couple functions that you can use with snipMate to do this with a snippet instead:

function! GetCurrentPythonClass()                                                                                                                                                                               
    call search('^\s*class ', 'sbe')                                                                                                                                                                            
    let class = expand('<cword>')                                                                                                                                                                               
    normal ''                                                                                                                                                                                                   
    return class                                                                                                                                                                                                
endfunction                                                                                                                                                                                                     
                                                                                                                                                                                                                
function! GetCurrentPythonMethod()                                                                                                                                                                              
    call search('^\s*def ', 'sbe')                                                                                                                                                                              
    let method = expand('<cword>')                                                                                                                                                                              
    normal ''                                                                                                                                                                                                   
    return method                                                                                                                                                                                               
endfunction     

And the snippet:

snippet sup
⇥super(`GetCurrentPythonClass()`, self).`GetCurrentPythonMethod()`(${1}

Note: ⇥ is a tabstop

Last edited by rson451 (2010-11-22 18:32:40)


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#3 2010-11-22 19:24:45

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: [solved] vim-fu: create super call in python

Thanks a mint, those snippet functions are exactly what I wanted, and I didn't have to spend any time debugging my own vim scripts. I can owe you one!

Dusty

Last edited by Dusty (2010-11-22 19:27:41)

Offline

Board footer

Powered by FluxBB