5

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

既然 Pydev 可以检测到 PEP-8 问题,难道不能自动修复它们吗?

4

2 回答 2

15

Ctrl您可以使用++手动激活 PyDev 代码格式化程序(首选项位于: Window Shift> Preferences > PyDev > Editor > Code Style > Code Formatter - 请注意,您可以将其配置为在Window > Preferences > PyDev > Editor > Save下自动运行行动)。F

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

  1. 您可以使用autopep8.pyorblack在最新版本中默认集成到 PyDev 中(通过Window > Preferences > PyDev > Editor > Code Style > Code Formatter > Formatter Style? 启用?然后选择 autopep8or black)。

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

于 2012-06-25T15:20:44.530 回答
4

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

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

接下来,转到 eclipse pydev 首选项页面(位于: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)
于 2013-09-26T13:57:31.573 回答