3 package Devel::REPL::Plugin::CompletionDriver::INC;
5 our $VERSION = '1.003027';
7 use Devel::REPL::Plugin;
8 use Devel::REPL::Plugin::Completion; # die early if cannot load
11 use namespace::autoclean;
15 $self->load_plugin('Completion');
18 around complete => sub {
20 my ($self, $text, $document) = @_;
22 my $last = $self->last_ppi_element($document, 'PPI::Statement::Include');
25 unless $last->isa('PPI::Statement::Include');
27 my @elements = $last->children;
28 shift @elements; # use or require
30 # too late for us to care, they're completing on something like
32 # OR they just have "use " and are tab completing. we'll spare them the flood
36 my $package = shift @elements;
39 my $keep_extension = 0;
42 # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for
43 # partially completed elements
44 #if ($package->isa('PPI::Token::Quote'))
46 # # we need to strip off the leading quote and stash it
47 # $package = $package->string;
48 # my $start = index($package->quote, $package);
49 # $prefix = substr($package->quote, 0, $start);
51 # # we're completing something like: require "Foo/Bar.pm"
52 # $outsep = $insep = '/';
53 # $keep_extension = 1;
57 # the goofball is using the ancient ' package sep, we'll humor him
62 my @directories = split $insep, $package;
64 # split drops trailing fields
65 push @directories, '' if $package =~ /(?:$insep)$/;
66 my $final = pop @directories;
67 my $final_re = qr/^\Q$final/;
71 # most VCSes don't litter every single fucking directory with garbage. if you
72 # know of any other, just stick them in here. No one wants to complete
73 # Devel::REPL::Plugin::.svn
81 # this will take a directory and add to @found all of the possible matches
83 $add_recursively = sub {
84 my ($path, $iteration, @more) = @_;
85 opendir((my $dirhandle), $path) || return;
86 for (grep { !$ignored{$_} } readdir $dirhandle)
90 # if this is the first time around, we need respect whatever the user had
91 # at the very end when he pressed tab
92 next if $iteration == 0 && $match !~ $final_re;
94 my $fullmatch = File::Spec->rel2abs($match, $path);
97 $add_recursively->($fullmatch, $iteration + 1, @more, $match);
101 $match =~ s/\..*// unless $keep_extension;
102 push @found, join '', $prefix,
103 join $outsep, @directories, @more, $match;
108 # look through all of
113 # match all of the fragments they have, so "use Moose::Meta::At<tab>"
114 # will only begin looking in ../Moose/Meta/
115 for my $subdir (@directories)
117 $path = File::Spec->catdir($path, $subdir);
118 -d $path or next INC;
121 $add_recursively->($path, 0);
124 return $orig->(@_), @found;
133 Devel::REPL::Plugin::CompletionDriver::INC - Complete module names in use and require
137 Shawn M Moore, C<< <sartak at gmail dot com> >>