Skip to content

Commit a8cbc89

Browse files
tsdhgitster
authored andcommitted
userdiff: improve java hunk header regex
Currently, the git diff hunk headers show the wrong method signature if the method has a qualified return type, an array return type, or a generic return type because the regex doesn't allow dots (.), [], or < and > in the return type. Also, type parameter declarations couldn't be matched. Add several t4018 tests asserting the right hunk headers for different cases: - enum constant change - change in generic method with bounded type parameters - change in generic method with wildcard - field change in a nested class Signed-off-by: Tassilo Horn <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2d755df commit a8cbc89

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

t/t4018/java-class-member-function

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ public class Beer
33
int special;
44
public static void main(String RIGHT[])
55
{
6+
someMethodCall();
7+
someOtherMethod("17")
8+
.doThat();
9+
// Whatever
610
System.out.print("ChangeMe");
711
}
812
}

t/t4018/java-enum-constant

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
private enum RIGHT {
2+
ONE,
3+
TWO,
4+
THREE,
5+
ChangeMe
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MyExample {
2+
public <T extends Bar & Foo<T>, R> Map<T, R[]> foo(String[] RIGHT) {
3+
someMethodCall();
4+
someOtherMethod()
5+
.doThat();
6+
// Whatever...
7+
return (List<T>) Arrays.asList("ChangeMe");
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MyExample {
2+
public List<? extends Comparable> foo(String[] RIGHT) {
3+
someMethodCall();
4+
someOtherMethod()
5+
.doThat();
6+
// Whatever...
7+
return Arrays.asList("ChangeMe");
8+
}
9+
}

t/t4018/java-nested-field

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class MyExample {
2+
private static class RIGHT {
3+
// change an inner class field
4+
String inner = "ChangeMe";
5+
}
6+
}

userdiff.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ PATTERNS("html",
142142
"[^<>= \t]+"),
143143
PATTERNS("java",
144144
"!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
145-
"^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
145+
/* Class, enum, and interface declarations */
146+
"^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+[A-Za-z][A-Za-z0-9_$]*[ \t]+.*)$\n"
147+
/* Method definitions; note that constructor signatures are not */
148+
/* matched because they are indistinguishable from method calls. */
149+
"^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
146150
/* -- */
147151
"[a-zA-Z_][a-zA-Z0-9_]*"
148152
"|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"

0 commit comments

Comments
 (0)