Re: [Pydev-code] Getting started with PyDev scripting
Brought to you by:
fabioz
From: Fabio Z. <fa...@gm...> - 2006-04-12 23:54:52
|
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 fo= r 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 <yo...@if...> 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 =3D=3D 'onCreateActions': > import re > from org.eclipse.jface.action import Action #@UnresolvedImport > from org.eclipse.jface.dialogs import MessageDialog #@UnresolvedImport > 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 =3D msg > def __str__(self): > return self.msg > > class AssignToAttribsOfSelf(Action): > _oEditor =3D editor > _rOK =3D re.compile(r'^\s+def\s+\w+\(self.*\)\s*:\s*$') > def run(self): > oSelection =3D PySelection(self._oEditor) > sCurrentLine =3D oSelection.getCursorLineContents() > try: > if not self._rOK.match(sCurrentLine): > msg =3D "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 =3D oSelection.getInsideParentesisToks(False) > lsParams =3D oParamInfo.o1 > iOffset =3D oParamInfo.o2 > if len(lsParams) =3D=3D 0: > msg =3D "This method def has no parameters, so > consequently there is nothing to assign to atributes." > raise ScriptUnapplicableError(msg) > sPreviousIndent =3D PySelection.getIndentationFromLine > (sCurrentLine) > sIndent =3D PyAction.getStaticIndentationString() > sTotalIndent =3D sPreviousIndent + sIndent > sTemplate =3D sTotalIndent + "self.%s =3D %s" > sNewLine =3D PyAction.getDelimiter(oSelection.getDoc()) > sAssignments =3D sNewLine.join([sTemplate % (s,s) for s in > lsParams]) > iLine =3D oSelection.getLineOfOffset(iOffset) > oSelection.addLine(sAssignments, iLine) > except ScriptUnapplicableError, e: > title =3D "Script Unapplicable" > header =3D "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 th= is > action with: Ctrl+2 ex2<ENTER> > > > |