source: perl/lib/BarnOwl/Completion/Context.pm @ 7488f27

release-1.10release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7488f27 was 30c5aab, checked in by David Benjamin <davidben@mit.edu>, 15 years ago
Add function for point-less parses Often, you don't care very much about the point when you do the parse. We rename existing tokenize to tokenize_with_point and add tokenize that does point-less parses. Signed-off-by: David Benjamin <davidben@mit.edu>
  • Property mode set to 100644
File size: 2.1 KB
Line 
1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Completion::Context
7
8=head1 DESCRIPTION
9
10BarnOwl::Completion::Context is the context that is passed to a
11completion function by BarnOwl. It contains information on the text
12being completed.
13
14=head1 METHODS
15
16=head2 line
17
18The entire command-line currently being completed.
19
20=head2 point
21
22The index of the cursor in C<line>, in the range C<[0,len($line)]>.
23
24=head2 words
25
26The current command-line, tokenized according to BarnOwl's
27tokenization rules, as an array reference.
28
29=head2 word
30
31The current word the point is sitting in, as an index into C<words>.
32
33=head2 word_point
34
35The index of the point within C<$words->[$word]>.
36
37=cut
38
39package BarnOwl::Completion::Context;
40
41use base qw(Class::Accessor::Fast);
42use Carp qw(croak);
43use BarnOwl::Parse qw(tokenize_with_point);
44
45__PACKAGE__->mk_ro_accessors(qw(line point words word word_point
46                                word_start word_end));
47
48sub new {
49    my $class = shift;
50    my $before_point = shift;
51    my $after_point = shift;
52
53    my $line  = $before_point . $after_point;
54    my $point = length ($before_point);
55    my ($words, $word, $word_point,
56        $word_start, $word_end) = tokenize_with_point($line, $point);
57    push @$words, '' if scalar @$words <= $word;
58
59    my $self = {
60        line  => $line,
61        point => $point,
62        words => $words,
63        word  => $word,
64        word_point => $word_point,
65        word_start => $word_start,
66        word_end   => $word_end
67       };
68    return bless($self, $class);
69}
70
71=head2 shift_words N
72
73Returns a new C<Context> object, with the leading C<N> words
74stripped. All fields are updated as appopriate. If C<N> > C<<
75$self->word >>, C<croak>s with an error message.
76
77=cut
78
79sub shift_words {
80    my $self = shift;
81    my $n    = shift;
82
83    if($n > $self->word) {
84        croak "Context::shift: Unable to shift $n words";
85    }
86
87    my $before = substr($self->line, 0, $self->point);
88    my $after  = substr($self->line, $self->point);
89
90    return BarnOwl::Completion::Context->new(BarnOwl::skiptokens($before, $n),
91                                             $after);
92}
93
941;
Note: See TracBrowser for help on using the repository browser.