3 package Devel::REPL::Plugin::CompletionDriver::INC;
5 use Devel::REPL::Plugin;
6 use Devel::REPL::Plugin::Completion; # die early if cannot load
9 use namespace::autoclean;
13 $self->load_plugin('Completion');
16 around complete => sub {
18 my ($self, $text, $document) = @_;
20 my $last = $self->last_ppi_element($document, 'PPI::Statement::Include');
23 unless $last->isa('PPI::Statement::Include');
25 my @elements = $last->children;
26 shift @elements; # use or require
28 # too late for us to care, they're completing on something like
30 # OR they just have "use " and are tab completing. we'll spare them the flood
34 my $package = shift @elements;
37 my $keep_extension = 0;
40 # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for
41 # partially completed elements
42 #if ($package->isa('PPI::Token::Quote'))
44 # # we need to strip off the leading quote and stash it
45 # $package = $package->string;
46 # my $start = index($package->quote, $package);
47 # $prefix = substr($package->quote, 0, $start);
49 # # we're completing something like: require "Foo/Bar.pm"
50 # $outsep = $insep = '/';
51 # $keep_extension = 1;
55 # the goofball is using the ancient ' package sep, we'll humor him
60 my @directories = split $insep, $package;
62 # split drops trailing fields
63 push @directories, '' if $package =~ /(?:$insep)$/;
64 my $final = pop @directories;
65 my $final_re = qr/^\Q$final/;
69 # most VCSes don't litter every single fucking directory with garbage. if you
70 # know of any other, just stick them in here. No one wants to complete
71 # Devel::REPL::Plugin::.svn
79 # this will take a directory and add to @found all of the possible matches
81 $add_recursively = sub {
82 my ($path, $iteration, @more) = @_;
83 opendir((my $dirhandle), $path) || return;
84 for (grep { !$ignored{$_} } readdir $dirhandle)
88 # if this is the first time around, we need respect whatever the user had
89 # at the very end when he pressed tab
90 next if $iteration == 0 && $match !~ $final_re;
92 my $fullmatch = File::Spec->rel2abs($match, $path);
95 $add_recursively->($fullmatch, $iteration + 1, @more, $match);
99 $match =~ s/\..*// unless $keep_extension;
100 push @found, join '', $prefix,
101 join $outsep, @directories, @more, $match;
106 # look through all of
111 # match all of the fragments they have, so "use Moose::Meta::At<tab>"
112 # will only begin looking in ../Moose/Meta/
113 for my $subdir (@directories)
115 $path = File::Spec->catdir($path, $subdir);
116 -d $path or next INC;
119 $add_recursively->($path, 0);
122 return $orig->(@_), @found;
131 Devel::REPL::Plugin::CompletionDriver::INC - Complete module names in use and require
135 Shawn M Moore, C<< <sartak at gmail dot com> >>