有没有办法解决pydev的PEP-8问题?

时间:2012-06-15 06:59:10

标签: pydev

我想知道是否有办法在eclipse-pydev中使用键盘快捷键自动修复所有PEP-8问题。谷歌搜索并没有把我带到任何地方。

由于Pydev可以检测到PEP-8问题,是否应该无法自动修复它们?

2 个答案:

答案 0 :(得分:14)

你可以用 Ctrl + Shift + F 激活PyDev代码格式化程序(首选项: Window> Preferences&gt ; PyDev>编辑器>代码样式>代码格式化程序 - 您甚至可以让它自动工作。)

尽管如此,内部PyDev代码格式化程序非常保守,并不会完成100%兼容PEP8代码所需的所有转换(尽管它处理更常见的情况),因此,如果它不足以满足您的需求,那么一些选择:

  1. 您可以使用autopep8.py,默认情况下也会在最新版本中集成到PyDev中(通过 Window> Preferences> PyDev> Editor> Code Style> Code Formatter>使用autopep8.py进行代码格式化?

  2. 您可以查看PythonTidy(外部工具)......可以按照以下定义使用它:http://bear330.wordpress.com/2007/10/30/using-pythontidy-in-pydev-as-code-formatter/

答案 1 :(得分:4)

我制作了一个脚本,可以在pydev中使用autopep8作为代码格式化程序, 它可以定制,以满足您团队中的编码标准。

如果要使用它,请将此代码保存为pyedit_autopep8.py(需要pyedit_XXXX.py)。您还必须安装python包pep8和autopep8。

接下来,转到eclipse pydev首选项页面(at:window> preferences> pydev> scripting pydev)来指定脚本位置:

现在,为了调用autopep8,你可以在eclipse中编辑python代码时按Ctrl + Shift + F.格式化所选文本也受支持!

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