1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Completion::Context |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | BarnOwl::Completion::Context is the context that is passed to a |
---|
11 | completion function by BarnOwl. It contains information on the text |
---|
12 | being completed. |
---|
13 | |
---|
14 | =head1 METHODS |
---|
15 | |
---|
16 | =head2 line |
---|
17 | |
---|
18 | The entire command-line currently being completed. |
---|
19 | |
---|
20 | =head2 point |
---|
21 | |
---|
22 | The index of the cursor in C<line>, in the range C<[0,len($line)]>. |
---|
23 | |
---|
24 | =head2 words |
---|
25 | |
---|
26 | The current command-line, tokenized according to BarnOwl's |
---|
27 | tokenization rules, as an array reference. |
---|
28 | |
---|
29 | =head2 word |
---|
30 | |
---|
31 | The current word the point is sitting in, as an index into C<words>. |
---|
32 | |
---|
33 | =head2 word_point |
---|
34 | |
---|
35 | The index of the point within C<$words->[$word]>. |
---|
36 | |
---|
37 | =cut |
---|
38 | |
---|
39 | package BarnOwl::Completion::Context; |
---|
40 | |
---|
41 | use base qw(Class::Accessor::Fast); |
---|
42 | use Carp qw(croak); |
---|
43 | use 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 | |
---|
48 | sub 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 | |
---|
73 | Returns a new C<Context> object, with the leading C<N> words |
---|
74 | stripped. All fields are updated as appopriate. If C<N> > C<< |
---|
75 | $self->word >>, C<croak>s with an error message. |
---|
76 | |
---|
77 | =cut |
---|
78 | |
---|
79 | sub 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 | |
---|
94 | 1; |
---|