2012-06-15 33 views

回答

14

可以激活PyDev的代碼格式與按Ctrl ++˚F(偏好於:窗口>首選項>的PyDev>編輯>代碼樣式>代碼格式化 - 你甚至可以啓用它自動工作)。

儘管如此,內部的PyDev代碼格式化器非常保守,不會完成100%兼容PEP8代碼所需的所有轉換(儘管它處理更常見的情況),因此,如果它不足以滿足您的需求,您有一些選擇:

  1. 您可以使用autopep8.py其中也融入了默認情況下在最新版本的PyDev(通過窗口>首選項>的PyDev>編輯>代碼樣式>代碼格式化>使用autopep8啓用。 py代碼格式?

  2. 您可以看看PythonTidy(外部工具)......這是可以使用它作爲定義:http://bear330.wordpress.com/2007/10/30/using-pythontidy-in-pydev-as-code-formatter/

4

我已經做了腳本,使它可以使用autopep8中的PyDev作爲代碼格式化, 它可以定製以滿足您團隊的編碼標準。

如果您想使用它,請將此代碼保存在pyedit_autopep8.py(需要pyedit_XXXX.py)處。您還必須安裝python軟件包pep8和autopep8。

下一步,進入到eclipse PyDev選項頁(在:窗口>首選項>的PyDev>腳本的PyDev)指定腳本位置:現在

,以便調用autopep8你可以簡單地按Ctrl + Shift + F同時在eclipse中編輯python代碼。格式選定的文本也支持!

""" 
By Per A. Brodtkorb 
based on pyedit_pythontidy.py by Bear Huang (http://bear330.wordpress.com/). 

This code is public domain. 
""" 

import tempfile 
import os 

if False: 
    from org.python.pydev.editor import PyEdit # @UnresolvedImport 
    cmd = 'command string' 
    editor = PyEdit 

assert cmd is not None 
assert editor is not None 

if cmd == 'onCreateActions': 
    from org.python.pydev.editor.actions import PyAction 
    from org.python.pydev.core.docutils import PySelection 
    from java.lang import Runnable 
    from org.eclipse.swt.widgets import Display 
    from java.io import FileWriter 
    import java.lang.Exception 

    FORMAT_ACTION_DEFINITION_ID = "org.python.pydev.editor.actions.pyFormatStd" 
    FORMAT_ACTION_ID = "org.python.pydev.editor.actions.navigation.pyFormatStd" 

    class Autopep8Action(PyAction): 
     def _autopep8(self, text): 
      tmp_full_file_name = tempfile.mktemp() 
      f1 = FileWriter(tmp_full_file_name) 
      f1.write(text) 
      f1.close() 
      os.system('autopep8-script.py -i "%s"' % (tmp_full_file_name)) 
      f2 = open(tmp_full_file_name, "r") 
      tidy_text = f2.read() 
      f2.close() 
      os.remove(tmp_full_file_name) 
      return tidy_text 

     def _get_text(self, selection): 
      text = selection.getSelectedText() 
      format_all = len(text) == 0 
      if format_all: 
       print "Autopep8: format all." 
       text = selection.getDoc().get() 
       text_offset = 0 
      else: 
       print "Autopep8: Format selected." 
       text_offset = selection.getAbsoluteCursorOffset() 
      return text, text_offset 

     def run(self): 
      try: 
       selection = PySelection(editor) 

       text, text_offset = self._get_text(selection) 
       tidy_text = self._autopep8(text) 

       if len(text)==len(tidy_text): 
        print "Autopep8: Nothing todo!" 
       else: 
        doc = selection.getDoc() 
        doc.replace(text_offset, len(text), tidy_text) 

      except java.lang.Exception, e: 
       self.beep(e) 

    def bindInInterface(): 
     act = Autopep8Action() 
     act.setActionDefinitionId(FORMAT_ACTION_DEFINITION_ID) 
     act.setId(FORMAT_ACTION_ID) 
     try: 
      editor.setAction(FORMAT_ACTION_ID, act) 
     except: 
      pass 

    class RunInUi(Runnable): 

     '''Helper class that implements a Runnable (just so that we 
     can pass it to the Java side). It simply calls some callable. 
     ''' 

     def __init__(self, c): 
      self.callable = c 

     def run(self): 
      self.callable() 

    def runInUi(callable): 
     ''' 
     @param callable: the callable that will be run in the UI 
     ''' 
     Display.getDefault().asyncExec(RunInUi(callable)) 

    runInUi(bindInInterface) 
+2

作爲一個說明,autopep8.py目前默認集成到PyDev中(只需要在首選項中啓用它)。 –

相關問題