Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 28th, 2013  |  syntax: None  |  size: 3.61 KB  |  views: 98  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. """
  2. This code is public domain.
  3. The original author is Bear Huang (http://bear330.wordpress.com/).
  4. """
  5. if False:
  6.     from org.python.pydev.editor import PyEdit #@UnresolvedImport
  7.     cmd = 'command string'
  8.     editor = PyEdit
  9.  
  10. assert cmd is not None
  11. assert editor is not None
  12.  
  13. if cmd == 'onCreateActions':
  14.     # from org.eclipse.jface.action import Action
  15.     from org.python.pydev.editor.actions import PyAction
  16.     from org.python.pydev.core.docutils import PySelection
  17.     from java.lang import Runnable
  18.     from org.eclipse.swt.widgets import Display
  19.     from org.eclipse.jface.text import IDocument
  20.     from org.eclipse.jface.text import TextSelection
  21.  
  22.     from java.io import FileWriter
  23.     import java.lang.Exception
  24.  
  25.     FORMAT_ACTION_DEFINITION_ID = "org.python.pydev.editor.actions.pyFormatStd"
  26.     FORMAT_ACTION_ID = "org.python.pydev.editor.actions.navigation.pyFormatStd"
  27.  
  28.     class PythonTidyAction(PyAction):
  29.  
  30.         def __init__(self, *args, **kws):
  31.             PyAction.__init__(self, *args, **kws)
  32.  
  33.         def run(self):
  34.             import tempfile
  35.             import os
  36.  
  37.             try:
  38.                 ps = PySelection(editor)
  39.                 doc = ps.getDoc()
  40.                 startLine = ps.getStartLineIndex()
  41.  
  42.                 p1 = tempfile.mktemp()
  43.                 p2 = tempfile.mktemp()
  44.                 f1 = FileWriter(p1)
  45.  
  46.                 formatAll = False
  47.                 if ps.getTextSelection().getLength() == 0:
  48.                     # format all.
  49.                     c = doc.get()
  50.                     f1.write(c)
  51.                     formatAll = True
  52.                 else:
  53.                     # format selection.
  54.                     #c = ps.getSelectedText()
  55.                     #f1.write(ps.getSelectedText())
  56.                     print "Format selected text is not supported yet."
  57.                     f1.write("")
  58.                     # A kind of solution is to insert a special comment in
  59.                     # front and end of selection text, pythontidy it, and
  60.                     # extract text according that comment.
  61.  
  62.                 f1.close()
  63.                 os.system('PythonTidy.py "%s" "%s"' % (p1, p2))
  64.                 f2 = open(p2, "r")
  65.                 result = f2.read()
  66.                 f2.close()
  67.  
  68.                 os.remove(p1)
  69.                 os.remove(p2)
  70.  
  71.                 if startLine >= doc.getNumberOfLines():
  72.                     startLine = doc.getNumberOfLines() - 1
  73.  
  74.                 if formatAll:
  75.                     doc.set(result)
  76.                 else:
  77.                     #doc.replace(doc.getLineOffset(startLine), 0, result)
  78.                     pass
  79.  
  80.                 sel = TextSelection(doc, doc.getLineOffset(startLine), 0)
  81.                 self.getTextEditor().getSelectionProvider().setSelection(sel)
  82.             except java.lang.Exception, e:
  83.                 self.beep(e)
  84.  
  85.     def bindInInterface():
  86.         act = PythonTidyAction()
  87.  
  88.         act.setActionDefinitionId(FORMAT_ACTION_DEFINITION_ID)
  89.         act.setId(FORMAT_ACTION_ID)
  90.         try:
  91.             editor.setAction(FORMAT_ACTION_ID, act)
  92.         except:
  93.             pass
  94.  
  95.     class RunInUi(Runnable):
  96.         '''Helper class that implements a Runnable (just so that we
  97.         can pass it to the Java side). It simply calls some callable.
  98.         '''
  99.  
  100.         def __init__(self, c):
  101.             self.callable = c
  102.         def run(self):
  103.             self.callable ()
  104.  
  105.     def runInUi(callable):
  106.         '''
  107.         @param callable: the callable that will be run in the UI
  108.         '''
  109.         Display.getDefault().asyncExec(RunInUi(callable))
  110.  
  111.     runInUi(bindInInterface)
clone this paste RAW Paste Data