2012-06-15 36 views

Trả lời

14

Bạn có thể kích hoạt các PyDev mã định dạng với Ctrl +phím Shift +F (ưu tiên tại địa chỉ: Window> Preferences> PyDev> Editor> Mã Phong cách> Mã Formatter - bạn thậm chí có thể kích hoạt nó để hoạt động tự động).

Tuy nhiên, trình định dạng mã PyDev bên trong khá bảo thủ và sẽ không thực hiện tất cả các phép biến đổi cần thiết cho mã PEP8 tương thích 100% (mặc dù nó xử lý các trường hợp phổ biến hơn), vì vậy, nếu nó không đủ cho nhu cầu của bạn, bạn có một số tùy chọn:

  1. Bạn có thể sử dụng autopep8.py mà cũng được tích hợp vào PyDev theo mặc định trong phiên bản mới nhất (Enabled qua Window> Preferences> PyDev> Editor> Mã Phong cách> Mã Formatter> sử dụng autopep8. py để định dạng mã?)

  2. Bạn có thể hãy nhìn vào PythonTidy (công cụ bên ngoài) ... nó có thể sử dụng nó như quy định tại: http://bear330.wordpress.com/2007/10/30/using-pythontidy-in-pydev-as-code-formatter/

4

Tôi đã thực hiện một kịch bản làm cho nó có thể sử dụng autopep8 trong PyDev như mã định dạng, và nó có thể được tùy chỉnh để đáp ứng tiêu chuẩn mã hóa trong nhóm của bạn.

Nếu bạn muốn sử dụng, hãy lưu mã này ở đâu đó như pyedit_autopep8.py (pyedit_XXXX.py là bắt buộc). Bạn cũng phải cài đặt các gói python pep8 và autopep8.

Tiếp theo, hãy làm lu mờ trang sở thích PyDev (tại địa chỉ: Window> Preferences> PyDev> scripting PyDev) để xác định các kịch bản vị trí:

Bây giờ, để gọi autopep8 bạn chỉ có thể nhấn tổ hợp phím Ctrl + Shift + F trong khi chỉnh sửa mã python trong nhật thực. Định dạng văn bản đã chọn cũng được hỗ trợ!

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

Lưu ý, autopep8.py hiện được tích hợp vào PyDev theo mặc định (nó chỉ cần được bật trong tùy chọn). –

Các vấn đề liên quan