~ubuntu-branches/ubuntu/quantal/padre/quantal

« back to all changes in this revision

Viewing changes to lib/Padre/Task/PPI/IntroduceTemporaryVariable.pm

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2010-12-11 20:56:34 UTC
  • mfrom: (1.3.1 upstream) (13.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20101211205634-sitpxguuf9cct47q
Tags: 0.76.ds1-1
* New upstream release

* bump liborlite (build-)dependency to 1.46
* bump libparse-errorstring-perl-perl (build-)dependency to 0.14
* bump libwx-perl-processstream-perl (build-)dependency to 0.29
* update the list of incomplete manuals

* Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package Padre::Task::PPI::IntroduceTemporaryVariable;
2
 
 
3
 
use 5.008;
4
 
use strict;
5
 
use warnings;
6
 
use Padre::Wx                                     ();
7
 
use Padre::Task::PPI                              ();
8
 
use PPIx::EditorTools::IntroduceTemporaryVariable ();
9
 
 
10
 
our $VERSION = '0.63';
11
 
our @ISA     = 'Padre::Task::PPI';
12
 
 
13
 
=pod
14
 
 
15
 
=head1 NAME
16
 
 
17
 
Padre::Task::PPI::IntroduceTemporaryVariable - Introduces a temporary variable using L<PPI>
18
 
 
19
 
=head1 SYNOPSIS
20
 
 
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
25
 
          varname        => '$foo',
26
 
  );
27
 
 
28
 
  $tempvarmaker->schedule();
29
 
 
30
 
=head1 DESCRIPTION
31
 
 
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
34
 
expression.
35
 
 
36
 
Usually, you simply set C<start_position> to what C<< $editor->GetSelectionStart() >> returns
37
 
and C<end_position> to C<< $editor->GetSelectionEnd() - 1 >>.
38
 
 
39
 
=cut
40
 
 
41
 
sub prepare {
42
 
        my $self = shift;
43
 
        $self->SUPER::prepare(@_);
44
 
 
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} ) {
51
 
                require Carp;
52
 
                Carp::croak("Missing Padre::Document::Perl object as {document} attribute of the temporary-variable task");
53
 
        }
54
 
 
55
 
        foreach my $key (qw(start_location end_location)) {
56
 
                if ( not defined $self->{$key} ) {
57
 
                        require Carp;
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} );
62
 
                }
63
 
        }
64
 
 
65
 
        return ();
66
 
}
67
 
 
68
 
sub process_ppi {
69
 
        my $self     = shift;
70
 
        my $ppi      = shift or return;
71
 
        my $location = $self->{start_location};
72
 
 
73
 
        my $munged = eval {
74
 
                PPIx::EditorTools::IntroduceTemporaryVariable->new->introduce(
75
 
                        ppi            => $ppi,
76
 
                        start_location => $self->{start_location},
77
 
                        end_location   => $self->{end_location},
78
 
                        varname        => $self->{varname},
79
 
                );
80
 
        };
81
 
        if ($@) {
82
 
                $self->{error} = $@;
83
 
                return;
84
 
        }
85
 
 
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;
89
 
        return ();
90
 
}
91
 
 
92
 
sub finish {
93
 
        my $self = shift;
94
 
        if ( defined $self->{updated_document_string} ) {
95
 
 
96
 
                # GUI update
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} );
100
 
        } else {
101
 
                my $text;
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?");
106
 
                } else {
107
 
                        $text = Wx::gettext("Unknown error");
108
 
                }
109
 
                Wx::MessageBox(
110
 
                        $text,    Wx::gettext("Replace Operation Canceled"),
111
 
                        Wx::wxOK, Padre->ide->wx->main
112
 
                );
113
 
        }
114
 
        return ();
115
 
}
116
 
 
117
 
1;
118
 
 
119
 
__END__
120
 
 
121
 
=head1 SEE ALSO
122
 
 
123
 
This class inherits from C<Padre::Task::PPI>.
124
 
 
125
 
=head1 AUTHOR
126
 
 
127
 
Steffen Mueller C<smueller@cpan.org>
128
 
 
129
 
=head1 COPYRIGHT AND LICENSE
130
 
 
131
 
Copyright 2008-2010 The Padre development team as listed in Padre.pm.
132
 
 
133
 
This program is free software; you can redistribute it and/or
134
 
modify it under the same terms as Perl 5 itself.
135
 
 
136
 
=cut
137
 
 
138
 
# Copyright 2008-2010 The Padre development team as listed in Padre.pm.
139
 
# LICENSE
140
 
# This program is free software; you can redistribute it and/or
141
 
# modify it under the same terms as Perl 5 itself.