3

Is there a quick way to add a gettext() call to a literal string in the PyDev editor running under Eclipse? I.e. when I place the cursor on any literal 'string' in a Python file, I want to turn this into _('string') with a single keypress. Can I use macros or something like that to add such functions?

4

3 回答 3

1

在 PyDev 中使用一些简单的 Python 脚本应该可以做到这一点。

看看: http: //pydev.org/manual_articles_scripting.html(可以使用https://github.com/aptana/Pydev/blob/master/plugins/org.python.pydev.jython/jysrc/pyedit_import_to_string。 py为例)。

对于文本选择,可以在以下位置找到 PySelection 实现:https ://github.com/aptana/Pydev/blob/master/plugins/org.python.pydev.core/src/org/python/pydev/core/docutils /PySelection.java(因此,您可以查看 getSelectedText 并使用您自己的版本来获取您想要的文本)。

于 2012-07-16T15:26:55.203 回答
0

这是使用Vrapper的另一个解决方案:

  :map gt ca'_(<esc>pa)<esc>

请注意,这仅适用于单引号字符串,当您使用双引号或多行字符串时它无法识别。

于 2012-07-18T17:12:30.287 回答
0

这是一个小 PyDev 脚本,我可以根据 Fabio 提供的提示创建它。如果您按 Ctrl+2,t,则光标位置处的文字字符串将被 gettext 调用包围。我不确定我是否按预期使用 Java API,但它对我有用。如果您有改进的想法,请发表评论。

if cmd == 'onCreateActions':
    from org.eclipse.jface.action import Action
    from org.python.pydev.core import IPythonPartitions
    from org.python.pydev.core.docutils import ParsingUtils, PySelection

    class AddGettext(Action):
        """Add gettext call around literal string at cursor position."""

        GETTEXT = '_'

        def run(self):
            sel = PySelection(editor)
            doc = sel.getDoc()
            pos = sel.getAbsoluteCursorOffset()
            ctype = ParsingUtils.getContentType(doc, pos)
            if ctype == IPythonPartitions.PY_SINGLELINE_STRING1:
                char, multi = "'", False
            elif ctype == IPythonPartitions.PY_SINGLELINE_STRING2:
                char, multi = '"', False
            elif ctype == IPythonPartitions.PY_MULTILINE_STRING1:
                char, multi = "'", True
            elif ctype == IPythonPartitions.PY_MULTILINE_STRING2:
                char, multi = '"', True
            else:
                char = None
            if char:
                par = ParsingUtils.create(doc)
                if multi:
                    start = par.findPreviousMulti(pos, char)
                    end = par.findNextMulti(pos, char)
                else:
                    start = par.findPreviousSingle(pos, char)
                    end = par.findNextSingle(pos, char)
                doc.replace(end + 1, 0, ')')
                doc.replace(start, 0, self.GETTEXT + '(')

    ACTIVATION_STRING = 't'
    WAIT_FOR_ENTER = False

    editor.addOfflineActionListener(
        ACTIVATION_STRING, AddGettext(), 'Add gettext call', WAIT_FOR_ENTER)
于 2012-07-18T15:21:01.780 回答