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