| | 84 | $self->{extract_subroutine} = $self->add_menu_item( |
| | 85 | $self, |
| | 86 | name => 'perl.extract_subroutine', |
| | 87 | label => Wx::gettext('Extract Subroutine'), |
| | 88 | menu_event => sub { |
| | 89 | my $doc = $_[0]->current->document; |
| | 90 | my $editor = $doc->editor; |
| | 91 | my $code = $editor->GetSelectedText(); |
| | 92 | require Padre::Wx::History::TextEntryDialog; |
| | 93 | my $dialog = Padre::Wx::History::TextEntryDialog->new( |
| | 94 | $_[0], |
| | 95 | Wx::gettext("New Subroutine Name"), |
| | 96 | Wx::gettext("Please enter a name for the new subroutine"), |
| | 97 | '$foo', |
| | 98 | ); |
| | 99 | return if $dialog->ShowModal == Wx::wxID_CANCEL; |
| | 100 | my $newname = $dialog->GetValue; |
| | 101 | $dialog->Destroy; |
| | 102 | return unless defined $newname; |
| | 103 | |
| | 104 | require Devel::Refactor; |
| | 105 | my $refactory = Devel::Refactor->new; |
| | 106 | my ( $new_sub_call, $new_code ) = $refactory->extract_subroutine( $newname, $code, 1 ); |
| | 107 | $editor->BeginUndoAction(); # do the edit atomically |
| | 108 | $editor->ReplaceSelection($new_sub_call); |
| | 109 | $editor->DocumentEnd(); # TODO: find a better place to put the new subroutine |
| | 110 | $editor->AddText($new_code); |
| | 111 | $editor->EndUndoAction(); |
| | 112 | }, |
| | 113 | ); |
| | 114 | |