All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tassilo Horn <tsdh@gnu.org>
To: git@vger.kernel.org
Cc: Tassilo Horn <tsdh@gnu.org>
Subject: [PATCH v2] userdiff: improve java regex for generic return types
Date: Tue, 10 Aug 2021 10:27:57 +0200	[thread overview]
Message-ID: <20210810082757.219007-1-tsdh@gnu.org> (raw)

Currently, the git diff hunk headers show the wrong method signature if the
method has an array or generic return type because the regex doesn't allow [],
or < and > in the return type.  Also, type parameter declarations couldn't be
matched.

Add several t4018 tests asserting the right hunk headers for increasingly
complex method signatures:

  public String[] secondMethod(String[] RIGHT)
  public List<String> secondMethod(String[] RIGHT)
  public <T> List<T> secondMethod(T[] RIGHT)
  public <AType, B> Map<AType, B> secondMethod(String[] RIGHT)
  public <AType, B> Map<AType, Map<B, B[]>> secondMethod(String[] RIGHT)

Signed-off-by: Tassilo Horn <tsdh@gnu.org>
---
 t/t4018/java-return-array    | 10 ++++++++++
 t/t4018/java-return-generic  | 10 ++++++++++
 t/t4018/java-return-generic2 | 10 ++++++++++
 t/t4018/java-return-generic3 | 10 ++++++++++
 t/t4018/java-return-generic4 | 10 ++++++++++
 userdiff.c                   | 10 +++++++++-
 6 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 t/t4018/java-return-array
 create mode 100644 t/t4018/java-return-generic
 create mode 100644 t/t4018/java-return-generic2
 create mode 100644 t/t4018/java-return-generic3
 create mode 100644 t/t4018/java-return-generic4

diff --git a/t/t4018/java-return-array b/t/t4018/java-return-array
new file mode 100644
index 0000000000..fc8882a5a9
--- /dev/null
+++ b/t/t4018/java-return-array
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+ public String[] secondMethod(String[] RIGHT) {
+        // Whatever...
+        return new; // ChangeMe
+    }
+}
diff --git a/t/t4018/java-return-generic b/t/t4018/java-return-generic
new file mode 100644
index 0000000000..7ea7bf8f07
--- /dev/null
+++ b/t/t4018/java-return-generic
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+    public List<String> secondMethod(String[] RIGHT) {
+        // Whatever...
+        return Arrays.asList("ChangeMe");
+    }
+}
diff --git a/t/t4018/java-return-generic2 b/t/t4018/java-return-generic2
new file mode 100644
index 0000000000..d2d2ab7f37
--- /dev/null
+++ b/t/t4018/java-return-generic2
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+    public <T> List<T> secondMethod(T[] RIGHT) {
+        // Whatever...
+        return (List<T>) Arrays.asList("ChangeMe");
+    }
+}
diff --git a/t/t4018/java-return-generic3 b/t/t4018/java-return-generic3
new file mode 100644
index 0000000000..3db1a1ddba
--- /dev/null
+++ b/t/t4018/java-return-generic3
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+    public <AType, B> Map<AType, B> secondMethod(String[] RIGHT) {
+        // Whatever...
+        return new java.util.HashMap<>(); // ChangeMe
+    }
+}
diff --git a/t/t4018/java-return-generic4 b/t/t4018/java-return-generic4
new file mode 100644
index 0000000000..1fc18a8482
--- /dev/null
+++ b/t/t4018/java-return-generic4
@@ -0,0 +1,10 @@
+class MyExample {
+    public void firstMethod() {
+        // Whatever...
+    }
+
+ public <AType, B> Map<AType, Map<B, B[]>> secondMethod(String[] RIGHT) {
+        // Whatever...
+        return new java.util.HashMap<>(); // ChangeMe
+    }
+}
diff --git a/userdiff.c b/userdiff.c
index 3c3bbe38b0..22f17c1d3e 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -142,7 +142,15 @@ PATTERNS("html",
 	 "[^<>= \t]+"),
 PATTERNS("java",
 	 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
-	 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
+         /* Method signatures contain: */
+         /*   modifiers: public static */
+         "^[ \t]*(([A-Za-z_][A-Za-z]*[ \t]+)*"
+         /*   optionally type parameters: <A, B, C> */
+         "(<[A-Za-z0-9_, \t]+>[ \t]+)?"
+         /*   a return type: Map<A, B[]> */
+         "([A-Za-z_]([A-Za-z_0-9<>,]|\\[[ \t]*\\])*[ \t]+)+"
+         /*   the method name followed by the parameter list: myMethod(...) */
+         "[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
 	 /* -- */
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
-- 
2.32.0


             reply	other threads:[~2021-08-10  8:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-10  8:27 Tassilo Horn [this message]
2021-08-10 14:44 ` [PATCH v2] userdiff: improve java regex for generic return types Tassilo Horn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210810082757.219007-1-tsdh@gnu.org \
    --to=tsdh@gnu.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.