use namespaace::clean in turtles completion driver
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / MultiLine / PPI.pm
CommitLineData
9cdb543b 1package Devel::REPL::Plugin::MultiLine::PPI;
2
3use Moose::Role;
4use PPI;
5use namespace::clean -except => [ 'meta' ];
6
7has 'continuation_prompt' => (
8 is => 'rw', required => 1, lazy => 1,
9 default => sub { '> ' }
10);
11
b21e7551 12has 'line_depth' => (
13 is => 'rw', required => 1, lazy => 1,
14 default => sub { 0 }
15);
16
9cdb543b 17around 'read' => sub {
18 my $orig = shift;
19 my ($self, @args) = @_;
20 my $line = $self->$orig(@args);
21
22 if (defined $line) {
0cbfa921 23 return $self->continue_reading_if_necessary($line, @args);
24 } else {
25 return $line;
26 }
27};
28
29sub continue_reading_if_necessary {
30 my ( $self, $line, @args ) = @_;
31
32 while ($self->line_needs_continuation($line)) {
33 my $orig_prompt = $self->prompt;
34 $self->prompt($self->continuation_prompt);
9cdb543b 35
0cbfa921 36 $self->line_depth($self->line_depth + 1);
37 my $append = $self->read(@args);
38 $self->line_depth($self->line_depth - 1);
b21e7551 39
998f8107 40 $line .= "\n$append" if defined($append);
9cdb543b 41
0cbfa921 42 $self->prompt($orig_prompt);
9cdb543b 43
0cbfa921 44 # ^D means "shut up and eval already"
45 return $line if !defined($append);
9cdb543b 46 }
0cbfa921 47
9cdb543b 48 return $line;
0cbfa921 49}
9cdb543b 50
49946f5c 51sub line_needs_continuation
9cdb543b 52{
49946f5c 53 my $repl = shift;
9cdb543b 54 my $line = shift;
55 my $document = PPI::Document->new(\$line);
56 return 0 if !defined($document);
57
58 # this could use more logic, such as returning 1 on s/foo/ba<Enter>
59 my $unfinished_structure = sub
60 {
61 my ($document, $element) = @_;
62 return 0 unless $element->isa('PPI::Structure');
63 return 1 unless $element->start && $element->finish;
64 return 0;
65 };
66
67 return $document->find_any($unfinished_structure);
68}
69
701;
d9ba19d2 71
72__END__
73
74=head1 NAME
75
76Devel::REPL::Plugin::MultiLine::PPI - read lines until all blocks are closed
77
78=head1 SYNOPSIS
79
80 #!/usr/bin/perl
81
82 use lib './lib';
83 use Devel::REPL;
84
85 my $repl = Devel::REPL->new;
86 $repl->load_plugin('LexEnv');
87 $repl->load_plugin('History');
88 $repl->load_plugin('MultiLine::PPI');
89 $repl->run;
90
91=head1 DESCRIPTION
92
93Plugin that will collect lines until you have no unfinished structures. This
94lets you write subroutines, C<if> statements, loops, etc. more naturally.
95
96For example, without a MultiLine plugin,
97
98 $ my $x = 3;
99 3
100 $ if ($x == 3) {
101
102will throw a compile error, because that C<if> statement is incomplete. With a
103MultiLine plugin,
104
105 $ my $x = 3;
106 3
107 $ if ($x == 3) {
108
109 > print "OH NOES!"
110
111 > }
112 OH NOES
113 1
114
115you may write the code across multiple lines, such as in C<irb> and C<python>.
116
117This module uses L<PPI>. This plugin is named C<MultiLine::PPI> because someone
118else may conceivably implement similar behavior some other less
119dependency-heavy way.
120
121=head1 SEE ALSO
122
123C<Devel::REPL>
124
125=head1 AUTHOR
126
127Shawn M Moore, C<< <sartak at gmail dot com> >>
128
129=head1 COPYRIGHT AND LICENSE
130
131Copyright (C) 2007 by Shawn M Moore
132
133This library is free software; you can redistribute it and/or modify
134it under the same terms as Perl itself.
135
136=cut