2012-06-15 6 views

Antwort

14

können Sie den PyDev Code-Formatierer mit Ctrl +Umschalttaste aktivieren + F (Vorlieben an: Fenster> Einstellungen> PyDev> Editor> Code-Style> Code Formatter - Sie können es auch ermöglichen, automatisch arbeiten). Der interne PyDev-Codeformatierer ist jedoch ziemlich konservativ und wird nicht alle Transformationen durchführen, die für 100% kompatiblen PEP8-Code erforderlich sind (obwohl er die üblicheren Fälle behandelt), wenn Sie also nicht für Ihre Bedürfnisse ausreichen haben einige Optionen:

  1. Sie können autopep8.py verwendet werden, die auch in PyDev standardmäßig in der neuesten Version integriert ist (aktiviert über Fenster> Einstellungen> PyDev> Editor> Code-Style> Code Formatter> Verwendung autopep8. py für die Codeformatierung?)

  2. Sie können einen Blick auf PythonTidy (externes Tool) ... ist es möglich, sie zu verwenden, wie in definiert: http://bear330.wordpress.com/2007/10/30/using-pythontidy-in-pydev-as-code-formatter/

4

ich ein Drehbuch gemacht haben macht es möglich, autopep8 in pydev als Codeformatierer zu verwenden, und es kann angepasst werden, um den Codierungsstandard in Ihrem Team zu erfüllen.

Wenn Sie es verwenden möchten, speichern Sie diesen Code irgendwo als pyedit_autopep8.py (pyedit_XXXX.py ist erforderlich). Sie müssen auch die Python-Pakete pep8 und autopep8 installieren.

Als nächstes gehen pydev Einstellungsseite (unter: Fenster> Einstellungen> pydev> Scripting pydev) zu verdunkeln, um anzugeben das Skript Lage:

Um nun autopep8 Sie einfach Strg + + F Umschalttaste drücken aufzurufen, während der Bearbeitung von Python-Code in Eclipse. Format ausgewählten Text wird ebenfalls unterstützt!

""" 
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

Als Anmerkung, autopep8.py ist derzeit standardmäßig in PyDev integriert (es muss nur in den Einstellungen aktiviert werden). –

Verwandte Themen