1 package Devel::REPL::Plugin::CompletionDriver::INC;
2 use Devel::REPL::Plugin;
5 use namespace::clean -except => [ 'meta' ];
7 around complete => sub {
9 my ($self, $text, $document) = @_;
11 my $last = $self->last_ppi_element($document, 'PPI::Statement::Include');
14 unless $last->isa('PPI::Statement::Include');
16 my @elements = $last->children;
17 shift @elements; # use or require
19 # too late for us to care, they're completing on something like
21 # OR they just have "use " and are tab completing. we'll spare them the flood
25 my $package = shift @elements;
28 my $keep_extension = 0;
31 # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for
32 # partially completed elements
33 #if ($package->isa('PPI::Token::Quote'))
35 # # we need to strip off the leading quote and stash it
36 # $package = $package->string;
37 # my $start = index($package->quote, $package);
38 # $prefix = substr($package->quote, 0, $start);
40 # # we're completing something like: require "Foo/Bar.pm"
41 # $outsep = $insep = '/';
42 # $keep_extension = 1;
46 # the goofball is using the ancient ' package sep, we'll humor him
51 my @directories = split $insep, $package;
53 # split drops trailing fields
54 push @directories, '' if $package =~ /(?:$insep)$/;
55 my $final = pop @directories;
56 my $final_re = qr/^\Q$final/;
68 $add_recursively = sub {
69 my ($path, $iteration, @more) = @_;
70 opendir((my $dirhandle), $path);
71 for (grep { !$ignored{$_} } readdir $dirhandle)
75 # if this is the first time around, we need respect whatever the user had
76 # at the very end when he pressed tab
77 next if $iteration == 0 && $match !~ $final_re;
79 my $fullmatch = File::Spec->rel2abs($match, $path);
82 $add_recursively->($fullmatch, $iteration + 1, @more, $match);
86 $match =~ s/\..*// unless $keep_extension;
87 push @found, join '', $prefix,
88 join $outsep, @directories, @more, $match;
96 for my $subdir (@directories)
98 $path = File::Spec->catdir($path, $subdir);
102 $add_recursively->($path, 0);
105 return $orig->(@_), @found;