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 <tsdh@gnu.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tassilo Horn committed Aug 11, 2021 at 19:51 UTC a8cbc895893f4c244e54374d3bf937819fb6e2e9
6 files changed +39 -1
t/t4018/java-class-member-function
+4
@@ -3,6 +3,10 @@ public class Beer
3 int special;
4 public static void main(String RIGHT[])
5 {
6 + someMethodCall();
7 + someOtherMethod("17")
8 + .doThat();
9 + // Whatever
10 System.out.print("ChangeMe");
11 }
12 }
t/t4018/java-enum-constant new
+6
@@ -0,0 +1,6 @@
1 +private enum RIGHT {
2 + ONE,
3 + TWO,
4 + THREE,
5 + ChangeMe
6 +}
t/t4018/java-method-return-generic-bounded new
+9
@@ -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 +}
t/t4018/java-method-return-generic-wildcard new
+9
@@ -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 new
+6
@@ -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
+5 -1
@@ -142,7 +142,11 @@ PATTERNS("html",
142 "[^<>= \t]+"),
143 PATTERNS("java",
144 "!^[ \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]*\\([^;]*)$",
150 /* -- */
151 "[a-zA-Z_][a-zA-Z0-9_]*"
152 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"