Hi Joel,
 
Nicely done, works very well for me (asside from the docstring issue you mentioned), so... congratulations!!!
 
The only thing that I saw that you shouldn't do is storing another editor reference in your class (self._oEditor), you should just use the editor variable directly (it will always be available in your namespace).
 
Now the only thing missing is checking if there is some docstring. You can do that in the following way: Check if there is some ' or ", get its absolute position in the document and then you can use:
 
from org.python.pydev.core.docutils import ParsingUtils
ParsingUtils.eatLiterals(document, StringBuffer(), initialOffsetOfLiteral)
 
The contents of the docstring will be on the passed StringBuffer and the returned value will be the absolute offset to the docstring end.
 
So, what were your first impressions on pydev scripting? I'd really like to know ways to improve it... Probably creating a more 'pythonic' interface for some common operations will be in that list, but this will come with time, so, aside from that, what do you think?
 
Cheers,
 
Fabio
 
On 4/12/06, Joel Hedlund <[EMAIL PROTECTED]> wrote:
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