Codit/Plugins/Snippets.pm untested

filter: Statementstmt 0.0% 0 / 184 Branchbran 0.0% 0 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 296 CC 30 SCAR 22.9
CC
30
Cov
0%
listRefresh
20.0
snippetCreate
20.0
snippetLoad
20.0
CRAP 9.9
1 package App::Codit::Plugins::Snippets;
2  
3 =head1 NAME
4  
5 App::Codit::Plugins::Snippets - 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 Carp;
15  
16 use base qw( Tk::AppWindow::BaseClasses::Plugin );
17  
18 require Tk::HList;
19 require Tk::XText;
20 use File::Path qw(make_path);
21  
22 =head1 DESCRIPTION
23  
24 Quick and easy code samples.
25  
26 =head1 DETAILS
27  
28 Snippets are shorts pieces of code that you find yourself writing over and over again.
29 The top side of the panel allows you to manage your collection of snippets.
30 The bottom side allows you to insert a selected snippet into your document,
31 copy the snippet to the clipboard or create a new document based on the snippet.
32 A file dialog is launched and the snippet is saved to the selected file name.
33 Then it is opened in a new tab.
34  
35 =cut
36  
37 sub new {
38 my $class = shift;
39 my $self = $class->SUPER::new(@_);
40 return undef unless defined $self;
41
42 $self->{CURRENT} = undef;
43 # my $tp = $self->extGet('ToolPanel');
44 # my $page = $tp->addPage('Snippets', 'insert-text', undef, 'Snippets');
45 my $page = $self->ToolRightPageAdd('Snippets', 'insert-text', undef, 'Snippets', 350);
46  
47 my @padding = (-padx => 2, -pady => 2);
48  
49 my $lf = $page->Frame(
50 -relief => 'groove',
51 -borderwidth => 2,
52 )->pack(@padding, -fill => 'x');
53
54 $lf->Button(
55 -text => 'New',
56 -command => ['snippetNew', $self],
57 )->pack(@padding, -fill => 'x');
58 $lf->Button(
59 -text => 'Copy to',
60 -command => ['snippetCopy', $self],
61 )->pack(@padding, -fill=> 'x');
62 $lf->Button(
63 -text => 'Delete',
64 -command => ['snippetDelete', $self],
65 )->pack(@padding, -fill => 'x');
66 my $hlist = $lf->Scrolled('HList',
67 -browsecmd => ['listSelect', $self],
68 -height => 4,
69 -scrollbars => 'osoe',
70 )->pack(@padding, -expand => 1, -fill => 'both');
71 $self->{LIST} = $hlist;
72  
73 $page->Adjuster(
74 -side => 'top',
75 -widget => $lf,
76 )->pack(-fill => 'x');
77
78 my $sf = $page->Frame(
79 -relief => 'groove',
80 -borderwidth => 2,
81 )->pack(@padding, -expand => 1, -fill => 'both');
82 $sf->Button(
83 -text => 'Insert',
84 -command => ['snippetInsert', $self],
85 )->pack(@padding, -fill => 'x');
86 $sf->Button(
87 -text => 'Clipboard',
88 -command => ['snippetClipboard', $self],
89 )->pack(@padding, -fill => 'x');
90 $sf->Button(
91 -text => 'Create',
92 -command => ['snippetCreate', $self],
93 )->pack(@padding, -fill => 'x');
94 my @to = ();
95  
96 my $text = $sf->Scrolled('XText', @to,
97 -scrollbars => 'osoe',
98 -tabs => '8m',
99 -wrap => 'none',
100 -height => 4,
101 -width => 20,
102 )->pack(@padding, -expand => 1, -fill => 'both');
103 $self->{TEXT} = $text;
104  
105 $self->ReConfigure;
106 $self->listRefresh;
107 return $self;
108 }
109  
110 sub _list {
111 return $_[0]->{LIST}
112 }
113  
114 sub _text {
115 return $_[0]->{TEXT}
116 }
117  
118 sub current {
119 my $self = shift;
120 $self->{CURRENT} = shift if @_;
121 return $self->{CURRENT}
122 }
123  
124 sub itemName {
125 my ($self, $item) = @_;
126 $item =~ s/\./`/g;
127 return $item
128 }
129  
130 sub itemText {
131 my ($self, $item) = @_;
132 return $self->_list->entrycget($item, '-text');
133 }
134  
135 sub listSelect {
136 my ($self, $item) = @_;
137 croak "Item not defined" unless defined $item;
138 my $cur = $self->current;
139 $self->snippetSave if defined $cur;
140 $self->current($item);
141 $self->snippetLoad;
142 }
143  
144 sub listRefresh {
145 my $self = shift;
146 my $folder = $self->snippetsFolder;
147 my $dh;
148 unless (opendir($dh, $folder)) {
149 croak "cannot open folder $folder";
150 return
151 }
152 $self->snippetSave;
153 my $l = $self->_list;
154 $l->deleteAll;
155 $self->current(undef);
156 $self->_text->clear;
157 while (my $i = readdir($dh)) {
158 next if $i eq '.';
159 next if $i eq '..';
160 $self->snippetAdd($i);
161 }
162 closedir($dh)
163 }
164  
165 sub Quit {
166 my $self = shift;
167 $self->snippetSave;
168 }
169  
170 sub ReConfigure {
171 my $self = shift;
172 my $txt = $self->{TEXT};
173 my $fam = $self->configGet('-contentfontfamily');
174 $fam = 'Courier' unless defined $fam;
175 my $siz = $self->configGet('-contentfontsize');
176 $siz = 10 unless defined $siz;
177 $txt->configure(-font => "{$fam} $siz");
178 }
179  
180 sub snippetAdd {
181 my ($self, $item) = @_;
182 croak "Item not defined" unless defined $item;
183 my $l = $self->_list;
184 my @op = ();
185 my $text = $item;
186 $item = $self->itemName($item);
187 my @peers = $l->infoChildren('');
188 for (@peers) {
189 if ($item lt $_) {
190 @op = (-before, $_);
191 last;
192 }
193 }
194 $l->add($item, -text => $text, @op);
195 }
196  
197 sub snippetClipboard {
198 my $self = shift;
199 my $cur = $self->current;
200 return unless defined $cur;
201 my $text = $self->snippetGet;
202 $self->clipboardClear;
203 $self->clipboardAppend($text);
204 }
205  
206 sub snippetCopy {
207 my $self = shift;
208 my $sel = $self->current;
209 return unless defined $sel;
210 my $text = $self->snippetGet;
211 my $new = $self->snippetDialog;
212 if (defined $new) {
213 $self->snippetAdd($new);
214 $self->listSelect($self->itemName($new));
215 $self->_text->insert('end', $text);
216 }
217 }
218  
219 sub snippetCreate {
220 my $self = shift;
221 my $list = $self->_list;
222 my ($sel) = $list->infoSelection;
223 return unless defined $sel;
224 my ($file) = $self->pickFileSave;
225 if (defined $file) {
226 my $dh;
227 unless (open($dh, ">", $file)) {
228 croak "cannot open $file";
229 return
230 }
231 print $dh $self->snippetGet;
232 close $dh;
233 $self->cmdExecute('doc_open', $file);
234 }
235 }
236  
237 sub snippetDelete {
238 my $self = shift;
239 my $list = $self->_list;
240 my ($sel) = $list->infoSelection;
241 return unless defined $sel;
242 $sel = $self->itemText($sel);
243 my $file = $self->snippetsFolder . "/$sel";
244 my $button = $self->popDialog(
245 'Deleting snippet',
246 "Deleting snippet '$sel'\nAre you sure?",
247 'dialog-warning',
248 'Ok', 'Cancel'
249 );
250 if ($button eq 'Ok') {
251 $self->current(undef);
252 unlink $file;
253 $self->listRefresh;
254 }
255 }
256  
257 sub snippetDialog {
258 my ($self, $value) = @_;
259 $value = '' unless defined $value;
260 return $self->popEntry('New snippet', 'Please enter a snippet name', $value, 'dialog-information')
261 }
262  
263 sub snippetGet {
264 my $self = shift;
265 my $text = $self->_text;
266 return $text->get('1.0', 'end -1c')
267 }
268  
269 sub snippetInsert {
270 my $self = shift;
271 my $cur = $self->current;
272 return unless defined $cur;
273 $self->cmdExecute('edit_insert', 'insert', $self->snippetGet);
274 }
275  
276 sub snippetLoad {
277 my ($self, $item) = @_;
278 $item = $self->current unless defined $item;
279 return unless defined $item;
280 $item = $self->itemText($item);
281 my $txt = $self->_text;
282 $txt->clear;
283 my $file = $self->snippetsFolder . "/$item";
284 $txt->load($file) if -e $file;
285 }
286  
287 sub snippetNew {
288 my $self = shift;
289 my $item = $self->snippetDialog;
290 return if $self->_list->infoExists($item);
291 $self->snippetAdd($item) if defined $item;
292 }
293  
294 sub snippetSave {
295 my ($self, $item) = @_;
296 $item = $self->current unless defined $item;
297 return unless defined $item;
298 $item = $self->itemText($item);
299 my $txt = $self->_text;
300 $txt->save($self->snippetsFolder . "/$item")
301 }
302  
303 sub snippetsFolder {
304 my $self = shift;
305 my $config = $self->extGet('ConfigFolder')->ConfigFolder . '/Snippets';
306 make_path($config) unless -e $config;
307 return $config
308 }
309  
310 sub Unload {
311 my $self = shift;
312 $self->snippetSave;
313 $self->ToolRightPageRemove('Snippets');
314 # $self->extGet('ToolPanel')->deletePage('Snippets');
315 return $self->SUPER::Unload
316 }
317  
318 =head1 LICENSE
319  
320 Same as Perl.
321  
322 =head1 AUTHOR
323  
324 Hans Jeuken (hanje at cpan dot org)
325  
326 =head1 TODO
327  
328 =over 4
329  
330 =back
331  
332 =head1 BUGS AND CAVEATS
333  
334 If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>.
335  
336 =head1 SEE ALSO
337  
338 =over 4
339  
340 =back
341  
342 =cut
343  
344 1;
345  
346  
347  
348  
349  
350