From: Sartak <Sartak@bd8105ee-0ff8-0310-8827-fb3f25b6796d>
Date: Sun, 25 May 2008 16:50:10 +0000 (+0000)
Subject: Refactor the finding of the last element of the document into a method in Completion
X-Git-Tag: v1.003015~113
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8051a5e0e588f2c30779c2e2982f33e6fb27fda9;p=p5sagit%2FDevel-REPL.git

Refactor the finding of the last element of the document into a method in Completion


git-svn-id: http://dev.catalyst.perl.org/repos/bast/trunk/Devel-REPL@4396 bd8105ee-0ff8-0310-8827-fb3f25b6796d
---

diff --git a/lib/Devel/REPL/Plugin/Completion.pm b/lib/Devel/REPL/Plugin/Completion.pm
index 126b81e..8fbe2bc 100644
--- a/lib/Devel/REPL/Plugin/Completion.pm
+++ b/lib/Devel/REPL/Plugin/Completion.pm
@@ -77,5 +77,16 @@ sub complete {
   return ();
 }
 
+# recursively find the last element
+sub last_ppi_element {
+  my ($self, $document, $type) = @_;
+  my $last = $document;
+  while ($last->can('last_element') && defined($last->last_element)) {
+    $last = $last->last_element;
+    return $last if $type && $last->isa($type);
+  }
+  return $last;
+}
+
 1;
 
diff --git a/lib/Devel/REPL/Plugin/CompletionDriver/Keywords.pm b/lib/Devel/REPL/Plugin/CompletionDriver/Keywords.pm
index 08e9f2b..55d5240 100644
--- a/lib/Devel/REPL/Plugin/CompletionDriver/Keywords.pm
+++ b/lib/Devel/REPL/Plugin/CompletionDriver/Keywords.pm
@@ -7,11 +7,7 @@ around complete => sub {
   my $orig = shift;
   my ($self, $text, $document) = @_;
 
-  # recursively find the last element
-  my $last = $document;
-  while ($last->can('last_element') && defined($last->last_element)) {
-      $last = $last->last_element;
-  }
+  my $last = $self->last_ppi_element($document);
 
   return $orig->(@_)
     unless $last->isa('PPI::Token::Word');
diff --git a/lib/Devel/REPL/Plugin/CompletionDriver/LexEnv.pm b/lib/Devel/REPL/Plugin/CompletionDriver/LexEnv.pm
index 2011896..f346bda 100644
--- a/lib/Devel/REPL/Plugin/CompletionDriver/LexEnv.pm
+++ b/lib/Devel/REPL/Plugin/CompletionDriver/LexEnv.pm
@@ -14,11 +14,7 @@ around complete => sub {
   my $orig = shift;
   my ($self, $text, $document) = @_;
 
-  # recursively find the last element
-  my $last = $document;
-  while ($last->can('last_element') && defined($last->last_element)) {
-      $last = $last->last_element;
-  }
+  my $last = $self->last_ppi_element($document);
 
   return $orig->(@_)
     unless $last->isa('PPI::Token::Symbol');