1 package Devel::REPL::Plugin::MultiLine::PPI;
3 use Devel::REPL::Plugin;
5 use namespace::autoclean;
7 has 'continuation_prompt' => (
10 default => sub { '> ' }
19 around 'read' => sub {
21 my ($self, @args) = @_;
22 my $line = $self->$orig(@args);
25 return $self->continue_reading_if_necessary($line, @args);
31 sub continue_reading_if_necessary {
32 my ( $self, $line, @args ) = @_;
34 while ($self->line_needs_continuation($line)) {
35 my $orig_prompt = $self->prompt;
36 $self->prompt($self->continuation_prompt);
38 $self->line_depth($self->line_depth + 1);
39 my $append = $self->read(@args);
40 $self->line_depth($self->line_depth - 1);
42 $line .= "\n$append" if defined($append);
44 $self->prompt($orig_prompt);
46 # ^D means "shut up and eval already"
47 return $line if !defined($append);
53 sub line_needs_continuation
58 # add this so we can test whether the document ends in PPI::Statement::Null
61 my $document = PPI::Document->new(\$line);
62 return 0 if !defined($document);
64 # adding ";" to a complete document adds a PPI::Statement::Null. we added a ;;
65 # so if it doesn't end in null then there's probably something that's
67 return 0 if $document->child(-1)->isa('PPI::Statement::Null');
69 # this could use more logic, such as returning 1 on s/foo/ba<Enter>
70 my $unfinished_structure = sub
72 my ($document, $element) = @_;
73 return 0 unless $element->isa('PPI::Structure');
74 return 1 unless $element->finish;
78 return $document->find_any($unfinished_structure);
87 Devel::REPL::Plugin::MultiLine::PPI - read lines until all blocks are closed
96 my $repl = Devel::REPL->new;
97 $repl->load_plugin('LexEnv');
98 $repl->load_plugin('History');
99 $repl->load_plugin('MultiLine::PPI');
104 Plugin that will collect lines until you have no unfinished structures. This
105 lets you write subroutines, C<if> statements, loops, etc. more naturally.
107 For example, without a MultiLine plugin,
113 will throw a compile error, because that C<if> statement is incomplete. With a
126 you may write the code across multiple lines, such as in C<irb> and C<python>.
128 This module uses L<PPI>. This plugin is named C<MultiLine::PPI> because someone
129 else may conceivably implement similar behavior some other less
130 dependency-heavy way.
138 Shawn M Moore, C<< <sartak at gmail dot com> >>
140 =head1 COPYRIGHT AND LICENSE
142 Copyright (C) 2007 by Shawn M Moore
144 This library is free software; you can redistribute it and/or modify
145 it under the same terms as Perl itself.