Alright... These are my first stumbling steps into the glorious realm of pydev scripting.

It's supposed to be a little helper for assigning the values of method parameters to attributes of self with the same names, and it's activated through "Ctrl-2" followed by "a<Enter>" ("a" for "assign"). Today it quite happily displaces docstrings and probably also behaves badly by other fascinating means which I still haven't discovered.

I'd appreciate any and all pointers I can get on all this, so I can continue in the right direction.

Take care everybody!
/Joel Hedlund
assert cmd is not None 
assert editor is not None

if cmd == 'onCreateActions':
    import re
    from org.eclipse.jface.action import Action [EMAIL PROTECTED]
    from org.eclipse.jface.dialogs import MessageDialog [EMAIL PROTECTED]
    from org.python.pydev.core.docutils import PySelection
    from org.python.pydev.editor.actions import PyAction
    
    class ScriptUnapplicableError(Exception):
        """Raised when the script is unapplicable to the current line."""
        def __init__(self, msg):
            self.msg = msg
        def __str__(self):
            return self.msg
    
    class AssignToAttribsOfSelf(Action): 
        _oEditor = editor
        _rOK = re.compile(r'^\s+def\s+\w+\(self.*\)\s*:\s*$')
        def run(self):            
            oSelection = PySelection(self._oEditor)            
            sCurrentLine = oSelection.getCursorLineContents()
            try:
                if not self._rOK.match(sCurrentLine):
                    msg = "So far, only one-liner method def lines are 
recognised by this script. The current line is not such a line."
                    raise ScriptUnapplicableError(msg)
                oParamInfo = oSelection.getInsideParentesisToks(False)
                lsParams = oParamInfo.o1
                iOffset = oParamInfo.o2
                if len(lsParams) == 0:
                    msg = "This method def has no parameters, so consequently 
there is nothing to assign to atributes."
                    raise ScriptUnapplicableError(msg)
                sPreviousIndent = 
PySelection.getIndentationFromLine(sCurrentLine)
                sIndent = PyAction.getStaticIndentationString()
                sTotalIndent = sPreviousIndent + sIndent
                sTemplate = sTotalIndent + "self.%s = %s"
                sNewLine = PyAction.getDelimiter(oSelection.getDoc())
                sAssignments = sNewLine.join([sTemplate % (s,s) for s in 
lsParams])
                iLine = oSelection.getLineOfOffset(iOffset)
                oSelection.addLine(sAssignments, iLine)
            except ScriptUnapplicableError, e:
                title = "Script Unapplicable"
                header = "Script: Assign Method Parameters to Attributes of 
self\r\n\r\n"
                MessageDialog.openInformation(editor.getSite().getShell(), 
title, header + str(e))
            
    editor.addOfflineActionListener("a", AssignToAttribsOfSelf(), 'Assign 
method parameters to attributes of self', True) #the user can activate this 
action with: Ctrl+2  ex2<ENTER>

Reply via email to