X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FDevel-REPL.git;a=blobdiff_plain;f=lib%2FDevel%2FREPL%2FPlugin%2FCompletion.pm;h=de8b5fc060e856c53899e729e153bef6a9490bfa;hp=91424ae795be4e5d9ad31dfa1713e9e6ee8a57f9;hb=afc8677b078de3f125147caf731bd298c8b6d04e;hpb=c8fafb5a47755bd6ae2f3e86fba2e894e787d525 diff --git a/lib/Devel/REPL/Plugin/Completion.pm b/lib/Devel/REPL/Plugin/Completion.pm index 91424ae..de8b5fc 100644 --- a/lib/Devel/REPL/Plugin/Completion.pm +++ b/lib/Devel/REPL/Plugin/Completion.pm @@ -1,52 +1,62 @@ +use strict; +use warnings; package Devel::REPL::Plugin::Completion; + use Devel::REPL::Plugin; use Scalar::Util 'weaken'; use PPI; -use namespace::clean -except => [ 'meta' ]; +use namespace::autoclean; has current_matches => ( - is => 'rw', - isa => 'ArrayRef', - lazy => 1, - default => sub { [] }, + is => 'rw', + isa => 'ArrayRef', + lazy => 1, + default => sub { [] }, ); has match_index => ( - is => 'rw', - isa => 'Int', - lazy => 1, - default => sub { 0 }, + is => 'rw', + isa => 'Int', + lazy => 1, + default => sub { 0 }, ); has no_term_class_warning => ( - isa => "Bool", - is => "rw", - default => 0, + isa => "Bool", + is => "rw", + default => 0, +); + +has do_readline_filename_completion => ( # so default is no if Completion loaded + isa => "Bool", + is => "rw", + lazy => 1, + default => sub { 0 }, ); before 'read' => sub { - my ($self) = @_; + my ($self) = @_; - if ((!$self->term->isa("Term::ReadLine::Gnu") and !$self->term->isa("Term::ReadLine::Perl")) - and !$self->no_term_class_warning) { - warn "Term::ReadLine::Gnu or Term::ReadLine::Perl is required for the Completion plugin to work"; - $self->no_term_class_warning(1); - } + if ((!$self->term->isa("Term::ReadLine::Gnu") and !$self->term->isa("Term::ReadLine::Perl")) + and !$self->no_term_class_warning) { + warn "Term::ReadLine::Gnu or Term::ReadLine::Perl is required for the Completion plugin to work"; + $self->no_term_class_warning(1); + } - my $weakself = $self; - weaken($weakself); + my $weakself = $self; + weaken($weakself); - if ($self->term->isa("Term::ReadLine::Gnu")) { - $self->term->Attribs->{attempted_completion_function} = sub { - $weakself->_completion(@_); - }; - } + if ($self->term->isa("Term::ReadLine::Gnu")) { + $self->term->Attribs->{attempted_completion_function} = sub { + $weakself->_completion(@_); + }; + } - if ($self->term->isa("Term::ReadLine::Perl")) { - $self->term->Attribs->{completion_function} = sub { - $weakself->_completion(@_); - }; - } + if ($self->term->isa("Term::ReadLine::Perl")) { + $self->term->Attribs->{completion_function} = sub { + $weakself->_completion(@_); + }; + } }; @@ -72,9 +82,10 @@ sub _completion { if (scalar(@matches)) { return @matches; } else { - return readline::rl_filename_list($text); + return ($self->do_readline_filename_completion) ? readline::rl_filename_list($text) : () ; } } else { + $self->term->Attribs->{attempted_completion_over} = 1 unless $self->do_readline_filename_completion; if (scalar(@matches)) { return $self->term->completion_matches($text, sub { my ($text, $state) = @_; @@ -90,25 +101,24 @@ sub _completion { return $self->current_matches->[$self->match_index]; }); } else { - # fall back to filename completion for Term::ReadLine::Gnu return; } } } sub complete { - return (); + return (); } # recursively find the last element sub last_ppi_element { - my ($self, $document, $type) = @_; - my $last = $document; - while ($last->can('last_element') && defined($last->last_element)) { - $last = $last->last_element; - return $last if $type && $last->isa($type); - } - return $last; + my ($self, $document, $type) = @_; + my $last = $document; + while ($last->can('last_element') && defined($last->last_element)) { + $last = $last->last_element; + return $last if $type && $last->isa($type); + } + return $last; } 1; @@ -119,6 +129,13 @@ __END__ Devel::REPL::Plugin::Completion - Extensible tab completion +=head1 NOTE + +By default, the Completion plugin explicitly does I use the Gnu readline +or Term::ReadLine::Perl fallback filename completion. + +Set the attribute C to 1 to enable this feature. + =head1 AUTHOR Shawn M Moore, C<< >>