Hello!
I've dropped from radar for a while since I've been busy coding stuff
for my work, but I figured I should share a litte pydev jython hack that
I whipped up. This one lets you assign the value of a variable to an
attribute of self with the same name, pretty much as you could do with
method parameters using my old "Assign params to attribs of self" hack
that was included in pydev 1.0.6.
The old one was good for avoiding repetitive typing when coding large
constructors and such. This one comes in handy for smaller changes, say
when you add a parameter to the constructor. It's nothing major, but
along with code completion, it saves more typing than you'd think.
In the attached code I've bound the jython script to 'a' ('a' for
'assign'), which is the same letter as my old hack. I figured that
letter would be up for grabs now that my old hack has been integrated
into the quick code assistant.
So copy it to your pydev jython script dir, open a new editor and hit
"Ctrl-2 a" and see what you make of it. Let me know what you think!
Cheers!
/Joel
"""Assign variable to attribute of self with same name.
Pydev script for generating code that assigns a variable to an attribute of
self with the same name.
This script is bound to "Ctrl-2 a" in PyDev by default, and must be
executed at an indented line containing only one alphanumeric word in order
to have any effect. Otherwise the script will just fling a popup at you and
make no changes to your code.
Example:
------------------------------------------------------
class Cow(object):
def moo(self, msg):
'''Deliver wisdom verbally, bovine style.'''
msg
------------------------------------------------------
Executing this script at the "msg" line will replace the word "msg" with
an assignment statement such as this: "self.msg = msg".
Note that this script does not check if msg is defined or if the name is
valid in any other way.
"""
__author__ = "Joel Hedlund <[EMAIL PROTECTED]>"
__version__ = "1.0.0"
__copyright__ = """\
This script is available under the same conditions as PyDev.
See PyDev license for details.
"""
__support__ = """Contact the author for bug reports/feature requests."""
# This is a magic trick that tells the PyDev Extensions editor
# about the namespace provided for pydev scripts:
if False:
from org.python.pydev.editor import PyEdit [EMAIL PROTECTED]
cmd = 'command string'
editor = PyEdit
assert cmd is not None
assert editor is not None
from org.python.pydev.jython import ExitScriptException
# 'onSave' can be added to the list for developing purposes.
if cmd not in ['onCreateActions']:
raise ExitScriptException
import re
from java.lang import StringBuffer
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 [EMAIL PROTECTED]
from org.python.pydev.editor.actions import PyAction [EMAIL PROTECTED]
from org.python.pydev.core.docutils import ParsingUtils [EMAIL PROTECTED]
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 AssignToAttribOfSelf(Action):
"""Assign variable to attribute of self with same name.
See module docs for details.
"""
_rName = re.compile(r'^ {8} *(\w+)\s*$')
_sNewline = '\r\n'
def _scriptApplicable(self, selection):
"""Raise ScriptUnapplicableError if the script is unapplicable.
@param selection: The current selection as a PySelection.
"""
sCurrentLine = selection.getCursorLineContents()
if not self._rName.match(sCurrentLine):
sTitle = "Script Unapplicable"
sHeader = "Script: Assign Variable to Attribute of self"
sExample = "Example: 'length' => 'self.length = length'."
sBody = ("This script can only be run if the current line "
"contains exactly one alphanumeric word indented by at "
"least 8 spaces. ")
lsText = [sHeader, '', sExample, '', 'Error:', sBody]
sDialogText = self._sNewline.join(lsText)
oShell = editor.getSite().getShell()
MessageDialog.openInformation(oShell, sTitle, sDialogText)
return False
return True
def run(self):
oSelection = PySelection(editor)
oDocument = editor.getDocument()
self._sNewLine = PyAction.getDelimiter(oDocument)
if not self._scriptApplicable(oSelection):
return None
# Build assignment expression:
sOldLine = oSelection.getCursorLineContents()
sName = self._rName.match(sOldLine).group(1)
sIndent = PySelection.getIndentationFromLine(sOldLine)
sAssignmentLine = sIndent + "self.%s = %s" % (sName, sName)
# Move to insert point:
iStartLineOffset = oSelection.getLineOffset()
iEndLineOffset = iStartLineOffset + len(sOldLine)
editor.setSelection(iEndLineOffset, 0)
oSelection = PySelection(editor)
# Replace the old line with the new assignment expression:
oSelection.replaceLineContentsToSelection(sAssignmentLine)
del oSelection
sDescription = 'Assign variable to attribute of self'
editor.addOfflineActionListener("a", AssignToAttribOfSelf(), sDescription,
False)
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
pydev-code mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pydev-code