Skip to content

Instantly share code, notes, and snippets.

@fabioz
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabioz/d398cfa30960285c869c to your computer and use it in GitHub Desktop.
Save fabioz/d398cfa30960285c869c to your computer and use it in GitHub Desktop.
Example command to create a 'surround with' action in PyDev
if False:
from org.python.pydev.editor import PyEdit #@UnresolvedImport
cmd = 'command string'
editor = PyEdit
systemGlobals = {}
if cmd == 'onCreateActions':
assert editor is not None
MySurroundAction = systemGlobals.get('MySurroundAction')
if MySurroundAction is None:
Action = editor.getActionClass() #from org.eclipse.jface.action import Action #@UnresolvedImport
class MySurroundAction(Action):
def __init__(self, editor):
self.editor = editor
def run(self):
editor = self.editor
sel = editor.createPySelection()
txt = sel.getSelectedText()
doc = sel.getDoc()
sel = sel.getTextSelection()
doc.replace(sel.getOffset(), sel.getLength(), 'my_func(%s)' % (txt,))
systemGlobals['MySurroundAction'] = MySurroundAction
# Change these constants if the default does not suit your needs
ACTIVATION_STRING = 'x'
WAIT_FOR_ENTER = False
# Register the extension as an ActionListener.
editor.addOfflineActionListener(ACTIVATION_STRING, MySurroundAction(editor), 'Surround with function', WAIT_FOR_ENTER)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment