1 package Devel::REPL::Plugin::CompletionDriver::INC;
2 use Devel::REPL::Plugin;
5 use namespace::clean -except => [ 'meta' ];
8 Devel::REPL::Plugin::Completion
11 around complete => sub {
13 my ($self, $text, $document) = @_;
15 my $last = $self->last_ppi_element($document, 'PPI::Statement::Include');
18 unless $last->isa('PPI::Statement::Include');
20 my @elements = $last->children;
21 shift @elements; # use or require
23 # too late for us to care, they're completing on something like
25 # OR they just have "use " and are tab completing. we'll spare them the flood
29 my $package = shift @elements;
32 my $keep_extension = 0;
35 # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for
36 # partially completed elements
37 #if ($package->isa('PPI::Token::Quote'))
39 # # we need to strip off the leading quote and stash it
40 # $package = $package->string;
41 # my $start = index($package->quote, $package);
42 # $prefix = substr($package->quote, 0, $start);
44 # # we're completing something like: require "Foo/Bar.pm"
45 # $outsep = $insep = '/';
46 # $keep_extension = 1;
50 # the goofball is using the ancient ' package sep, we'll humor him
55 my @directories = split $insep, $package;
57 # split drops trailing fields
58 push @directories, '' if $package =~ /(?:$insep)$/;
59 my $final = pop @directories;
60 my $final_re = qr/^\Q$final/;
64 # most VCSes don't litter every single fucking directory with garbage. if you
65 # know of any other, just stick them in here. noone wants to complete
66 # Devel::REPL::Plugin::.svn
74 # this will take a directory and add to @found all of the possible matches
76 $add_recursively = sub {
77 my ($path, $iteration, @more) = @_;
78 opendir((my $dirhandle), $path);
79 for (grep { !$ignored{$_} } readdir $dirhandle)
83 # if this is the first time around, we need respect whatever the user had
84 # at the very end when he pressed tab
85 next if $iteration == 0 && $match !~ $final_re;
87 my $fullmatch = File::Spec->rel2abs($match, $path);
90 $add_recursively->($fullmatch, $iteration + 1, @more, $match);
94 $match =~ s/\..*// unless $keep_extension;
95 push @found, join '', $prefix,
96 join $outsep, @directories, @more, $match;
101 # look through all of
106 # match all of the fragments they have, so "use Moose::Meta::At<tab>"
107 # will only begin looking in ../Moose/Meta/
108 for my $subdir (@directories)
110 $path = File::Spec->catdir($path, $subdir);
111 -d $path or next INC;
114 $add_recursively->($path, 0);
117 return $orig->(@_), @found;
126 Devel::REPL::Plugin::CompletionDriver::INC - Complete module names in use and require
130 Shawn M Moore, C<< <sartak at gmail dot com> >>