1 package Devel::REPL::Plugin::CompletionDriver::INC;
2 use Devel::REPL::Plugin;
9 $self->load_plugin('Completion');
12 around complete => sub {
14 my ($self, $text, $document) = @_;
16 my $last = $self->last_ppi_element($document, 'PPI::Statement::Include');
19 unless $last->isa('PPI::Statement::Include');
21 my @elements = $last->children;
22 shift @elements; # use or require
24 # too late for us to care, they're completing on something like
26 # OR they just have "use " and are tab completing. we'll spare them the flood
30 my $package = shift @elements;
33 my $keep_extension = 0;
36 # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for
37 # partially completed elements
38 #if ($package->isa('PPI::Token::Quote'))
40 # # we need to strip off the leading quote and stash it
41 # $package = $package->string;
42 # my $start = index($package->quote, $package);
43 # $prefix = substr($package->quote, 0, $start);
45 # # we're completing something like: require "Foo/Bar.pm"
46 # $outsep = $insep = '/';
47 # $keep_extension = 1;
51 # the goofball is using the ancient ' package sep, we'll humor him
56 my @directories = split $insep, $package;
58 # split drops trailing fields
59 push @directories, '' if $package =~ /(?:$insep)$/;
60 my $final = pop @directories;
61 my $final_re = qr/^\Q$final/;
65 # most VCSes don't litter every single fucking directory with garbage. if you
66 # know of any other, just stick them in here. noone wants to complete
67 # Devel::REPL::Plugin::.svn
75 # this will take a directory and add to @found all of the possible matches
77 $add_recursively = sub {
78 my ($path, $iteration, @more) = @_;
79 opendir((my $dirhandle), $path) || return;
80 for (grep { !$ignored{$_} } readdir $dirhandle)
84 # if this is the first time around, we need respect whatever the user had
85 # at the very end when he pressed tab
86 next if $iteration == 0 && $match !~ $final_re;
88 my $fullmatch = File::Spec->rel2abs($match, $path);
91 $add_recursively->($fullmatch, $iteration + 1, @more, $match);
95 $match =~ s/\..*// unless $keep_extension;
96 push @found, join '', $prefix,
97 join $outsep, @directories, @more, $match;
102 # look through all of
107 # match all of the fragments they have, so "use Moose::Meta::At<tab>"
108 # will only begin looking in ../Moose/Meta/
109 for my $subdir (@directories)
111 $path = File::Spec->catdir($path, $subdir);
112 -d $path or next INC;
115 $add_recursively->($path, 0);
118 return $orig->(@_), @found;
127 Devel::REPL::Plugin::CompletionDriver::INC - Complete module names in use and require
131 Shawn M Moore, C<< <sartak at gmail dot com> >>