protected void doRun()
{
Shell shell = PerlEditorPlugin.getWorkbenchWindow().getShell();
TextSelection selection =
((TextSelection) getEditor().getSelectionProvider().getSelection());
if (selection.getText().length() == 0)
{
MessageDialog.openInformation(shell, "No selection", "Nothing has been selected.");
return;
}
InputDialog inputDialog =
new InputDialog(shell, "Subroutine Name", "Name of Subroutine", "", null);
int returnCode = inputDialog.open();
// return unless the ok button was pressed
if (returnCode != Window.OK) { return; }
String[] result = SourceRefactor.extractMethod(inputDialog.getValue(), selection.getText(),
getLog());
if (result.length == 0)
{
MessageDialog.openInformation(shell, "Error", "Subroutine could not be generated.");
return;
}
// Delete trailing \n
if (result[0].endsWith("\n"))
{
result[0] = result[0].substring(0, result[0].lastIndexOf("\n"));
}
IDocument doc = getEditor().getDocumentProvider().getDocument(getEditor().getEditorInput());
try
{
// Repace the selection with the subroutine call
doc.replace(selection.getOffset(), selection.getLength(), result[0]);
int offset = -1;
FindReplaceDocumentAdapter docFind = new FindReplaceDocumentAdapter(doc);
IRegion regionEnd =
docFind.find(selection.getOffset(), "^__END__", true, true, false, true);
offset = (regionEnd != null) ? regionEnd.getOffset() : doc.getLength();
String lineSep = getLineSeparator(doc.get());
// format and insert the new subroutine code