From d30b4d66828b31823af1885d48a07825b4b570ee Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 10 Jul 2013 02:23:13 -0400 Subject: autocomplete for commands (fixes #18) --- lib/Reply/Plugin.pm | 8 ++++++++ lib/Reply/Plugin/Autocomplete/Commands.pm | 19 +++++++++++++++++++ lib/Reply/Plugin/Autocomplete/Keywords.pm | 1 + lib/Reply/Plugin/Autocomplete/Packages.pm | 1 + 4 files changed, 29 insertions(+) create mode 100644 lib/Reply/Plugin/Autocomplete/Commands.pm diff --git a/lib/Reply/Plugin.pm b/lib/Reply/Plugin.pm index ed3efbb..f17b135 100644 --- a/lib/Reply/Plugin.pm +++ b/lib/Reply/Plugin.pm @@ -3,6 +3,8 @@ use strict; use warnings; # ABSTRACT: base class for Reply plugins +use Reply::Util 'methods'; + =head1 SYNOPSIS package Reply::Plugin::Foo; @@ -183,6 +185,12 @@ sub publish { $self->{publisher}->(@_); } +sub commands { + my $self = shift; + + return map { s/^command_//; $_ } grep { /^command_/ } methods($self); +} + =for Pod::Coverage new diff --git a/lib/Reply/Plugin/Autocomplete/Commands.pm b/lib/Reply/Plugin/Autocomplete/Commands.pm new file mode 100644 index 0000000..ec86208 --- /dev/null +++ b/lib/Reply/Plugin/Autocomplete/Commands.pm @@ -0,0 +1,19 @@ +package Reply::Plugin::Autocomplete::Commands; +use strict; +use warnings; + +use base 'Reply::Plugin'; + +sub tab_handler { + my $self = shift; + my ($line) = @_; + + my ($prefix) = $line =~ /^#(.*)/; + return unless defined $prefix; + + my @commands = $self->publish('commands'); + + return map { "#$_" } sort grep { index($_, $prefix) == 0 } @commands; +} + +1; diff --git a/lib/Reply/Plugin/Autocomplete/Keywords.pm b/lib/Reply/Plugin/Autocomplete/Keywords.pm index 9e22ae4..ee863d1 100644 --- a/lib/Reply/Plugin/Autocomplete/Keywords.pm +++ b/lib/Reply/Plugin/Autocomplete/Keywords.pm @@ -25,6 +25,7 @@ sub tab_handler { 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*$/; diff --git a/lib/Reply/Plugin/Autocomplete/Packages.pm b/lib/Reply/Plugin/Autocomplete/Packages.pm index 91306c4..c2bb6a0 100644 --- a/lib/Reply/Plugin/Autocomplete/Packages.pm +++ b/lib/Reply/Plugin/Autocomplete/Packages.pm @@ -29,6 +29,7 @@ sub tab_handler { # $module_name_rx does not permit trailing :: my ($before, $package_fragment) = $line =~ /(.*?)(${module_name_rx}:?:?)$/; return unless $package_fragment; + return if $before =~ /^#/; # command return if $before =~ /->\s*$/; # method call return if $before =~ /[\$\@\%\&\*]\s*$/; -- cgit v1.2.3-54-g00ecf