File Coverage

lib/Devel/Trepan/CmdProcessor/Command/Edit.pm
Criterion Covered Total %
statement 72 136 52.9
branch 0 36 0.0
condition 0 4 0.0
subroutine 24 28 85.7
pod 0 4 0.0
total 96 208 46.1


line stmt bran cond sub pod time code
1             # Copyright (C) 2011-2012 Rocky Bernstein <rocky@cpan.org>
2 12     12   95 use warnings; no warnings 'redefine';
  12     12   37  
  12     1   609  
  12     1   64  
  12         98  
  12         381  
  1         7  
  1         2  
  1         22  
  1         5  
  1         2  
  1         25  
3 12     12   62 use rlib '../../../..';
  12     1   25  
  12         125  
  1         5  
  1         1  
  1         12  
4              
5 12     12   4623 use Devel::Trepan::DB::LineCache;
  12     1   26  
  12         2172  
  1         325  
  1         2  
  1         119  
6 12     12   76 use Devel::Trepan::DB::Sub;
  12     1   25  
  12         811  
  1         5  
  1         2  
  1         43  
7              
8             package Devel::Trepan::CmdProcessor::Command::Edit;
9 12     12   67 use if !@ISA, Devel::Trepan::CmdProcessor::Command ;
  12     1   24  
  12         66  
  1         5  
  1         2  
  1         29  
10             unless (@ISA) {
11 12     12   69 eval <<"EOE";
  12     12   37  
  12     12   611  
  12     12   69  
  12     12   33  
  12     12   525  
  12         72  
  12         30  
  12         546  
  12         77  
  12         33  
  12         605  
  12         72  
  12         236  
  12         564  
  12         70  
  12         24  
  12         444  
12             use constant ALIASES => ('e');
13             use constant CATEGORY => 'files';
14             use constant SHORT_HELP => 'Invoke an editor on some source code';
15             use constant NEED_STACK => 0;
16             use constant MIN_ARGS => 0; # Need at least this many
17             use constant MAX_ARGS => 2; # Need at most this many - undef -> unlimited.
18             EOE
19             }
20              
21 12     12   1889 use strict;
  12     1   22  
  12         568  
  1         84  
  1         3  
  1         33  
22             our @ISA = @CMD_ISA; # value inherited from parent
23 12     12   77 use vars @CMD_VARS; # value inherited from parent
  12     1   24  
  12         5065  
  1         4  
  1         2  
  1         352  
24              
25             our $NAME = set_name();
26             =pod
27              
28             =head2 Synopsis:
29              
30             =cut
31             our $HELP = <<"HELP";
32             =pod
33              
34             B<edit> [[I<file>] [I<line>]]
35              
36             With no argument, edits file containing most recent line listed.
37             The value of the environment variable I<EDITOR> is used for the
38             editor to run. If no I<EDITOR> environment variable is set /bin/ex
39             is used. The editor should support line and file positioning via
40              
41             editor-name +line file-name
42              
43             (Most editors do.)
44              
45             =head2 Examples:
46              
47             edit # Edit current location
48             edit 7 # Edit current file at line 7
49             edit test.rb # Edit test.rb, line 1
50             edit test.rb 10 # Edit test.rb line 10
51             =cut
52             HELP
53              
54             # FIXME: include line numbers. Should we include all files?
55             # Combine with BREAK completion.
56             sub complete($$)
57             {
58 0     0 0   my ($self, $prefix) = @_;
  0     0 0    
59 0           my @completions = sort ('.', DB::LineCache::file_list());
  0            
60 0           Devel::Trepan::Complete::complete_token(\@completions, $prefix);
  0            
61             }
62              
63             # This method runs the command
64             sub run($$)
65             {
66 0     0 0   my ($self, $args) = @_;
  0     0 0    
67 0           my $proc = $self->{proc};
  0            
68 0           my ($filename, $line_number);
  0            
69 0           my $count = scalar @$args;
  0            
70 0 0         if (1 == $count) {
  0 0          
    0          
    0          
    0          
    0          
71 0 0         if ($proc->{terminated}) {
  0 0          
72 0           $proc->msg_need_running("implicit edit file and line");
  0            
73 0           return;
  0            
74             }
75 0           $filename = $self->{proc}->filename;
  0            
76 0           $line_number = $self->{proc}->line;
  0            
77             } elsif (2 == $count) {
78 0           $line_number = $self->{proc}->get_int_noerr($args->[1]);
  0            
79 0 0         if (defined $line_number) {
  0 0          
80 0 0         if ($proc->{terminated}) {
  0 0          
81 0           $proc->msg_need_running("implicit edit file");
  0            
82 0           return;
  0            
83             }
84 0           $filename = $self->{proc}->filename;
  0            
85             } else {
86 0           $filename = $args->[1];
  0            
87 0           $line_number = 1;
  0            
88             }
89             } elsif (3 == $count) {
90 0           ($line_number, $filename) = ($args->[2], $args->[1]);
  0            
91             } else {
92 0           $self->errmsg("edit needs at most 2 args.");
  0            
93 0           return;
  0            
94             }
95 0   0       my $editor = $ENV{'EDITOR'} || '/bin/ex';
  0   0        
96 0 0         if ( -r $filename ) {
  0 0          
97 12     12   226 use File::Basename;
  12     1   28  
  12         4340  
  1         7  
  1         2  
  1         316  
98 0 0         $filename = basename($filename) if $self->{proc}{settings}{basename};
  0 0          
99 0           my @edit_cmd = ($editor, "+$line_number", $filename);
  0            
100 0           $self->{proc}->msg(sprintf "Running: %s...", join(' ', @edit_cmd));
  0            
101 0           system(@edit_cmd);
  0            
102 0 0         $self->{proc}->msg("Warning: return code was $?") if $? != 0;
  0 0          
103             } else {
104 0           $self->errmsg("File \"${filename}\" is not readable.");
  0            
105             }
106             }
107              
108             unless (caller) {
109             require Devel::Trepan::CmdProcessor::Mock;
110             my $proc = Devel::Trepan::CmdProcessor->new(undef, 'bogus');
111             my $cmd = __PACKAGE__->new($proc);
112             my $frame_ary = Devel::Trepan::CmdProcessor::Mock::create_frame();
113             $proc->frame_setup($frame_ary);
114              
115             $cmd->run([$NAME]);
116             $cmd->run([$NAME, '7']);
117             $cmd->run([$NAME, __FILE__, '10']);
118             }
119              
120             1;