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