Default for Completion is *no* filename completion
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Completion.pm
index 91424ae..1e2b09a 100644 (file)
@@ -5,48 +5,55 @@ use PPI;
 use namespace::clean -except => [ 'meta' ];
 
 has current_matches => (
-  is => 'rw',
-  isa => 'ArrayRef',
-  lazy => 1,
-  default => sub { [] },
+   is => 'rw',
+   isa => 'ArrayRef',
+   lazy => 1,
+   default => sub { [] },
 );
 
 has match_index => (
-  is => 'rw',
-  isa => 'Int',
-  lazy => 1,
-  default => sub { 0 },
+   is => 'rw',
+   isa => 'Int',
+   lazy => 1,
+   default => sub { 0 },
 );
 
 has no_term_class_warning => (
-  isa => "Bool",
-  is  => "rw",
-  default => 0,
+   isa => "Bool",
+   is  => "rw",
+   default => 0,
+);
+
+has do_readline_filename_completion => (  # so default is no if Completion loaded
+   isa => "Bool",
+   is  => "rw",
+   lazy => 1,
+   default => sub { 0 },
 );
 
 before 'read' => sub {
-  my ($self) = @_;
+   my ($self) = @_;
 
-  if ((!$self->term->isa("Term::ReadLine::Gnu") and !$self->term->isa("Term::ReadLine::Perl"))
-        and !$self->no_term_class_warning) {
-     warn "Term::ReadLine::Gnu or Term::ReadLine::Perl is required for the Completion plugin to work";
-     $self->no_term_class_warning(1);
-  }
+   if ((!$self->term->isa("Term::ReadLine::Gnu") and !$self->term->isa("Term::ReadLine::Perl"))
+         and !$self->no_term_class_warning) {
+      warn "Term::ReadLine::Gnu or Term::ReadLine::Perl is required for the Completion plugin to work";
+      $self->no_term_class_warning(1);
+   }
 
-  my $weakself = $self;
-  weaken($weakself);
+   my $weakself = $self;
+   weaken($weakself);
 
-  if ($self->term->isa("Term::ReadLine::Gnu")) {
-     $self->term->Attribs->{attempted_completion_function} = sub {
-        $weakself->_completion(@_);
-     };
-  }
+   if ($self->term->isa("Term::ReadLine::Gnu")) {
+      $self->term->Attribs->{attempted_completion_function} = sub {
+         $weakself->_completion(@_);
+      };
+   }
 
-  if ($self->term->isa("Term::ReadLine::Perl")) {
-     $self->term->Attribs->{completion_function} = sub {
-        $weakself->_completion(@_);
-     };
-  }
+   if ($self->term->isa("Term::ReadLine::Perl")) {
+      $self->term->Attribs->{completion_function} = sub {
+         $weakself->_completion(@_);
+      };
+   }
 
 };
 
@@ -72,9 +79,10 @@ sub _completion {
       if (scalar(@matches)) {
          return @matches;
       } else {
-         return readline::rl_filename_list($text);
+         return ($self->do_readline_filename_completion) ? readline::rl_filename_list($text) : () ;
       }
    } else {
+      $self->term->Attribs->{attempted_completion_over} = 1 unless $self->do_readline_filename_completion;
       if (scalar(@matches)) {
          return $self->term->completion_matches($text, sub {
                my ($text, $state) = @_;
@@ -90,25 +98,24 @@ sub _completion {
                return $self->current_matches->[$self->match_index];
             });
       } else {
-         # fall back to filename completion for Term::ReadLine::Gnu
          return;
       }
    }
 }
 
 sub complete {
-  return ();
+   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;
+   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;