File: StrictWarnings.pm

package info (click to toggle)
padre 1.00%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,720 kB
  • ctags: 3,775
  • sloc: perl: 61,147; ansic: 135; makefile: 41; sh: 37; sql: 36; cobol: 16; ruby: 6; java: 5; ada: 5; pascal: 3; tcl: 3; haskell: 2; php: 1; python: 1
file content (121 lines) | stat: -rw-r--r-- 2,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package Padre::Document::Perl::QuickFix::StrictWarnings;

use 5.008;
use strict;
use warnings;

our $VERSION = '1.00';

#
# Constructor.
# No need to override this
#
sub new {
	my ($class) = @_;

	# Create myself :)
	my $self = bless {}, $class;

	return $self;
}

#
# Returns the quick fix list
#
sub apply {
	my ( $self, $doc, $document ) = @_;

	my @items = ();

	my $editor          = $document->editor;
	my $text            = $editor->GetText;
	my $current_line_no = $editor->GetCurrentLine;

	my ( $use_strict_include, $use_warnings_include );
	my $includes = $doc->find('PPI::Statement::Include');
	if ($includes) {
		foreach my $include ( @{$includes} ) {
			next if $include->type eq 'no';
			if ( $include->pragma ) {
				my $pragma = $include->pragma;
				if ( $pragma eq 'strict' ) {
					$use_strict_include = $include;
				} elsif ( $pragma eq 'warnings' ) {
					$use_warnings_include = $include;
				}
			}
		}
	}

	my ( $replace, $col, $row, $len );
	if ( $use_strict_include and not $use_warnings_include ) {

		# insert 'use warnings;' afterwards
		$replace = "use strict;\nuse warnings;";
		$row     = $use_strict_include->line_number - 1;
		$col     = $use_strict_include->column_number - 1;
		$len     = length $use_strict_include->content;
	} elsif ( not $use_strict_include and $use_warnings_include ) {

		# insert 'use strict';' before
		$replace = "use strict;\nuse warnings;";
		$row     = $use_warnings_include->line_number - 1;
		$col     = $use_warnings_include->column_number - 1;
		$len     = length $use_warnings_include->content;
	} elsif ( not $use_strict_include and not $use_warnings_include ) {

		# insert 'use strict; use warnings;' at the top
		my $first = $doc->find_first(
			sub {
				return ( $_[1]->isa('PPI::Statement')
					or $_[1]->isa('PPI::Structure') );
			}
		);
		$replace = "use strict;\nuse warnings;\n";
		if ($first) {
			$row = $first->line_number - 1;
			$col = $first->column_number - 1;
			$len = 0;

		} else {
			$row = $current_line_no;
			$col = 0;
			$len = 0;
		}
	}

	if ($replace) {
		push @items, {
			text     => qq{Fix '$replace'},
			listener => sub {
				my $line_start = $editor->PositionFromLine($row) + $col;
				my $line_end   = $line_start + $len;
				my $line       = $editor->GetTextRange( $line_start, $line_end );
				$editor->SetSelection( $line_start, $line_end );
				$editor->ReplaceSelection($replace);
				}
		};
	}

	return @items;
}

1;

__END__

=head1 NAME

Padre::Document::Perl::QuickFix::StrictWarnings - Check for strict and warnings pragmas

=head1 DESCRIPTION

This ensures that you have the following in your script:

	use strict;
	use warnings;

# Copyright 2008-2013 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.