| 1 | package App::Codit::Plugins::Critic; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Codit::Plugins::Critic - plugin for App::Codit | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw( $VERSION ); | ||
| 12 | $VERSION = '0.22'; | ||
| 13 | |||
| 14 | use base qw( Tk::AppWindow::BaseClasses::Plugin ); | ||
| 15 | |||
| 16 | require Perl::Critic; | ||
| 17 | require Tk::ROText; | ||
| 18 | use Tk; | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | Check your code for best practices. | ||
| 23 | |||
| 24 | =head1 DETAILS | ||
| 25 | |||
| 26 | This plugin uses L<Perl::Critic> to check your document for best practices. | ||
| 27 | You can select your favorite severity level. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | my $class = shift; | ||
| 33 | my $self = $class->SUPER::new(@_); | ||
| 34 | return undef unless defined $self; | ||
| 35 | |||
| 36 | my $page = $self->ToolRightPageAdd('Critic', 'code-class', undef, 'Check your code for best practices'); | ||
| 37 | |||
| 38 | |||
| 39 | my @padding = (-padx => 2, -pady => 2); | ||
| 40 | |||
| 41 | my $sm = $page->Frame( | ||
| 42 | -relief => 'groove', | ||
| 43 | -borderwidth => 2, | ||
| 44 | )->pack(-padx => 2, -fill => 'x'); | ||
| 45 | my $mf = $sm->Frame->pack(-expand => 1, -fill =>'x'); | ||
| 46 | $mf->Label( | ||
| 47 | -anchor =>'e', | ||
| 48 | -width => 10, | ||
| 49 | -text => 'Severity:', | ||
| 50 | )->pack(-side => 'left'); | ||
| 51 | my $sev = 'harsh'; | ||
| 52 | $self->{SEVERITY} = \$sev; | ||
| 53 | my $mb = $mf->Menubutton( | ||
| 54 | -anchor => 'w', | ||
| 55 | -textvariable => \$sev, | ||
| 56 | )->pack(@padding, -side => 'left', -expand => 1, -fill => 'x'); | ||
| 57 | my @menu = (); | ||
| 58 | for ('gentle', 'stern', 'harsh', 'cruel', 'brutal') { | ||
| 59 | my $operation = $_; | ||
| 60 | push @menu, [command => $operation, | ||
| 61 | -command => sub { $sev = $operation }, | ||
| 62 | ]; | ||
| 63 | } | ||
| 64 | $mb->configure(-menu => $mb->Menu( | ||
| 65 | -menuitems => \@menu, | ||
| 66 | )); | ||
| 67 | |||
| 68 | my $sa = $page->Frame( | ||
| 69 | -relief => 'groove', | ||
| 70 | -borderwidth => 2, | ||
| 71 | )->pack(@padding, -expand => 1, -fill => 'both'); | ||
| 72 | $sa->Button( | ||
| 73 | -text => 'Critique', | ||
| 74 | -command => ['Critique', $self], | ||
| 75 | )->pack(@padding, -fill => 'x'); | ||
| 76 | $sa->Button( | ||
| 77 | -text => 'Clear', | ||
| 78 | -command => ['Clear', $self], | ||
| 79 | )->pack(@padding, -fill => 'x'); | ||
| 80 | |||
| 81 | my $txt = $sa->Scrolled('ROText', | ||
| 82 | -scrollbars => 'oe', | ||
| 83 | -width => 80, | ||
| 84 | -wrap => 'word', | ||
| 85 | )->pack(@padding, -expand => 1, -fill => 'both'); | ||
| 86 | $self->{TXT} = $txt; | ||
| 87 | |||
| 88 | $txt->tagConfigure('link', -foreground => $self->configGet('-linkcolor')); | ||
| 89 | $txt->tagBind('link', '<Enter>', sub { $txt->configure('-cursor', 'hand1')}); | ||
| 90 | $txt->tagBind('link', '<Leave>', sub { $txt->configure('-cursor', 'xterm')}); | ||
| 91 | $txt->tagBind('link', '<ButtonRelease-1>', [$self, 'linkClick', Ev('x'), Ev('y')]); | ||
| 92 | |||
| 93 | $self->ReConfigure; | ||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub Clear { | ||
| 98 | my $self = shift; | ||
| 99 | my $txt = $self->{TXT}; | ||
| 100 | $txt->delete('0.0', 'end'); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub Critique { | ||
| 104 | my $self = shift; | ||
| 105 | my $txt = $self->{TXT}; | ||
| 106 | $txt->delete('0.0', 'end'); | ||
| 107 | my $widg = $self->mdi->docWidget; | ||
| 108 | my $source = $widg->get('0.0', 'end - 1 char'); | ||
| 109 | my $sev = $self->{SEVERITY}; | ||
| 110 | my $cr = new Perl::Critic(-severity => $$sev); | ||
| 111 | my @violations = $cr->critique(\$source); | ||
| 112 | for (@violations) { | ||
| 113 | my $line = $_; | ||
| 114 | while ($line ne '') { | ||
| 115 | if ($line =~ s/^(line\s\d+,\scolumn\s\d+)//) { | ||
| 116 | $txt->insert('end', $1, 'link'); | ||
| 117 | } else { | ||
| 118 | my $char = substr($line, 0, 1); | ||
| 119 | $line = substr($line, 1, length($line) - 1); | ||
| 120 | if ($char eq "\n") { | ||
| 121 | $txt->insert('end', "\n\n"); | ||
| 122 | } else { | ||
| 123 | $txt->insert('end', $char); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | sub linkClick { | ||
| 131 | my ($self, $x, $y) = @_; | ||
| 132 | my $link; | ||
| 133 | my $txt = $self->{TXT}; | ||
| 134 | |||
| 135 | #find the link | ||
| 136 | my $pos = $txt->index('@' ."$x,$y"); | ||
| 137 | my @ranges = $txt->tagRanges('link'); | ||
| 138 | while (@ranges) { | ||
| 139 | my $begin = shift @ranges; | ||
| 140 | my $end = shift @ranges; | ||
| 141 | if (($txt->compare($begin, '<=', $pos)) and ($txt->compare($end, '>=', $pos))) { | ||
| 142 | $link = $txt->get($begin, $end); | ||
| 143 | last | ||
| 144 | } | ||
| 145 | } | ||
| 146 | if ($link =~ /^line\s(\d+),\scolumn\s(\d+)/) { | ||
| 147 | my $widg = $self->mdi->docWidget; | ||
| 148 | my $goto = "$1.$2"; | ||
| 149 | $widg->goTo($goto); | ||
| 150 | $widg->focus; | ||
| 151 | } | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | sub ReConfigure { | ||
| 156 | my $self = shift; | ||
| 157 | my $txt = $self->{TXT}; | ||
| 158 | my $fam = $self->configGet('-contentfontfamily'); | ||
| 159 | $fam = 'Courier' unless defined $fam; | ||
| 160 | my $siz = $self->configGet('-contentfontsize'); | ||
| 161 | $siz = 10 unless defined $siz; | ||
| 162 | $txt->configure(-font => "{$fam} $siz"); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub Unload { | ||
| 166 | my $self = shift; | ||
| 167 | $self->ToolRightPageRemove('Critic'); | ||
| 168 | return $self->SUPER::Unload | ||
| 169 | } | ||
| 170 | |||
| 171 | =head1 LICENSE | ||
| 172 | |||
| 173 | Same as Perl. | ||
| 174 | |||
| 175 | =head1 AUTHOR | ||
| 176 | |||
| 177 | Hans Jeuken (hanje at cpan dot org) | ||
| 178 | |||
| 179 | =head1 TODO | ||
| 180 | |||
| 181 | =over 4 | ||
| 182 | |||
| 183 | =back | ||
| 184 | |||
| 185 | =head1 BUGS AND CAVEATS | ||
| 186 | |||
| 187 | If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>. | ||
| 188 | |||
| 189 | =head1 SEE ALSO | ||
| 190 | |||
| 191 | =over 4 | ||
| 192 | |||
| 193 | =item L<Perl::Critic> | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | |||
| 200 | 1; | ||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 |