3 package Devel::REPL::Plugin::CompletionDriver::INC;
4 use Devel::REPL::Plugin;
5 use Devel::REPL::Plugin::Completion; # die early if cannot load
8 use namespace::autoclean;
12 $self->load_plugin('Completion');
15 around complete => sub {
17 my ($self, $text, $document) = @_;
19 my $last = $self->last_ppi_element($document, 'PPI::Statement::Include');
22 unless $last->isa('PPI::Statement::Include');
24 my @elements = $last->children;
25 shift @elements; # use or require
27 # too late for us to care, they're completing on something like
29 # OR they just have "use " and are tab completing. we'll spare them the flood
33 my $package = shift @elements;
36 my $keep_extension = 0;
39 # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for
40 # partially completed elements
41 #if ($package->isa('PPI::Token::Quote'))
43 # # we need to strip off the leading quote and stash it
44 # $package = $package->string;
45 # my $start = index($package->quote, $package);
46 # $prefix = substr($package->quote, 0, $start);
48 # # we're completing something like: require "Foo/Bar.pm"
49 # $outsep = $insep = '/';
50 # $keep_extension = 1;
54 # the goofball is using the ancient ' package sep, we'll humor him
59 my @directories = split $insep, $package;
61 # split drops trailing fields
62 push @directories, '' if $package =~ /(?:$insep)$/;
63 my $final = pop @directories;
64 my $final_re = qr/^\Q$final/;
68 # most VCSes don't litter every single fucking directory with garbage. if you
69 # know of any other, just stick them in here. noone wants to complete
70 # Devel::REPL::Plugin::.svn
78 # this will take a directory and add to @found all of the possible matches
80 $add_recursively = sub {
81 my ($path, $iteration, @more) = @_;
82 opendir((my $dirhandle), $path) || return;
83 for (grep { !$ignored{$_} } readdir $dirhandle)
87 # if this is the first time around, we need respect whatever the user had
88 # at the very end when he pressed tab
89 next if $iteration == 0 && $match !~ $final_re;
91 my $fullmatch = File::Spec->rel2abs($match, $path);
94 $add_recursively->($fullmatch, $iteration + 1, @more, $match);
98 $match =~ s/\..*// unless $keep_extension;
99 push @found, join '', $prefix,
100 join $outsep, @directories, @more, $match;
105 # look through all of
110 # match all of the fragments they have, so "use Moose::Meta::At<tab>"
111 # will only begin looking in ../Moose/Meta/
112 for my $subdir (@directories)
114 $path = File::Spec->catdir($path, $subdir);
115 -d $path or next INC;
118 $add_recursively->($path, 0);
121 return $orig->(@_), @found;
130 Devel::REPL::Plugin::CompletionDriver::INC - Complete module names in use and require
134 Shawn M Moore, C<< <sartak at gmail dot com> >>