| 1 | package Devel::IPerl::Kernel::Backend::Reply; | ||
| 2 | $Devel::IPerl::Kernel::Backend::Reply::VERSION = '0.012'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Log::Any qw($log); | ||
| 7 | use Devel::IPerl::ExecutionResult; | ||
| 8 | use Capture::Tiny ':all'; | ||
| 9 | use Moo; | ||
| 10 | |||
| 11 | has repl => ( is => 'lazy' ); | ||
| 12 | |||
| 13 | sub _build_repl { | ||
| 14 | my ($self) = @_; | ||
| 15 | require Reply; | ||
| 16 | $log->trace('Creating REPL: Reply'); | ||
| 17 | |||
| 18 | my @autocomplete_plugins = qw( Autocomplete::Functions Autocomplete::Globals | ||
| 19 | Autocomplete::Keywords Autocomplete::Lexicals Autocomplete::Methods | ||
| 20 | Autocomplete::Packages); | ||
| 21 | my $repl = Reply->new( | ||
| 22 | # needs these at a minimum | ||
| 23 | plugins => [ qw[IPerl Packages LexicalPersistence Hints], | ||
| 24 | @autocomplete_plugins ] | ||
| 25 | |||
| 26 | |||
| 27 | ); | ||
| 28 | |||
| 29 | $repl; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub completion { | ||
| 33 | my ($self, $line, $cursor_location) = @_; | ||
| 34 | |||
| 35 | my $repl = $self->repl; | ||
| 36 | my $line_up_to_cursor = substr $line, 0, $cursor_location; | ||
| 37 | my @matches = $repl->_concatenate_plugin('tab_handler', $line_up_to_cursor); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub run_line { | ||
| 41 | my ($self, $cmd) = @_; | ||
| 42 | my $repl = $self->repl; | ||
| 43 | $log->tracef('Running command: %s', $cmd); | ||
| 44 | |||
| 45 | $repl->_concatenate_plugin('clear_data'); | ||
| 46 | |||
| 47 | capture { | ||
| 48 | $repl->step( $cmd, 0 ); | ||
| 49 | }; | ||
| 50 | |||
| 51 | my $exec_result = Devel::IPerl::ExecutionResult->new(); | ||
| 52 | $exec_result->stdout( $repl->_concatenate_plugin('stdout') ); | ||
| 53 | $exec_result->stderr( $repl->_concatenate_plugin('stderr') ); | ||
| 54 | |||
| 55 | my @last_output = $repl->_concatenate_plugin('last_output'); | ||
| 56 | $last_output[0] = "" unless defined $last_output[0]; | ||
| 57 | $last_output[0] =~ s/^\n+$//g; # not sure why, but the output can just be a newline? | ||
| 58 | $exec_result->last_output( $last_output[0] ); | ||
| 59 | |||
| 60 | $exec_result->results( $repl->_concatenate_plugin('results') ); | ||
| 61 | |||
| 62 | my $status_ok = 1; | ||
| 63 | |||
| 64 | my $warn = [ $repl->_concatenate_plugin('warning') ]; | ||
| 65 | $exec_result->warning( @$warn ) if defined $warn->[0]; | ||
| 66 | if( defined $exec_result->warning ) { | ||
| 67 | $status_ok = 0; | ||
| 68 | my $warning_string = join "\n", @{$exec_result->warning}; | ||
| 69 | $exec_result->status_error; # TODO not really an error? | ||
| 70 | $exec_result->warning_name( 'Warning' ); | ||
| 71 | $exec_result->warning_value( $warning_string ); | ||
| 72 | |||
| 73 | # TODO get an actual traceback | ||
| 74 | $exec_result->warning_traceback( [$warning_string] ); | ||
| 75 | } | ||
| 76 | |||
| 77 | # capture exceptions | ||
| 78 | my @error = $repl->_concatenate_plugin('error'); | ||
| 79 | $exec_result->error( $error[0] ); | ||
| 80 | if( defined $exec_result->error ) { | ||
| 81 | $status_ok = 0; | ||
| 82 | my $exception = $exec_result->error; | ||
| 83 | $exec_result->status_error; | ||
| 84 | $exec_result->exception_name( ref($exception) || 'Error' ); | ||
| 85 | $exec_result->exception_value( $exception ); | ||
| 86 | |||
| 87 | # TODO get an actual traceback | ||
| 88 | $exec_result->exception_traceback( [$exception] ); | ||
| 89 | } | ||
| 90 | |||
| 91 | if( $status_ok ) { | ||
| 92 | $exec_result->status_ok; # TODO | ||
| 93 | } | ||
| 94 | |||
| 95 | $exec_result; | ||
| 96 | } | ||
| 97 | |||
| 98 | # taken/modified from Devel::REPL::Plugin::MultiLine::PPI | ||
| 99 | sub is_complete { | ||
| 100 | my ($self, $code) = @_; | ||
| 101 | |||
| 102 | require PPI::Document; | ||
| 103 | |||
| 104 | # add this so we can test whether the document ends in PPI::Statement::Null | ||
| 105 | $code .= "\n;;"; | ||
| 106 | |||
| 107 | my $document = PPI::Document->new(\$code); | ||
| 108 | return 1 if !defined($document); | ||
| 109 | |||
| 110 | # adding ";" to a complete document adds a PPI::Statement::Null. we added a ;; | ||
| 111 | # so if it doesn't end in null then there's probably something that's | ||
| 112 | # incomplete | ||
| 113 | return 1 if $document->child(-1)->isa('PPI::Statement::Null'); | ||
| 114 | |||
| 115 | # this could use more logic, such as returning 1 on s/foo/ba<Enter> | ||
| 116 | my $unfinished_structure = sub { | ||
| 117 | my ($document, $element) = @_; | ||
| 118 | return 0 unless $element->isa('PPI::Structure'); | ||
| 119 | return 1 unless $element->finish; | ||
| 120 | return 0; | ||
| 121 | }; | ||
| 122 | |||
| 123 | return !($document->find_any($unfinished_structure)); | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | 1; | ||
| 128 | |||
| 129 | __END__ |