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