| 1 | package App::Codit::Plugins::PerlSubs; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Codit::Plugins::PerlSubs - plugin for App::Codit | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw( $VERSION ); | ||
| 12 | $VERSION = '0.20'; | ||
| 13 | use base qw( App::Codit::BaseClasses::TextModPlugin ); | ||
| 14 | |||
| 15 | use Data::Compare; | ||
| 16 | use Tk; | ||
| 17 | require Tk::ListBrowser; | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Easily find the subs in your document. | ||
| 22 | |||
| 23 | =head1 DETAILS | ||
| 24 | |||
| 25 | PerlSubs scans the current selected document for lines that begin | ||
| 26 | with 'sub someName' and displays it in a list with the line number. | ||
| 27 | The list is refreshed after an edit. | ||
| 28 | |||
| 29 | When you click on and item in the list, the insert cursor is moved to that | ||
| 30 | line and it is scrolled into visibility. | ||
| 31 | |||
| 32 | Both colums are sizable and sortable. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my $class = shift; | ||
| 38 | my $self = $class->SUPER::new(@_); | ||
| 39 | return undef unless defined $self; | ||
| 40 | |||
| 41 | my $page = $self->ToolLeftPageAdd('PerlSubs', 'code-context', undef, 'Find your Perl subs', 250); | ||
| 42 | |||
| 43 | $self->{CURRENT} = []; | ||
| 44 | $self->{SORTON} = 'Line'; | ||
| 45 | $self->{SORTORDER} = 'ascending'; | ||
| 46 | |||
| 47 | my $list = $page->ListBrowser( | ||
| 48 | -arrange => 'list', | ||
| 49 | # -autorefresh => 1, | ||
| 50 | -browsecmd => ['Select', $self], | ||
| 51 | -filterforce => 1, | ||
| 52 | -itemtype => 'text', | ||
| 53 | -selectmode => 'single', | ||
| 54 | -textanchor => 'w', | ||
| 55 | -textside => 'right', | ||
| 56 | )->pack(-expand => 1, -fill => 'both'); | ||
| 57 | $self->{LIST} = $list; | ||
| 58 | $list->forceWidth(100); | ||
| 59 | $list->headerCreate('', | ||
| 60 | -text => 'Sub', | ||
| 61 | -sortable => 1, | ||
| 62 | ); | ||
| 63 | my $col = $list->columnCreate('Line', | ||
| 64 | -sortnumerical => 1, | ||
| 65 | ); | ||
| 66 | $col->forceWidth(50); | ||
| 67 | $list->headerCreate('Line', | ||
| 68 | -text => 'Line', | ||
| 69 | -sortable => 1, | ||
| 70 | ); | ||
| 71 | $list->headerPlace; | ||
| 72 | |||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub Clear { | ||
| 77 | my $self = shift; | ||
| 78 | my $hlist = $self->{LIST}; | ||
| 79 | $hlist->deleteAll; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub Current { | ||
| 83 | my $self = shift; | ||
| 84 | $self->{CURRENT} = shift if @_; | ||
| 85 | return $self->{CURRENT} | ||
| 86 | } | ||
| 87 | |||
| 88 | sub FillList { | ||
| 89 | my ($self, $new, $select) = @_; | ||
| 90 | $select = 1 unless defined $select; | ||
| 91 | my $hlist = $self->{LIST}; | ||
| 92 | my $cursel; | ||
| 93 | ( $cursel ) = $hlist->infoSelection; | ||
| 94 | # my $lastvisible = $hlist->nearest($hlist->height - 1); | ||
| 95 | $self->Clear; | ||
| 96 | |||
| 97 | for (@$new) { | ||
| 98 | my ($name, $num) = @$_; | ||
| 99 | my $item = $name; | ||
| 100 | my $count = 2; | ||
| 101 | while ($hlist->infoExists($item)) { | ||
| 102 | $item = "$name$count"; | ||
| 103 | $count ++ | ||
| 104 | } | ||
| 105 | $hlist->add($item, -data => $num, -text => $name); | ||
| 106 | $hlist->itemCreate($item, 'Line', -text => $num); | ||
| 107 | } | ||
| 108 | |||
| 109 | #find and set selection | ||
| 110 | if ($select) { | ||
| 111 | $hlist->selectionSet($cursel) if (defined $cursel) and $hlist->infoExists($cursel); | ||
| 112 | # $hlist->see($lastvisible) if (defined $lastvisible) and $hlist->infoExists($lastvisible); | ||
| 113 | } | ||
| 114 | $self->Current($new); | ||
| 115 | $hlist->clear; | ||
| 116 | my $c = $hlist->Subwidget('Canvas'); | ||
| 117 | my ($view) = $c->yview; | ||
| 118 | $hlist->refresh; | ||
| 119 | $c->yview('moveto', $view); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub Refresh { | ||
| 123 | my ($self, $select) = @_; | ||
| 124 | $self->SUPER::Refresh; | ||
| 125 | $select = 1 unless defined $select; | ||
| 126 | my $current = $self->Current; | ||
| 127 | my @new = (); | ||
| 128 | |||
| 129 | my $mdi = $self->extGet('CoditMDI'); | ||
| 130 | my $doc = $self->mdi->docWidget; | ||
| 131 | return unless defined $doc; | ||
| 132 | my $end = $doc->index('end - 1c'); | ||
| 133 | my $numlines = $end; | ||
| 134 | $numlines =~ s/\.\d+$//; | ||
| 135 | for (1 .. $numlines) { | ||
| 136 | my $num = $_; | ||
| 137 | my $line = $doc->get("$num.0", "$num.0 lineend"); | ||
| 138 | if ($line =~ /^\s*sub\s+([^\s|^\{]+)/) { | ||
| 139 | my $name = $1; | ||
| 140 | push @new, [$name, $num]; | ||
| 141 | } | ||
| 142 | @new = $self->Sort(@new); | ||
| 143 | } | ||
| 144 | |||
| 145 | unless (Compare($current, \@new)) { | ||
| 146 | $self->FillList(\@new, $select); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | sub Select { | ||
| 151 | my ($self, $name) = @_; | ||
| 152 | my $hlist = $self->{LIST}; | ||
| 153 | my $doc = $self->mdi->docWidget; | ||
| 154 | if (defined $doc) { | ||
| 155 | my $line = $hlist->infoData($name); | ||
| 156 | my $index = "$line.0"; | ||
| 157 | $doc->goTo($index); | ||
| 158 | $doc->focus; | ||
| 159 | # $doc->see($index); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub Sort { | ||
| 164 | my ($self, @list) = @_; | ||
| 165 | my $on = $self->SortOn; | ||
| 166 | my $order = $self->SortOrder; | ||
| 167 | my @new; | ||
| 168 | if ($on eq 'Sub') { | ||
| 169 | if ($order eq 'ascending') { | ||
| 170 | @new = sort {lc($a->[0]) cmp lc($b->[0])} @list | ||
| 171 | } elsif ($order eq 'descending') { | ||
| 172 | @new = reverse sort {lc($a->[0]) cmp lc($b->[0])} @list | ||
| 173 | } | ||
| 174 | } elsif ($on eq 'Line') { | ||
| 175 | if ($order eq 'ascending') { | ||
| 176 | @new = sort {$a->[1] <=> $b->[1]} @list | ||
| 177 | } elsif ($order eq 'descending') { | ||
| 178 | @new = reverse sort {$a->[1] <=> $b->[1]} @list | ||
| 179 | } | ||
| 180 | } | ||
| 181 | return @new | ||
| 182 | } | ||
| 183 | |||
| 184 | sub Sortcall { | ||
| 185 | my ($self, $on, $order) = @_; | ||
| 186 | $self->SortOn($on); | ||
| 187 | $self->SortOrder($order); | ||
| 188 | my $hlist = $self->{LIST}; | ||
| 189 | |||
| 190 | my $col; | ||
| 191 | $col = 0 if $on eq 'Sub'; | ||
| 192 | $col = 1 if $on eq 'Line'; | ||
| 193 | my $h1 = $hlist->headerCget($col, '-widget'); | ||
| 194 | $h1->configure(-sortorder => $order); | ||
| 195 | my $ncol = not $col; | ||
| 196 | my $h2 = $hlist->headerCget($ncol, '-widget'); | ||
| 197 | $h2->configure(-sortorder => 'none'); | ||
| 198 | |||
| 199 | my $cur = $self->Current; | ||
| 200 | my @new = $self->Sort(@$cur); | ||
| 201 | $self->FillList(\@new); | ||
| 202 | $self->Current(\@new); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub SortOn { | ||
| 206 | my $self = shift; | ||
| 207 | $self->{SORTON} = shift if @_; | ||
| 208 | return $self->{SORTON} | ||
| 209 | } | ||
| 210 | |||
| 211 | sub SortOrder { | ||
| 212 | my $self = shift; | ||
| 213 | $self->{SORTORDER} = shift if @_; | ||
| 214 | return $self->{SORTORDER} | ||
| 215 | } | ||
| 216 | |||
| 217 | sub Unload { | ||
| 218 | my $self = shift; | ||
| 219 | $self->ToolLeftPageRemove('PerlSubs'); | ||
| 220 | return $self->SUPER::Unload | ||
| 221 | } | ||
| 222 | |||
| 223 | =head1 LICENSE | ||
| 224 | |||
| 225 | Same as Perl. | ||
| 226 | |||
| 227 | =head1 AUTHOR | ||
| 228 | |||
| 229 | Hans Jeuken (hanje at cpan dot org) | ||
| 230 | |||
| 231 | =head1 TODO | ||
| 232 | |||
| 233 | =over 4 | ||
| 234 | |||
| 235 | =back | ||
| 236 | |||
| 237 | =head1 BUGS AND CAVEATS | ||
| 238 | |||
| 239 | If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>. | ||
| 240 | |||
| 241 | =head1 SEE ALSO | ||
| 242 | |||
| 243 | =over 4 | ||
| 244 | |||
| 245 | =back | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | |||
| 250 | 1; |