3 package Devel::REPL::Plugin::CompletionDriver::INC;
4 use Devel::REPL::Plugin;
7 use namespace::autoclean;
11 $self->load_plugin('Completion');
14 around complete => sub {
16 my ($self, $text, $document) = @_;
18 my $last = $self->last_ppi_element($document, 'PPI::Statement::Include');
21 unless $last->isa('PPI::Statement::Include');
23 my @elements = $last->children;
24 shift @elements; # use or require
26 # too late for us to care, they're completing on something like
28 # OR they just have "use " and are tab completing. we'll spare them the flood
32 my $package = shift @elements;
35 my $keep_extension = 0;
38 # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for
39 # partially completed elements
40 #if ($package->isa('PPI::Token::Quote'))
42 # # we need to strip off the leading quote and stash it
43 # $package = $package->string;
44 # my $start = index($package->quote, $package);
45 # $prefix = substr($package->quote, 0, $start);
47 # # we're completing something like: require "Foo/Bar.pm"
48 # $outsep = $insep = '/';
49 # $keep_extension = 1;
53 # the goofball is using the ancient ' package sep, we'll humor him
58 my @directories = split $insep, $package;
60 # split drops trailing fields
61 push @directories, '' if $package =~ /(?:$insep)$/;
62 my $final = pop @directories;
63 my $final_re = qr/^\Q$final/;
67 # most VCSes don't litter every single fucking directory with garbage. if you
68 # know of any other, just stick them in here. noone wants to complete
69 # Devel::REPL::Plugin::.svn
77 # this will take a directory and add to @found all of the possible matches
79 $add_recursively = sub {
80 my ($path, $iteration, @more) = @_;
81 opendir((my $dirhandle), $path) || return;
82 for (grep { !$ignored{$_} } readdir $dirhandle)
86 # if this is the first time around, we need respect whatever the user had
87 # at the very end when he pressed tab
88 next if $iteration == 0 && $match !~ $final_re;
90 my $fullmatch = File::Spec->rel2abs($match, $path);
93 $add_recursively->($fullmatch, $iteration + 1, @more, $match);
97 $match =~ s/\..*// unless $keep_extension;
98 push @found, join '', $prefix,
99 join $outsep, @directories, @more, $match;
104 # look through all of
109 # match all of the fragments they have, so "use Moose::Meta::At<tab>"
110 # will only begin looking in ../Moose/Meta/
111 for my $subdir (@directories)
113 $path = File::Spec->catdir($path, $subdir);
114 -d $path or next INC;
117 $add_recursively->($path, 0);
120 return $orig->(@_), @found;
129 Devel::REPL::Plugin::CompletionDriver::INC - Complete module names in use and require
133 Shawn M Moore, C<< <sartak at gmail dot com> >>