From 9ad70dd68d45203970fed6e7c2bdca13da13d8c4 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 10 Jul 2013 00:49:28 -0400 Subject: factor this out --- dist.ini | 4 +++- lib/Reply/Plugin/Autocomplete/Methods.pm | 24 +++++---------------- lib/Reply/Util.pm | 37 +++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/dist.ini b/dist.ini index 866595c..c6d0fbc 100644 --- a/dist.ini +++ b/dist.ini @@ -42,8 +42,10 @@ Proc::InvokeEditor = 0 Term::ReadLine::Gnu = 0 B::Keywords = 0 Package::Stash = 0 -MRO::Compat = 0 ; XXX it'd be nice if we could make this recommended instead of required [OSPrereqs / MSWin32] Win32::Console::ANSI = 0 + +[PerlVersionPrereqs / 5.010] +MRO::Compat = 0 diff --git a/lib/Reply/Plugin/Autocomplete/Methods.pm b/lib/Reply/Plugin/Autocomplete/Methods.pm index d4319b2..b643fb2 100644 --- a/lib/Reply/Plugin/Autocomplete/Methods.pm +++ b/lib/Reply/Plugin/Autocomplete/Methods.pm @@ -5,11 +5,9 @@ use warnings; use base 'Reply::Plugin'; -use MRO::Compat; -use Package::Stash; use Scalar::Util 'blessed'; -use Reply::Util qw($ident_rx $fq_ident_rx $fq_varname_rx); +use Reply::Util qw($ident_rx $fq_ident_rx $fq_varname_rx methods); =head1 SYNOPSIS @@ -52,12 +50,12 @@ sub tab_handler { my $self = shift; my ($line) = @_; - my ($invocant, $method) = $line =~ /($fq_varname_rx|$fq_ident_rx)->($ident_rx)?$/; + my ($invocant, $method_prefix) = $line =~ /($fq_varname_rx|$fq_ident_rx)->($ident_rx)?$/; return unless $invocant; # XXX unicode return unless $invocant =~ /^[\$A-Z_a-z]/; - $method = '' unless defined $method; + $method_prefix = '' unless defined $method_prefix; my $class; if ($invocant =~ /^\$/) { @@ -73,21 +71,9 @@ sub tab_handler { $class = $invocant; } - my @mro = ( - @{ mro::get_linear_isa('UNIVERSAL') }, - @{ mro::get_linear_isa($class) }, - ); - my @results; - for my $package (@mro) { - my $stash = eval { Package::Stash->new($package) }; - next unless $stash; - - for my $stash_method ($stash->list_all_symbols('CODE')) { - next unless index($stash_method, $method) == 0; - - push @results, $stash_method; - } + for my $method (methods($class)) { + push @results, $method if index($method, $method_prefix) == 0; } return sort @results; diff --git a/lib/Reply/Util.pm b/lib/Reply/Util.pm index 5f22ecd..e6d193a 100644 --- a/lib/Reply/Util.pm +++ b/lib/Reply/Util.pm @@ -2,8 +2,23 @@ package Reply::Util; use strict; use warnings; +BEGIN { + if ($] < 5.010) { + require MRO::Compat; + } + else { + require mro; + } +} + +use Package::Stash; +use Scalar::Util 'blessed'; + use Exporter 'import'; -our @EXPORT_OK = qw($ident_rx $varname_rx $fq_ident_rx $fq_varname_rx); +our @EXPORT_OK = qw( + $ident_rx $varname_rx $fq_ident_rx $fq_varname_rx + methods +); # XXX this should be updated for unicode our $varstart_rx = qr/[A-Z_a-z]/; @@ -14,4 +29,24 @@ our $varname_rx = qr/$sigil_rx\s*$ident_rx/; our $fq_ident_rx = qr/$ident_rx(?:::$varcont_rx+)*/; our $fq_varname_rx = qr/$varname_rx(?:::$varcont_rx+)*/; +sub methods { + my ($invocant) = @_; + + my $class = blessed($invocant) || $invocant; + + my @mro = ( + @{ mro::get_linear_isa('UNIVERSAL') }, + @{ mro::get_linear_isa($class) }, + ); + + my @methods; + for my $package (@mro) { + my $stash = eval { Package::Stash->new($package) }; + next unless $stash; + push @methods, $stash->list_all_symbols('CODE'); + } + + return @methods; +} + 1; -- cgit v1.2.3-54-g00ecf