1 package Devel::REPL::Plugin::MultiLine::PPI;
5 use namespace::clean -except => [ 'meta' ];
7 has 'continuation_prompt' => (
8 is => 'rw', required => 1, lazy => 1,
9 default => sub { '> ' }
12 around 'read' => sub {
14 my ($self, @args) = @_;
15 my $line = $self->$orig(@args);
18 while (needs_continuation($line)) {
19 my $orig_prompt = $self->prompt;
20 $self->prompt($self->continuation_prompt);
22 my $append = $self->read(@args);
23 $line .= $append if defined($append);
25 $self->prompt($orig_prompt);
27 # ^D means "shut up and eval already"
28 return $line if !defined($append);
34 sub needs_continuation
37 my $document = PPI::Document->new(\$line);
38 return 0 if !defined($document);
40 # this could use more logic, such as returning 1 on s/foo/ba<Enter>
41 my $unfinished_structure = sub
43 my ($document, $element) = @_;
44 return 0 unless $element->isa('PPI::Structure');
45 return 1 unless $element->start && $element->finish;
49 return $document->find_any($unfinished_structure);