score:1

Accepted answer

it should be possible to do that with some simple python scripting inside pydev.

take a look at: http://pydev.org/manual_articles_scripting.html (you can use https://github.com/aptana/pydev/blob/master/plugins/org.python.pydev.jython/jysrc/pyedit_import_to_string.py as an example).

for the text selection, the pyselection implementation may be found at: https://github.com/aptana/pydev/blob/master/plugins/org.python.pydev.core/src/org/python/pydev/core/docutils/pyselection.java (so, you can see how getselectedtext and work your own version to get the text you want).

score:0

here is a little pydev script that i was able to create with the hints given by fabio. if you press ctrl+2,t then the literal string at the cursor position will be surrounded with a gettext call. i'm not sure whether i'm using the java api as expected, but it works for me. if you have ideas for improvement, please comment.

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)

score:0

here is another solution using vrapper:

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

note that this only works with single-quoted strings, it does not recognize when you use double quotes or multi-line strings.


Related Query

More Query from same tag