From 600e0172ac0987d32918a9273308a99206402c8e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 27 Jun 2013 15:01:13 -0400 Subject: split out different completion types --- lib/Reply/Plugin/Autocomplete.pm | 71 ------------------------------- lib/Reply/Plugin/Autocomplete/Keywords.pm | 39 +++++++++++++++++ lib/Reply/Plugin/Autocomplete/Packages.pm | 53 +++++++++++++++++++++++ 3 files changed, 92 insertions(+), 71 deletions(-) delete mode 100644 lib/Reply/Plugin/Autocomplete.pm create mode 100644 lib/Reply/Plugin/Autocomplete/Keywords.pm create mode 100644 lib/Reply/Plugin/Autocomplete/Packages.pm diff --git a/lib/Reply/Plugin/Autocomplete.pm b/lib/Reply/Plugin/Autocomplete.pm deleted file mode 100644 index f504f0f..0000000 --- a/lib/Reply/Plugin/Autocomplete.pm +++ /dev/null @@ -1,71 +0,0 @@ -package Reply::Plugin::Autocomplete; -use strict; -use warnings; -# ABSTRACT: tab complete your input - -use base 'Reply::Plugin'; - -use B::Keywords qw/@Functions @Barewords/; -use Module::Runtime '$module_name_rx'; - -=head1 SYNOPSIS - - ; .replyrc - [Autocomplete] - -=head1 DESCRIPTION - -This plugin registers a tab key handler to autocomplete Perl code. - -=cut - -sub tab_handler { - my ($self, $line) = @_; - - return ( - $self->_tab_keyword($line), - $self->_tab_package_loaded($line), - ); -} - -sub _tab_keyword { - my ($self, $line) = @_; - - my ($last_word) = $line =~ /(\w+)$/; - return unless $last_word; - - my $re = qr/^\Q$last_word/; - - return grep { $_ =~ $re } @Functions, @Barewords; -} - -sub _tab_package_loaded { - my ($self, $line) = @_; - - # $module_name_rx does not permit trailing :: - my ($package_fragment) = $line =~ /($module_name_rx(?:::)?)$/; - return unless $package_fragment; - - my $file_fragment = $package_fragment; - $file_fragment =~ s{::}{/}g; - - my $re = qr/^\Q$file_fragment/; - - my @results; - for my $inc (keys %INC) { - if ($inc =~ $re) { - $inc =~ s{/}{::}g; - $inc =~ s{\.pm$}{}; - push @results, $inc; - } - } - - return @results; -} - -=for Pod::Coverage - tab_handler - -=cut - -1; diff --git a/lib/Reply/Plugin/Autocomplete/Keywords.pm b/lib/Reply/Plugin/Autocomplete/Keywords.pm new file mode 100644 index 0000000..85bfe1e --- /dev/null +++ b/lib/Reply/Plugin/Autocomplete/Keywords.pm @@ -0,0 +1,39 @@ +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 ($last_word) = $line =~ /(\w+)$/; + return unless $last_word; + + my $re = qr/^\Q$last_word/; + + return grep { $_ =~ $re } @Functions, @Barewords; +} + +=for Pod::Coverage + tab_handler + +=cut + +1; diff --git a/lib/Reply/Plugin/Autocomplete/Packages.pm b/lib/Reply/Plugin/Autocomplete/Packages.pm new file mode 100644 index 0000000..4f7f2a4 --- /dev/null +++ b/lib/Reply/Plugin/Autocomplete/Packages.pm @@ -0,0 +1,53 @@ +package Reply::Plugin::Autocomplete::Packages; +use strict; +use warnings; +# ABSTRACT: tab completion for package names + +use base 'Reply::Plugin'; + +use Module::Runtime '$module_name_rx'; + +=head1 SYNOPSIS + + ; .replyrc + [ReadLine] + [Autocomplete::Packages] + +=head1 DESCRIPTION + +This plugin registers a tab key handler to autocomplete package names in Perl +code. + +=cut + +sub tab_handler { + my $self = shift; + my ($line) = @_; + + # $module_name_rx does not permit trailing :: + my ($package_fragment) = $line =~ /($module_name_rx(?:::)?)$/; + return unless $package_fragment; + + my $file_fragment = $package_fragment; + $file_fragment =~ s{::}{/}g; + + my $re = qr/^\Q$file_fragment/; + + my @results; + for my $inc (keys %INC) { + if ($inc =~ $re) { + $inc =~ s{/}{::}g; + $inc =~ s{\.pm$}{}; + push @results, $inc; + } + } + + return @results; +} + +=for Pod::Coverage + tab_handler + +=cut + +1; -- cgit v1.2.3-54-g00ecf