From 903b05bdb8c1224deabd9674bb4be1bae5195312 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 4 Jul 2013 13:07:16 -0400 Subject: refactor this to avoid some duplication --- lib/Reply/Plugin/Autocomplete/Globals.pm | 7 ++++--- lib/Reply/Plugin/Autocomplete/Lexicals.pm | 10 ++++++---- lib/Reply/Plugin/Autocomplete/Methods.pm | 6 +++++- lib/Reply/Util.pm | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 lib/Reply/Util.pm (limited to 'lib') diff --git a/lib/Reply/Plugin/Autocomplete/Globals.pm b/lib/Reply/Plugin/Autocomplete/Globals.pm index 99ab864..ac2ca3e 100644 --- a/lib/Reply/Plugin/Autocomplete/Globals.pm +++ b/lib/Reply/Plugin/Autocomplete/Globals.pm @@ -7,6 +7,8 @@ use base 'Reply::Plugin'; use Package::Stash; +use Reply::Util qw($fq_ident_rx $fq_varname_rx); + =head1 SYNOPSIS ; .replyrc @@ -32,7 +34,7 @@ sub tab_handler { my $self = shift; my ($line) = @_; - my ($maybe_var) = $line =~ /([\$\@\%\&\*]\s*[0-9A-Z_a-z:]*)$/; + my ($maybe_var) = $line =~ /($fq_varname_rx)$/; return unless $maybe_var; $maybe_var =~ s/\s+//g; @@ -90,8 +92,7 @@ sub _recursive_symbols { # to not also block out punctuation variables. # XXX fix for unicode # XXX fix for variables like ${^GLOBAL_PHASE} - next unless $name =~ /^[A-Z_a-z][0-9A-Z_a-z]*(?:::)?$/ - || length($name) == 1; + next unless $name =~ /^$fq_ident_rx(?:::)?$/ || length($name) == 1; if ($name =~ s/::$//) { my $next = Package::Stash->new(join('::', $stash_name, $name)); diff --git a/lib/Reply/Plugin/Autocomplete/Lexicals.pm b/lib/Reply/Plugin/Autocomplete/Lexicals.pm index 08d4c02..dd953bf 100644 --- a/lib/Reply/Plugin/Autocomplete/Lexicals.pm +++ b/lib/Reply/Plugin/Autocomplete/Lexicals.pm @@ -5,6 +5,8 @@ use warnings; use base 'Reply::Plugin'; +use Reply::Util qw($varname_rx); + =head1 SYNOPSIS ; .replyrc @@ -18,9 +20,6 @@ Perl code. =cut -# XXX unicode? -my $var_name_rx = qr/[\$\@\%]\s*(?:[A-Z_a-z][0-9A-Z_a-z]*)?/; - sub new { my $class = shift; @@ -41,11 +40,14 @@ sub tab_handler { my $self = shift; my ($line) = @_; - my ($var) = $line =~ /($var_name_rx)$/; + my ($var) = $line =~ /($varname_rx)$/; return unless $var; my ($sigil, $name_prefix) = $var =~ /(.)(.*)/; + # these can't be lexicals + return if $sigil eq '&' || $sigil eq '*'; + my $env = { map { %$_ } values %{ $self->{env} } }; my @env = keys %$env; diff --git a/lib/Reply/Plugin/Autocomplete/Methods.pm b/lib/Reply/Plugin/Autocomplete/Methods.pm index 4b1c4f5..7421226 100644 --- a/lib/Reply/Plugin/Autocomplete/Methods.pm +++ b/lib/Reply/Plugin/Autocomplete/Methods.pm @@ -9,6 +9,8 @@ use MRO::Compat; use Package::Stash; use Scalar::Util 'blessed'; +use Reply::Util qw($ident_rx $fq_ident_rx $fq_varname_rx); + =head1 SYNOPSIS ; .replyrc @@ -50,8 +52,10 @@ sub tab_handler { my $self = shift; my ($line) = @_; - my ($invocant, $method) = $line =~ /((?:\$\s*)?[A-Z_a-z][0-9A-Z_a-z:]*)->([A-Z_a-z][0-9A-Z_a-z]*)?$/; + my ($invocant, $method) = $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; diff --git a/lib/Reply/Util.pm b/lib/Reply/Util.pm new file mode 100644 index 0000000..cb33466 --- /dev/null +++ b/lib/Reply/Util.pm @@ -0,0 +1,17 @@ +package Reply::Util; +use strict; +use warnings; + +use Exporter 'import'; +our @EXPORT_OK = qw($ident_rx $varname_rx $fq_ident_rx $fq_varname_rx); + +# XXX this should be updated for unicode +our $varstart_rx = qr/[A-Z_a-z]/; +our $varcont_rx = qr/[0-9A-Z_a-z]/; +our $ident_rx = qr/${varstart_rx}${varcont_rx}*/; +our $sigil_rx = qr/[\$\@\%\&\*]/; +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+)?/; + +1; -- cgit v1.2.3-54-g00ecf