Changeset e97c5d05


Ignore:
Timestamp:
Sep 30, 2009, 1:02:36 AM (15 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
b06a888
Parents:
9e5b5fd
git-author:
Nelson Elhage <nelhage@mit.edu> (09/14/09 09:57:55)
git-committer:
David Benjamin <davidben@mit.edu> (09/30/09 01:02:36)
Message:
Implement a shift_words method on completion contexts.

This allows completion methods to easily pass on the remainder of a
command-line to another completer if appropriate.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl/Completion/Context.pm

    r7be5d8b re97c5d05  
    4040
    4141use base qw(Class::Accessor::Fast);
     42use Carp qw(croak);
    4243
    4344__PACKAGE__->mk_ro_accessors(qw(line point words word word_point
     
    6465       };
    6566    return bless($self, $class);
     67}
     68
     69=head2 shift_words N
     70
     71Returns a new C<Context> object, with the leading C<N> words
     72stripped. All fields are updated as appopriate. If C<N> > C<<
     73$self->word >>, C<croak>s with an error message.
     74
     75=cut
     76
     77sub shift_words {
     78    my $self = shift;
     79    my $n    = shift;
     80
     81    if($n > $self->word) {
     82        croak "Context::shift: Unable to shift $n words";
     83    }
     84
     85    my $before = substr($self->line, 0, $self->point);
     86    my $after  = substr($self->line, $self->point);
     87
     88    return BarnOwl::Completion::Context->new(BarnOwl::skiptokens($before, $n),
     89                                             $after);
    6690}
    6791
  • t/completion.t

    rb017b03 re97c5d05  
    2020    my $after_point = shift;
    2121   
     22    my $ctx = BarnOwl::Completion::Context->new($before_point,
     23                                                $after_point);
     24    is($ctx->line, $before_point . $after_point);
     25    is($ctx->point, length $before_point);
     26
     27    test_ctx($ctx, @_);
     28}
     29
     30sub test_ctx {
     31    local $Test::Builder::Level = $Test::Builder::Level + 1;
     32
     33    my $ctx = shift;
     34
    2235    my $words = shift;
    2336    my $word = shift;
     
    2740    my $word_end   = shift;
    2841
    29     my $ctx = BarnOwl::Completion::Context->new($before_point,
    30                                                 $after_point);
    31 
    32     is($ctx->line, $before_point . $after_point);
    33     is($ctx->point, length $before_point);
    3442    is_deeply($ctx->words, $words);
    3543    if (defined($word)) {
     
    4149}
    4250
     51sub test_shift {
     52    local $Test::Builder::Level = $Test::Builder::Level + 1;
     53
     54    my $before_point = shift;
     55    my $after_point = shift;
     56    my $shift = shift;
     57   
     58    my $ctx = BarnOwl::Completion::Context->new($before_point,
     59                                                $after_point);
     60    $ctx = $ctx->shift_words($shift);
     61
     62    test_ctx($ctx, @_);
     63}
     64
    4365
    4466isa_ok(BarnOwl::Completion::Context->new('Hello, W', 'orld'), 'BarnOwl::Completion::Context');
     
    108130              1, -1, 7, 12);
    109131
    110 
     132## Test Context::shift
     133SKIP: {
     134    skip "Can't yet test code that depends on perlglue.xs", 4;
     135    test_shift('lorem ipsum dolor ', 'sit amet', 0,
     136               [qw(lorem ipsum dolor sit amet)],
     137               3, 0, 18, 21);
     138
     139    test_shift('lorem ipsum dolor ', 'sit amet', 1,
     140               [qw(lorem ipsum dolor sit amet)],
     141               2, 0, 12, 15);
     142
     143    test_shift('lorem ipsum dolor ', 'sit amet', 2,
     144               [qw(lorem ipsum dolor sit amet)],
     145               1, 0, 6, 9);
     146
     147    test_shift('lorem ipsum dolor ', 'sit amet', 3,
     148               [qw(lorem ipsum dolor sit amet)],
     149               0, 0, 0, 3);
     150
     151}
    111152## Test common_prefix
    112153
Note: See TracChangeset for help on using the changeset viewer.