userdiff/perl: catch sub with brace on second line
Accept sub foo { } as an alternative to a more common style that introduces perl functions with a brace on the first line (and likewise for BEGIN/END blocks). The new regex is a little hairy to avoid matching # forward declaration sub foo; while continuing to match "sub foo($;@) {" and sub foo { # This routine is interesting; # in fact, the lines below explain how... While at it, pay attention to Perl 5.14's "package foo {" syntax as an alternative to the traditional "package foo;". Requested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
May 21, 2011 at 14:38 UTC
ea2ca4497bdb716977a3e2526780635cb6bac513
2 files changed
+40
-5
t/t4018-diff-funcname.sh
+23
-2
@@ -35,7 +35,11 @@ package Beer;
35
use strict;
36
use warnings;
37
use parent qw(Exporter);
38
-our @EXPORT_OK = qw(round);
38
+our @EXPORT_OK = qw(round finalround);
39
+
40
+sub other; # forward declaration
41
+
42
+# hello
43
44
sub round {
45
my ($n) = @_;
@@ -46,6 +50,12 @@ sub round {
50
print "$n bottles of beer on the wall.\n";
51
}
52
53
+sub finalround
54
+{
55
+ print "Go to the store, buy some more\n";
56
+ print "99 bottles of beer on the wall.\n");
57
+}
58
+
59
__END__
60
61
=head1 NAME
@@ -54,12 +64,13 @@ Beer - subroutine to output fragment of a drinking song
64
65
=head1 SYNOPSIS
66
57
- use Beer qw(round);
67
+ use Beer qw(round finalround);
68
69
sub song {
70
for (my $i = 99; $i > 0; $i--) {
71
round $i;
72
}
73
+ finalround;
74
}
75
76
song;
@@ -67,7 +78,9 @@ Beer - subroutine to output fragment of a drinking song
78
=cut
79
EOF
80
sed -e '
81
+ s/hello/goodbye/
82
s/beer\\/beer,\\/
83
+ s/more\\/more,\\/
84
s/song;/song();/
85
' <Beer.perl >Beer-correct.perl
86
@@ -121,6 +134,10 @@ test_expect_success 'preset perl pattern' '
134
test_expect_funcname "sub round {\$" perl
135
'
136
137
+test_expect_success 'perl pattern accepts K&R style brace placement, too' '
138
+ test_expect_funcname "sub finalround\$" perl
139
+'
140
+
141
test_expect_success 'perl pattern is not distracted by sub within POD' '
142
test_expect_funcname "=head" perl
143
'
@@ -129,6 +146,10 @@ test_expect_success 'perl pattern gets full line of POD header' '
146
test_expect_funcname "=head1 SYNOPSIS\$" perl
147
'
148
149
+test_expect_success 'perl pattern is not distracted by forward declaration' '
150
+ test_expect_funcname "package Beer;\$" perl
151
+'
152
+
153
test_expect_success 'custom pattern' '
154
test_config diff.java.funcname "!static
155
!String
userdiff.c
+17
-3
@@ -60,9 +60,23 @@ PATTERNS("pascal",
60
"|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
61
"|<>|<=|>=|:=|\\.\\."),
62
PATTERNS("perl",
63
- "^package .*;\n"
64
- "^sub .* \\{\n"
65
- "^[A-Z]+ \\{\n" /* BEGIN, END, ... */
63
+ "^package .*\n"
64
+ "^sub [[:alnum:]_':]+[ \t]*"
65
+ "(\\([^)]*\\)[ \t]*)?" /* prototype */
66
+ /*
67
+ * Attributes. A regex can't count nested parentheses,
68
+ * so just slurp up whatever we see, taking care not
69
+ * to accept lines like "sub foo; # defined elsewhere".
70
+ *
71
+ * An attribute could contain a semicolon, but at that
72
+ * point it seems reasonable enough to give up.
73
+ */
74
+ "(:[^;#]*)?"
75
+ "(\\{[ \t]*)?" /* brace can come here or on the next line */
76
+ "(#.*)?$\n" /* comment */
77
+ "^[A-Z]+[ \t]*" /* BEGIN, END, ... */
78
+ "(\\{[ \t]*)?" /* brace can come here or on the next line */
79
+ "(#.*)?$\n"
80
"^=head[0-9] .*", /* POD */
81
/* -- */
82
"[[:alpha:]_'][[:alnum:]_']*"