X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FDevel-REPL.git;a=blobdiff_plain;f=lib%2FDevel%2FREPL%2FPlugin%2FCompletionDriver%2FINC.pm;h=bf4e6c3637b00b0260ddae6f54ee8009f9ef9dc7;hp=493a886c2ddcd394c3a613ba51a4d34582b6fbad;hb=6f4f9516b3422a8ce51b89b596fb808b197f833e;hpb=6c3218fee98e0dc35d1ed442197b99f8437b0bee diff --git a/lib/Devel/REPL/Plugin/CompletionDriver/INC.pm b/lib/Devel/REPL/Plugin/CompletionDriver/INC.pm index 493a886..bf4e6c3 100644 --- a/lib/Devel/REPL/Plugin/CompletionDriver/INC.pm +++ b/lib/Devel/REPL/Plugin/CompletionDriver/INC.pm @@ -1,8 +1,17 @@ +use strict; +use warnings; package Devel::REPL::Plugin::CompletionDriver::INC; + use Devel::REPL::Plugin; +use Devel::REPL::Plugin::Completion; # die early if cannot load use File::Next; use File::Spec; -use namespace::clean -except => [ 'meta' ]; +use namespace::autoclean; + +sub BEFORE_PLUGIN { + my $self = shift; + $self->load_plugin('Completion'); +} around complete => sub { my $orig = shift; @@ -24,19 +33,27 @@ around complete => sub { my $package = shift @elements; my $outsep = '::'; - my $insep = '::'; + my $insep = "::"; my $keep_extension = 0; - - # require "Module" - if ($package->isa('PPI::Token::Quote')) - { - $outsep = $insep = '/'; - $keep_extension = 1; - } - elsif ($package =~ /'/) + my $prefix = ''; + + # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for + # partially completed elements + #if ($package->isa('PPI::Token::Quote')) + #{ + # # we need to strip off the leading quote and stash it + # $package = $package->string; + # my $start = index($package->quote, $package); + # $prefix = substr($package->quote, 0, $start); + + # # we're completing something like: require "Foo/Bar.pm" + # $outsep = $insep = '/'; + # $keep_extension = 1; + #} + if ($package =~ /'/) { # the goofball is using the ancient ' package sep, we'll humor him - $outsep = q{'}; + $outsep = "'"; $insep = "'|::"; } @@ -49,16 +66,29 @@ around complete => sub { my @found; + # most VCSes don't litter every single fucking directory with garbage. if you + # know of any other, just stick them in here. No one wants to complete + # Devel::REPL::Plugin::.svn + my %ignored = + ( + '.' => 1, + '..' => 1, + '.svn' => 1, + ); + + # this will take a directory and add to @found all of the possible matches my $add_recursively; $add_recursively = sub { my ($path, $iteration, @more) = @_; - opendir((my $dirhandle), $path); - for (readdir $dirhandle) + opendir((my $dirhandle), $path) || return; + for (grep { !$ignored{$_} } readdir $dirhandle) { - next if /^\.+$/; # skip . and .. - next if $iteration == 0 && $_ !~ $final_re; - my $match = $_; + + # if this is the first time around, we need respect whatever the user had + # at the very end when he pressed tab + next if $iteration == 0 && $match !~ $final_re; + my $fullmatch = File::Spec->rel2abs($match, $path); if (-d $fullmatch) { @@ -67,14 +97,19 @@ around complete => sub { else { $match =~ s/\..*// unless $keep_extension; - push @found, join $outsep, @directories, @more, $match; + push @found, join '', $prefix, + join $outsep, @directories, @more, $match; } } }; + # look through all of INC: for (@INC) { my $path = $_; + + # match all of the fragments they have, so "use Moose::Meta::At" + # will only begin looking in ../Moose/Meta/ for my $subdir (@directories) { $path = File::Spec->catdir($path, $subdir); @@ -89,3 +124,15 @@ around complete => sub { 1; +__END__ + +=head1 NAME + +Devel::REPL::Plugin::CompletionDriver::INC - Complete module names in use and require + +=head1 AUTHOR + +Shawn M Moore, C<< >> + +=cut +