Practically Macro: Writing editor scripts
Editor scripts are written in beanshell.
The context variables passed in are:
styledText - a StyledText object - This is the object corresponding to the current editor. You can use it to insert text, grab text, change the selection, etc.
findTarget - an IFindReplaceTarget object - This object is useful for performing searches on the text object. You can typically cast this object to IFindReplaceTargetExtension and IFindReplaceTargetExtension3 to perform searches with more options
console - a console object corresponding to the Macro console view. You can use this object to spit out debug information for your scripts. Use write(String), writeln(String), and write(Exception)
Return Boolean.TRUE to indicate that the script finished normally, or Boolean.FALSE to indicate abnormal termination and that further macro commands should not be executed. If you return nothing, that is considered "TRUE".
Here's an example script that removes the XML comments from a block including the cursor.
//Scripts are beanshell format (see http://www.beanshell.org/)
//variable type
//styledText the org.eclipse.swt.custom.StyledText instance for the current editor
//console write output to the macro console via console.write(String), .writeln(String), .write(Exception)
//findTarget the instance of org.eclipse.jface.text.IFindReplaceTarget
import org.eclipse.swt.custom.StyledText;
import org.eclipse.jface.text.IFindReplaceTarget;
int caretOffset=styledText.getCaretOffset();
int startComment=findTarget.findAndSelect(caretOffset, "<!--", false, true, false);
int endComment=findTarget.findAndSelect(caretOffset, "-->", true, true, false);
int possibleStartComment=findTarget.findAndSelect(caretOffset, "<!--", true, true, false);
int possibleEndComment=findTarget.findAndSelect(caretOffset, "-->", false, true, false);
if (startComment>=0 && endComment>=0 && (possibleEndComment<0 || startComment>=possibleEndComment) && (possibleStartComment<0 || endComment<possibleStartComment))
{
styledText.replaceTextRange(endComment, 3, "");
styledText.replaceTextRange(startComment, 4, "");
styledText.setSelection(caretOffset-4, caretOffset-4);
}
else
{
//regardless, set position back to original
styledText.setSelection(caretOffset, caretOffset);
}