# decorator to tie delegate and TextCommand together
# expects a view instance as first arg to delegate/decorated function.
def asTextCommand(f):
def runThruTextCommand(*args, **kwargs):
i = sublimeplugin.textCommands["textCommandRunner"]
i.prime(f, args, kwargs)
args[0].runCommand("textCommandRunner")
return runThruTextCommand
# Makes sure to run the delegate thru a TextCommand.run method
class TextCommandRunner(sublimeplugin.TextCommand):
def run(self, view, args):
if not hasattr(self, 'f'): return
self.f(*self.args, **self.kwargs)
del self.f
def prime(self, f, args, kwargs):
self.f = f
self.args = args
self.kwargs = kwargs
# A delegate
@asTextCommand
def replace(view, what, with_this):
view.runCommand("splitSelectionIntoLines")
for r in view.sel():
view.replace(r, re.sub(what, with_this, view.substr(r)))
Users browsing this forum: No registered users and 4 guests