1
package Padre::Task::PPI::IntroduceTemporaryVariable;
7
use Padre::Task::PPI ();
8
use PPIx::EditorTools::IntroduceTemporaryVariable ();
10
our $VERSION = '0.63';
11
our @ISA = 'Padre::Task::PPI';
17
Padre::Task::PPI::IntroduceTemporaryVariable - Introduces a temporary variable using L<PPI>
21
my $tempvarmaker = Padre::Task::PPI::IntroduceTemporaryVariable->new(
22
document => $document_obj,
23
start_location => [$line, $column], # or just character position
24
end_location => [$line, $column], # or ppi-style location
28
$tempvarmaker->schedule();
32
Given a region of code within a statement, replaces that code with a temporary variable.
33
Declares and initializes the temporary variable right above the statement that included the selected
36
Usually, you simply set C<start_position> to what C<< $editor->GetSelectionStart() >> returns
37
and C<end_position> to C<< $editor->GetSelectionEnd() - 1 >>.
43
$self->SUPER::prepare(@_);
45
# move the document to the main-thread-only storage
46
my $mto = $self->{main_thread_only} ||= {};
47
$mto->{document} = $self->{document}
48
if defined $self->{document};
49
delete $self->{document};
50
if ( not defined $mto->{document} ) {
52
Carp::croak("Missing Padre::Document::Perl object as {document} attribute of the temporary-variable task");
55
foreach my $key (qw(start_location end_location)) {
56
if ( not defined $self->{$key} ) {
58
Carp::croak("Need a {$key}!");
59
} elsif ( not ref( $self->{$key} ) ) {
60
my $doc = $mto->{document};
61
$self->{$key} = $doc->character_position_to_ppi_location( $self->{$key} );
70
my $ppi = shift or return;
71
my $location = $self->{start_location};
74
PPIx::EditorTools::IntroduceTemporaryVariable->new->introduce(
76
start_location => $self->{start_location},
77
end_location => $self->{end_location},
78
varname => $self->{varname},
86
# TO DO: passing this back and forth is probably hyper-inefficient, but such is life.
87
$self->{updated_document_string} = $munged->code;
88
$self->{location} = $munged->element->location;
94
if ( defined $self->{updated_document_string} ) {
97
# TO DO: What if the document changed? Bad luck for now.
98
$self->{main_thread_only}->{document}->editor->SetText( $self->{updated_document_string} );
99
$self->{main_thread_only}->{document}->ppi_select( $self->{location} );
102
if ( $self->{error} =~ /no token/ ) {
103
$text = Wx::gettext("First character of selection does not seem to point at a token.");
104
} elsif ( $self->{error} =~ /no statement/ ) {
105
$text = Wx::gettext("Selection not part of a Perl statement?");
107
$text = Wx::gettext("Unknown error");
110
$text, Wx::gettext("Replace Operation Canceled"),
111
Wx::wxOK, Padre->ide->wx->main
123
This class inherits from C<Padre::Task::PPI>.
127
Steffen Mueller C<smueller@cpan.org>
129
=head1 COPYRIGHT AND LICENSE
131
Copyright 2008-2010 The Padre development team as listed in Padre.pm.
133
This program is free software; you can redistribute it and/or
134
modify it under the same terms as Perl 5 itself.
138
# Copyright 2008-2010 The Padre development team as listed in Padre.pm.
140
# This program is free software; you can redistribute it and/or
141
# modify it under the same terms as Perl 5 itself.