Commit | Line | Data |
e4ac8502 |
1 | package Devel::REPL::Plugin::Completion; |
1989c3d2 |
2 | use Devel::REPL::Plugin; |
3 | use Scalar::Util 'weaken'; |
4 | use PPI; |
e2d0b019 |
5 | use namespace::sweep; |
6 | use MooX::Types::MooseLike::Base qw(ArrayRef Int Bool); |
e4ac8502 |
7 | |
1989c3d2 |
8 | has current_matches => ( |
fd81abf1 |
9 | is => 'rw', |
e2d0b019 |
10 | isa => ArrayRef, |
fd81abf1 |
11 | lazy => 1, |
12 | default => sub { [] }, |
1989c3d2 |
13 | ); |
ac71b56c |
14 | |
1989c3d2 |
15 | has match_index => ( |
fd81abf1 |
16 | is => 'rw', |
e2d0b019 |
17 | isa => Int, |
fd81abf1 |
18 | lazy => 1, |
19 | default => sub { 0 }, |
1989c3d2 |
20 | ); |
e4ac8502 |
21 | |
97d28d6b |
22 | has no_term_class_warning => ( |
e2d0b019 |
23 | isa => Bool, |
fd81abf1 |
24 | is => "rw", |
e2d0b019 |
25 | default => sub { 0 }, |
fd81abf1 |
26 | ); |
27 | |
28 | has 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 |
35 | before '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 |
61 | sub _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 | |
107 | sub complete { |
fd81abf1 |
108 | return (); |
1989c3d2 |
109 | } |
e4ac8502 |
110 | |
8051a5e0 |
111 | # recursively find the last element |
112 | sub 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 |
122 | 1; |
1989c3d2 |
123 | |
cfd1094b |
124 | __END__ |
125 | |
126 | =head1 NAME |
127 | |
128 | Devel::REPL::Plugin::Completion - Extensible tab completion |
129 | |
1a00e38d |
130 | =head1 NOTE |
131 | |
132 | By default, the Completion plugin explicitly does I<not> use the Gnu readline |
133 | or Term::ReadLine::Perl fallback filename completion. |
134 | |
135 | Set the attribute C<do_readline_filename_completion> to 1 to enable this feature. |
136 | |
30b459d4 |
137 | =head1 AUTHOR |
138 | |
139 | Shawn M Moore, C<< <sartak at gmail dot com> >> |
140 | |
cfd1094b |
141 | =cut |
142 | |