Bolster
Bolster

Reputation: 7916

Grep-ing classes / functions from projects

Is there a way in grep (or vim) to print out a named function/class?

i.e. From:

    class InternalTimer(Sim.Process):
        def __init__(self, fsm):
            Sim.Process.__init__(self, name="Timer")
            random.seed()
            self.fsm = fsm

        def Lifecycle(self, Request):
            while True:
                yield Sim.waitevent, self, Request
                yield Sim.hold, self, Request.signalparam[0]
                if(self.interrupted()):
                    self.interruptReset()
                else:
                self.fsm.process(Request.signalparam[1])

Calling $my-func-grep '__init__(self,fsm)' filename.py would produce

        def __init__(self, fsm):
            Sim.Process.__init__(self, name="Timer")
            random.seed()
            self.fsm = fsm

Upvotes: 1

Views: 214

Answers (1)

Simeon Visser
Simeon Visser

Reputation: 122486

You could create a vim extension which effectively performs the following:

import inspect
print inspect.getsource(name_of_function)

This prints the function signature and the body of the function. If Vim has been compiled with Python support, you can write extensions in Python itself.

Upvotes: 3

Related Questions