summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/Autocomplete/Keywords.pm
blob: ee863d195cc8f5ba96f9bddc191459afd46f4f39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package Reply::Plugin::Autocomplete::Keywords;
use strict;
use warnings;
# ABSTRACT: tab completion for perl keywords

use base 'Reply::Plugin';

use B::Keywords qw/@Functions @Barewords/;

=head1 SYNOPSIS

  ; .replyrc
  [ReadLine]
  [Autocomplete::Keywords]

=head1 DESCRIPTION

This plugin registers a tab key handler to autocomplete keywords in Perl code.

=cut

sub tab_handler {
    my $self = shift;
    my ($line) = @_;

    my ($before, $last_word) = $line =~ /(.*?)(\w+)$/;
    return unless $last_word;
    return if $before =~ /^#/; # command
    return if $before =~ /::$/; # Package::function call
    return if $before =~ /->\s*$/; # method call
    return if $before =~ /[\$\@\%\&\*]\s*$/;

    my $re = qr/^\Q$last_word/;

    return grep { $_ =~ $re } @Functions, @Barewords;
}

1;