Document/BibTeX.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 164 CC 17 SCAR 41.0
CC
17
Cov
0%
autocomplete
110.0
find_completions
72.0
BEGIN
2.0
CRAP 60.6
1 package Padre::Document::BibTeX;
2 BEGIN {
3 $Padre::Document::BibTeX::VERSION = '0.13';
4 }
5  
6 # ABSTRACT: BibTeX support document for Padre
7  
8 use 5.008;
9 use strict;
10 use warnings;
11 use Carp ();
12 use Padre::Document ();
13  
14 our @ISA = 'Padre::Document';
15  
16 sub task_functions {
17 return '';
18 }
19  
20 sub task_outline {
21 return '';
22 }
23  
24 sub task_syntax {
25 return '';
26 }
27  
28 sub comment_lines_str { return '%' }
29  
30 # TODO complete this
31 my @bibtex_types = qw/
32 article book incollection inproceedings misc proceedings thesis
33 /;
34  
35 # TODO check for completeness
36 my @bibtex_fields = qw/
37 abstract address author booktitle crossref editor ee isbn issn journal keywords
38 location month pages publisher title volume year
39 /;
40  
41 sub autocomplete {
42 my $self = shift;
43 my $event = shift;
44  
45 my $config = Padre->ide->config;
46 my $min_chars = $config->lang_perl5_autocomplete_min_chars; # TODO rename this config option?
47  
48 my $editor = $self->editor;
49 my $pos = $editor->GetCurrentPos;
50 my $line = $editor->LineFromPosition($pos);
51 my $first = $editor->PositionFromLine($line);
52  
53 # This function is called very often, return asap
54 return if ( $pos - $first ) < ( $min_chars - 1 );
55  
56 # line from beginning to current position
57 my $prefix = $editor->GetTextRange( $first, $pos );
58  
59 # Remove any ident from the beginning of the prefix
60 $prefix =~ s/^[\r\t]+//;
61 return if length($prefix) == 0;
62  
63 # One char may be added by the current event
64 return if length($prefix) < ( $min_chars - 1 );
65  
66 # The second parameter may be a reference to the current event or the next
67 # char which will be added to the editor:
68 my $nextchar = ''; # Use empty instead of undef
69 if ( defined($event) and ( ref($event) eq 'Wx::KeyEvent' ) ) {
70 my $key = $event->GetUnicodeKey;
71 $nextchar = chr($key);
72 } elsif ( defined($event) and ( !ref($event) ) ) {
73 $nextchar = $event;
74 }
75 return if ord($nextchar) == 27; # Close on escape
76 $nextchar = '' if ord($nextchar) < 32;
77  
78 # TODO fields, crossref, author, year
79  
80 # check for BibTeX entry types
81 if ( $prefix =~ /@(\w*)$/ ) {
82 my $entry_prefix = $1;
83 return $self->find_completions( $entry_prefix, $nextchar, map { $_ . '{,' } @bibtex_types );
84 }
85  
86 # check for BibTeX fields
87 if ( $prefix =~ /(\w*)$/ ) {
88 my $field_prefix = $1;
89 return $self->find_completions( $field_prefix, $nextchar, @bibtex_fields );
90 }
91  
92 return;
93 }
94  
95 sub find_completions {
96 my $self = shift;
97 my $prefix = shift;
98 my $nextchar = shift;
99 my @candidates = @_;
100  
101 my $editor = $self->editor;
102 my $pos = $editor->GetCurrentPos;
103 my $line = $editor->LineFromPosition($pos);
104 my $first = $editor->PositionFromLine($line);
105  
106 my $last = $editor->GetLength();
107 my $pre_text = $editor->GetTextRange( 0, $first + length($prefix) );
108 my $post_text = $editor->GetTextRange( $first, $last );
109  
110 my $regex;
111 eval { $regex = qr{\b(\Q$prefix\E\w*)\b} };
112 if ($@) {
113 warn "Cannot build regex for '$prefix'\n";
114 return;
115 }
116  
117 my %seen;
118 my @words;
119 push @words, grep { $_ =~ $regex and !$seen{$_}++ } @candidates;
120 push @words, grep { !$seen{$_}++ } reverse( $pre_text =~ /$regex/g );
121 push @words, grep { !$seen{$_}++ } ( $post_text =~ /$regex/g );
122  
123 # TODO is 20 a good limit?
124 # TODO configurable?
125 if ( scalar @words > 20 ) {
126 @words = @words[ 0 .. 19 ];
127 }
128  
129 # Suggesting the current word as the only solution doesn't help
130 # anything, but your need to close the suggestions window before
131 # you may press ENTER/RETURN.
132 if ( ( $#words == 0 ) and ( $prefix eq $words[0] ) ) {
133 return;
134 }
135  
136 my $suffix = $editor->GetTextRange( $pos, $pos + 15 );
137 $suffix = $1 if $suffix =~ /^(\w*)/; # Cut away any non-word chars
138  
139 # While typing within a word, the rest of the word shouldn't be
140 # inserted.
141 if ( defined($suffix) ) {
142 for ( 0 .. $#words ) {
143 $words[$_] =~ s/\Q$suffix\E$//;
144 }
145 } # TODO check this
146  
147 # This is the final result if there is no char which hasn't been
148 # saved to the editor buffer until now
149 return ( length($prefix), @words ) if !defined($nextchar);
150  
151 # Finally cut out all words which do not match the next char
152 # which will be inserted into the editor (by the current event)
153 my @final_words;
154 for (@words) {
155  
156 # Accept everything which has prefix + next char + at least one other char
157 next if !/^\Q$prefix$nextchar\E./;
158 push @final_words, $_;
159 }
160  
161 return ( length($prefix), @final_words );
162 }
163  
164  
165 1;
166  
167 __END__