3 package Devel::REPL::Plugin::MultiLine::PPI;
5 our $VERSION = '1.003027';
7 use Devel::REPL::Plugin;
9 use namespace::autoclean;
11 has 'continuation_prompt' => (
14 default => sub { '> ' }
23 around 'read' => sub {
25 my ($self, @args) = @_;
26 my $line = $self->$orig(@args);
29 return $self->continue_reading_if_necessary($line, @args);
35 sub continue_reading_if_necessary {
36 my ( $self, $line, @args ) = @_;
38 while ($self->line_needs_continuation($line)) {
39 my $orig_prompt = $self->prompt;
40 $self->prompt($self->continuation_prompt);
42 $self->line_depth($self->line_depth + 1);
43 my $append = $self->read(@args);
44 $self->line_depth($self->line_depth - 1);
46 $line .= "\n$append" if defined($append);
48 $self->prompt($orig_prompt);
50 # ^D means "shut up and eval already"
51 return $line if !defined($append);
57 sub line_needs_continuation
62 # add this so we can test whether the document ends in PPI::Statement::Null
65 my $document = PPI::Document->new(\$line);
66 return 0 if !defined($document);
68 # adding ";" to a complete document adds a PPI::Statement::Null. we added a ;;
69 # so if it doesn't end in null then there's probably something that's
71 return 0 if $document->child(-1)->isa('PPI::Statement::Null');
73 # this could use more logic, such as returning 1 on s/foo/ba<Enter>
74 my $unfinished_structure = sub
76 my ($document, $element) = @_;
77 return 0 unless $element->isa('PPI::Structure');
78 return 1 unless $element->finish;
82 return $document->find_any($unfinished_structure);
91 Devel::REPL::Plugin::MultiLine::PPI - read lines until all blocks are closed
97 my $repl = Devel::REPL->new;
98 $repl->load_plugin('LexEnv');
99 $repl->load_plugin('History');
100 $repl->load_plugin('MultiLine::PPI');
105 Plugin that will collect lines until you have no unfinished structures. This
106 lets you write subroutines, C<if> statements, loops, etc. more naturally.
108 For example, without a MultiLine plugin,
114 will throw a compile error, because that C<if> statement is incomplete. With a
127 you may write the code across multiple lines, such as in C<irb> and C<python>.
129 This module uses L<PPI>. This plugin is named C<MultiLine::PPI> because someone
130 else may conceivably implement similar behavior some other less
131 dependency-heavy way.
139 Shawn M Moore, C<< <sartak at gmail dot com> >>
141 =head1 COPYRIGHT AND LICENSE
143 Copyright (C) 2007 by Shawn M Moore
145 This library is free software; you can redistribute it and/or modify
146 it under the same terms as Perl itself.