keep $VERSION right in the repo
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Completion.pm
1 use strict;
2 use warnings;
3 package Devel::REPL::Plugin::Completion;
4
5 our $VERSION = '1.003027';
6
7 use Devel::REPL::Plugin;
8 use Scalar::Util 'weaken';
9 use PPI;
10 use namespace::autoclean;
11
12 has current_matches => (
13    is => 'rw',
14    isa => 'ArrayRef',
15    lazy => 1,
16    default => sub { [] },
17 );
18
19 has match_index => (
20    is => 'rw',
21    isa => 'Int',
22    lazy => 1,
23    default => sub { 0 },
24 );
25
26 has no_term_class_warning => (
27    isa => "Bool",
28    is  => "rw",
29    default => 0,
30 );
31
32 has do_readline_filename_completion => (  # so default is no if Completion loaded
33    isa => "Bool",
34    is  => "rw",
35    lazy => 1,
36    default => sub { 0 },
37 );
38
39 before 'read' => sub {
40    my ($self) = @_;
41
42    if ((!$self->term->isa("Term::ReadLine::Gnu") and !$self->term->isa("Term::ReadLine::Perl"))
43          and !$self->no_term_class_warning) {
44       warn "Term::ReadLine::Gnu or Term::ReadLine::Perl is required for the Completion plugin to work";
45       $self->no_term_class_warning(1);
46    }
47
48    my $weakself = $self;
49    weaken($weakself);
50
51    if ($self->term->isa("Term::ReadLine::Gnu")) {
52       $self->term->Attribs->{attempted_completion_function} = sub {
53          $weakself->_completion(@_);
54       };
55    }
56
57    if ($self->term->isa("Term::ReadLine::Perl")) {
58       $self->term->Attribs->{completion_function} = sub {
59          $weakself->_completion(@_);
60       };
61    }
62
63 };
64
65 sub _completion {
66    my $is_trp = scalar(@_) == 4 ? 1 : 0;
67    my ($self, $text, $line, $start, $end) = @_;
68    $end = $start+length($text) if $is_trp;
69
70    # we're discarding everything after the cursor for completion purposes
71    # we can't just use $text because we want all the code before the cursor to
72    # matter, not just the current word
73    substr($line, $end) = '';
74
75    my $document = PPI::Document->new(\$line);
76    return unless defined($document);
77
78    $document->prune('PPI::Token::Whitespace');
79
80    my @matches = $self->complete($text, $document);
81
82    # iterate through the completions
83    if ($is_trp) {
84       if (scalar(@matches)) {
85          return @matches;
86       } else {
87          return ($self->do_readline_filename_completion) ? readline::rl_filename_list($text) : () ;
88       }
89    } else {
90       $self->term->Attribs->{attempted_completion_over} = 1 unless $self->do_readline_filename_completion;
91       if (scalar(@matches)) {
92          return $self->term->completion_matches($text, sub {
93                my ($text, $state) = @_;
94
95                if (!$state) {
96                   $self->current_matches(\@matches);
97                   $self->match_index(0);
98                }
99                else {
100                   $self->match_index($self->match_index + 1);
101                }
102
103                return $self->current_matches->[$self->match_index];
104             });
105       } else {
106          return;
107       }
108    }
109 }
110
111 sub complete {
112    return ();
113 }
114
115 # recursively find the last element
116 sub last_ppi_element {
117    my ($self, $document, $type) = @_;
118    my $last = $document;
119    while ($last->can('last_element') && defined($last->last_element)) {
120       $last = $last->last_element;
121       return $last if $type && $last->isa($type);
122    }
123    return $last;
124 }
125
126 1;
127
128 __END__
129
130 =head1 NAME
131
132 Devel::REPL::Plugin::Completion - Extensible tab completion
133
134 =head1 NOTE
135
136 By default, the Completion plugin explicitly does I<not> use the Gnu readline
137 or Term::ReadLine::Perl fallback filename completion.
138
139 Set the attribute C<do_readline_filename_completion> to 1 to enable this feature.
140
141 =head1 AUTHOR
142
143 Shawn M Moore, C<< <sartak at gmail dot com> >>
144
145 =cut
146