Add the appropriate newlines from MultiLine::PPI
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / MultiLine / PPI.pm
1 package Devel::REPL::Plugin::MultiLine::PPI;
2
3 use Moose::Role;
4 use PPI;
5 use namespace::clean -except => [ 'meta' ];
6
7 has 'continuation_prompt' => (
8   is => 'rw', required => 1, lazy => 1,
9   default => sub { '> ' }
10 );
11
12 has 'line_depth' => (
13   is => 'rw', required => 1, lazy => 1,
14   default => sub { 0 }
15 );
16
17 around 'read' => sub {
18   my $orig = shift;
19   my ($self, @args) = @_;
20   my $line = $self->$orig(@args);
21
22   if (defined $line) {
23     return $self->continue_reading_if_necessary($line, @args);
24   } else {
25     return $line;
26   }
27 };
28
29 sub 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);
35
36     $self->line_depth($self->line_depth + 1);
37     my $append = $self->read(@args);
38     $self->line_depth($self->line_depth - 1);
39
40     $line .= "\n$append" if defined($append);
41
42     $self->prompt($orig_prompt);
43
44     # ^D means "shut up and eval already"
45     return $line if !defined($append);
46   }
47
48   return $line;
49 }
50
51 sub line_needs_continuation
52 {
53   my $repl = shift;
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
70 1;
71
72 __END__
73
74 =head1 NAME
75
76 Devel::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
93 Plugin that will collect lines until you have no unfinished structures. This
94 lets you write subroutines, C<if> statements, loops, etc. more naturally.
95
96 For example, without a MultiLine plugin,
97
98     $ my $x = 3;
99     3
100     $ if ($x == 3) {
101
102 will throw a compile error, because that C<if> statement is incomplete. With a
103 MultiLine 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
115 you may write the code across multiple lines, such as in C<irb> and C<python>.
116
117 This module uses L<PPI>. This plugin is named C<MultiLine::PPI> because someone
118 else may conceivably implement similar behavior some other less
119 dependency-heavy way.
120
121 =head1 SEE ALSO
122
123 C<Devel::REPL>
124
125 =head1 AUTHOR
126
127 Shawn M Moore, C<< <sartak at gmail dot com> >>
128
129 =head1 COPYRIGHT AND LICENSE
130
131 Copyright (C) 2007 by Shawn M Moore
132
133 This library is free software; you can redistribute it and/or modify
134 it under the same terms as Perl itself.
135
136 =cut