Hi, I wrote this script for Eclipse Monkey, an Eclipse plugin, that automates some AS3 development tasks. Details about the mokney here: http://panellabs.net/2008/06/01/generate-actionscript-3-code-with-eclipse-monkey-flex-builder/. I’m posting it here since the ofiicial script exchange site is dead. Note: seems the monkey project is dead but there are efforts to resurect it and fold it into Groovy Monkey(?).
The script will generate a getter, setter, and constructor argument for a given member variable. Setup: install Eclipse Monkey and add the script. Usage: place the cursor on a line where a member variable is declared. Hit Ctrl + 4 and a window with options will pop up.
/*
* Menu: Actionscript > Member Variable Helper
* Key: M1+4
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
* Author: Mihail K
* Version: 1.0
*/
function main() {
var editor = editors.activeEditor;
var variable = parseVariableAtCurrentLine(editor);
if(variable == null)
return;
var varName = variable.name;
var varType = variable.type;
var args = input('Member Variable Helper',
'Use a combination of these options to generate code for the variable \''+ variable.name +'\':'
+'
p - append \'public \' before declaration;
r - append \'private \' before declaration
o - append \'protected \' before declaration;
a - add a constructor argument and initialize;
g - generate getter;
s - generate setter;','args');
var appendDecl = '';
if (args.indexOf('p') != -1) {
appendDecl = 'public ';
} else if (args.indexOf('o') != -1) {
appendDecl = 'protected ';
} else if (args.indexOf('r') != -1) {
appendDecl = 'private ';
}
editor.beginCompoundChange();
if (appendDecl!=''){
var insertPos = editor.source.indexOf('var ' + varName + ':' + varType);
editor.applyEdit(insertPos, 0, appendDecl);
}
var cls = new AS3Class(editor);
if (args.indexOf('a') != -1) {
cls.appendCtorArg(varName+':'+varType);
cls.appendCtorStmnt('this.'+varName+' = '+varName+';');
}
if (args.indexOf('g') != -1)
cls.appendMethod(getGetter(varName, varType));
if (args.indexOf('s') != -1)
cls.appendMethod(getSetter(varName, varType));
editor.endCompoundChange();
}
function AS3Class(editor)
{
this.editor = editor;
this.parse();
}
AS3Class.prototype.parse = function()
{
var re = new RegExp("public\\sfunction\\s(\\w+)\\((.*)\\)([\ \
\\r\\s]*)","g");
var res = re.exec(this.editor.source);
this.name = res[1];
this.ctorHasArgs = res[2].length > 0;
this.ctorArgInsertOffset = re.lastIndex - res[0].length + res[0].lastIndexOf(')');
this.ctorStmntInsertOffset = this.getCtorStmntInsertOffset();
this.methodInsertOffset = this.getMethodInsertOffset();
}
AS3Class.prototype.getCtorStmntInsertOffset = function()
{
var numBrackets = 0;
var closingBracketOffset;
var startIndex = this.editor.source.indexOf("{", this.ctorArgInsertOffset);
for(var i=startIndex; i<this.editor.source.length; i++)
{
if(this.editor.source.charAt(i) == "{")
numBrackets++;
if(this.editor.source.charAt(i) == "}")
numBrackets--;
if(numBrackets == 0)
{
closingBracketOffset = i;
break;
}
}
return closingBracketOffset;
}
AS3Class.prototype.getMethodInsertOffset = function()
{
var index = this.editor.source.lastIndexOf("}");
return this.editor.source.lastIndexOf("}", index-1);
}
AS3Class.prototype.appendCtorStmnt = function(str)
{
this.editor.applyEdit(this.ctorStmntInsertOffset, 0, ' '+str+'
');
var offset = str.length + 4;
this.ctorStmntInsertOffset += offset;
this.methodInsertOffset += offset;
}
AS3Class.prototype.appendCtorArg = function(str)
{
var offset = str.length;
if (this.ctorHasArgs) {
this.editor.applyEdit(this.ctorArgInsertOffset, 0, ', '+str);
offset += 2;
}else{
this.editor.applyEdit(this.ctorArgInsertOffset, 0, str);
this.ctorHasArgs = true;
}
this.ctorArgInsertOffset+= offset;
this.ctorStmntInsertOffset += offset;
this.methodInsertOffset += offset;
}
AS3Class.prototype.appendMethod = function(str)
{
this.editor.applyEdit(this.methodInsertOffset, 0, '
' + str + '
');
var offset = str.length + 5;
this.methodInsertOffset += offset;
}
function getSelection(editor)
{
var range = editor.selectionRange;
return editor.source.substring(range.startingOffset, range.endingOffset);
}
function getEclosingWord(editor)
{
var startingOffset = editor.currentOffset;
var endingOffset = editor.currentOffset;
var chr = /[A-Za-z0-9_]/;
while(chr.test(editor.source.charAt(startingOffset))){
startingOffset--;
}
while(chr.test(editor.source.charAt(endingOffset))){
endingOffset++;
}
return editor.source.substring(startingOffset+1, endingOffset);
}
function getVariableType(editor, variable)
{
decl = new RegExp(variable + ":(\\w+)", "g");
return decl.exec(editor.source)[1];
}
function getVariableDeclaration(variable, editor)
{
decl = new RegExp("var ("+variable + ":\\w+)", "g");
return decl.exec(editor.source)[1];
}
function parseVariableAtCurrentLine(editor)
{
var line = editor.getLineAtOffset(editor.currentOffset);
var lineSrc = editor.source.substring(editor.getOffsetAtLine(line), editor.getOffsetAtLine(line+1));
var decl = new RegExp("var\\s(\\w+):(\\w+)", "g");
var res = decl.exec(lineSrc);
if(res==null)
return null;
return {name: res[1], type:res[2]};
}
function isTextSelected(editor)
{
var range = editor.selectionRange;
if(range.startingOffset == range.endingOffset)
return false;
else
return true;
}
function getNumTabs(editor)
{
var tabulatorCharCode = "9";
var beginOffsett = editor.getOffsetAtLine(editor.getLineAtOffset(editor.currentOffset));//getLinesBeginOffset(editor);
numberOfTabulators = 0;
for(var i=beginOffsett; i<editor.source.length; i++)
{
if(editor.source.charCodeAt(i) == tabulatorCharCode)
numberOfTabulators++;
else
break;
}
return numberOfTabulators;
}
function getTabString(numTabs)
{
var tabString = '';
for (var i = 0; i< numTabs;i++)
{
tabString += ' ';
}
return tabString;
}
function getGetter(varName, varType)
{
var s = "";
s += "public function get" + capFirstLetter(varName) + "():" + varType;
s += "
{";
s += "
return " + varName + ";";
s += "
}";
return s;
}
function getSetter(varName, varType)
{
var s = "";
s += "public function set" + capFirstLetter(varName) + "(value:" + varType + "):void";
s += "
{";
s += "
" + varName + " = value;";
s += "
}";
return s;
}
function capFirstLetter (s)
{
return s.substring(0,1).toUpperCase() + s.substring(1,s.length);
}
function alert(str)
{
Packages.org.eclipse.jface.dialogs.MessageDialog.openInformation(window.getShell(), "Monkey Dialog", str);
}
function input(title, message, initialValue)
{
var dialog = new Packages.org.eclipse.jface.dialogs.InputDialog(window.getShell(), title, message, initialValue, null);
var result = dialog.open();
if (result == Packages.org.eclipse.jface.window.Window.OK)
{
return dialog.getValue();
}
return '';
}