首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法用pydev解决PEP-8问题?

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

Stack Overflow用户
提问于 2012-06-15 14:59:10
回答 1查看 6.3K关注 0票数 5

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

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

EN

回答 1

Stack Overflow用户

发布于 2013-09-26 21:57:32

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

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

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

现在,要调用autopep8,只需在eclipse中编辑python代码时按Ctrl+Shift+F即可。还支持格式化所选文本!

代码语言:javascript
复制
"""
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)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11046001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档