3 package Devel::REPL::Plugin::CompletionDriver::INC;
4 # ABSTRACT: Complete module names in use and require
6 our $VERSION = '1.003027';
8 use Devel::REPL::Plugin;
9 use Devel::REPL::Plugin::Completion; # die early if cannot load
12 use namespace::autoclean;
16 $self->load_plugin('Completion');
19 around complete => sub {
21 my ($self, $text, $document) = @_;
23 my $last = $self->last_ppi_element($document, 'PPI::Statement::Include');
26 unless $last->isa('PPI::Statement::Include');
28 my @elements = $last->children;
29 shift @elements; # use or require
31 # too late for us to care, they're completing on something like
33 # OR they just have "use " and are tab completing. we'll spare them the flood
37 my $package = shift @elements;
40 my $keep_extension = 0;
43 # require "Foo/Bar.pm" -- not supported yet, ->string doesn't work for
44 # partially completed elements
45 #if ($package->isa('PPI::Token::Quote'))
47 # # we need to strip off the leading quote and stash it
48 # $package = $package->string;
49 # my $start = index($package->quote, $package);
50 # $prefix = substr($package->quote, 0, $start);
52 # # we're completing something like: require "Foo/Bar.pm"
53 # $outsep = $insep = '/';
54 # $keep_extension = 1;
58 # the goofball is using the ancient ' package sep, we'll humor him
63 my @directories = split $insep, $package;
65 # split drops trailing fields
66 push @directories, '' if $package =~ /(?:$insep)$/;
67 my $final = pop @directories;
68 my $final_re = qr/^\Q$final/;
72 # most VCSes don't litter every single fucking directory with garbage. if you
73 # know of any other, just stick them in here. No one wants to complete
74 # Devel::REPL::Plugin::.svn
82 # this will take a directory and add to @found all of the possible matches
84 $add_recursively = sub {
85 my ($path, $iteration, @more) = @_;
86 opendir((my $dirhandle), $path) || return;
87 for (grep { !$ignored{$_} } readdir $dirhandle)
91 # if this is the first time around, we need respect whatever the user had
92 # at the very end when he pressed tab
93 next if $iteration == 0 && $match !~ $final_re;
95 my $fullmatch = File::Spec->rel2abs($match, $path);
98 $add_recursively->($fullmatch, $iteration + 1, @more, $match);
102 $match =~ s/\..*// unless $keep_extension;
103 push @found, join '', $prefix,
104 join $outsep, @directories, @more, $match;
109 # look through all of
114 # match all of the fragments they have, so "use Moose::Meta::At<tab>"
115 # will only begin looking in ../Moose/Meta/
116 for my $subdir (@directories)
118 $path = File::Spec->catdir($path, $subdir);
119 -d $path or next INC;
122 $add_recursively->($path, 0);
125 return $orig->(@_), @found;
136 Shawn M Moore, C<< <sartak at gmail dot com> >>