git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 1/3] userdiff: support Java type parameters

From
Andrei Rybak <rybak.a.v@gmail.com>
Date
Feb 8, 2023, 00:04 UTC
Message-ID
<152251b7-79a1-9ea9-01de-61a68b729196@gmail.com>
In-Reply-To
<20230207234259.452141-2-rybak.a.v@gmail.com>
On 2023-02-08T00:42, Andrei Rybak wrote:
Show 31 quoted lines
> A class or interface in Java can have type parameters following the name
> in the declared type, surrounded by angle brackets (paired less than and
> greater than signs).[2]   The type parameters -- `A` and `B` in the
> examples -- may follow the class name immediately:
> 
>      public class ParameterizedClass<A, B> {
>      }
> 
> or may be separated by whitespace:
> 
>      public class SpaceBeforeTypeParameters <A, B> {
>      }
> 
> A part of the builtin userdiff pattern for Java matches declarations of
> classes, enums, and interfaces.  The regular expression requires at
> least one whitespace character after the name of the declared type.
> This disallows matching for opening angle bracket of type parameters
> immediately after the name of the type.  Mandatory whitespace after the
> name of the type also disallows using the pattern in repositories with a
> fairly common code style that puts braces for the body of a class on
> separate lines:
> 
>      class WithLineBreakBeforeOpeningBrace
>      {
>      }
> 
> Support matching Java code in more diverse code styles and declarations
> of classes and interfaces with type parameters immediately following the
> name of the type in the builtin userdiff pattern for Java.  Do so by
> just matching anything until the end of the line after the keywords for
> the kind of type being declared.

The above explains why removing the mandatory matching for whitespace after the class name is needed, but it doesn't explain why removing the part of the regex that matches the class name itself is OK. Perhaps, something like this could be added:

     An possible approach could be to keep matching the name of the
     type: "...[ \t]+[A-Za-z][A-Za-z0-9_$]*.*)$\n", but without matching
     mandatory whitespace after the name of the type, matching the name
     itself separately isn't useful for our purposes.
?
Show 7 quoted lines
> [1] Since Java 5 released in 2004.
> [2] Detailed description is available in the Java Language
>      Specification, sections "Type Variables" and "Parameterized Types":
>      https://docs.oracle.com/javase/specs/jls/se17/html/jls-4.html#jls-4.4
> 
> Signed-off-by: Andrei Rybak <rybak.a.v@gmail.com>
> ---
[...]
Show 13 quoted lines
> diff --git a/userdiff.c b/userdiff.c
> index d71b82feb7..bc5f3ed4c3 100644
> --- a/userdiff.c
> +++ b/userdiff.c
> @@ -171,7 +171,7 @@ PATTERNS("html",
>   PATTERNS("java",
>   	 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
>   	 /* Class, enum, and interface declarations */
> -	 "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+[A-Za-z][A-Za-z0-9_$]*[ \t]+.*)$\n"
> +	 "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+.*)$\n"
>   	 /* Method definitions; note that constructor signatures are not */
>   	 /* matched because they are indistinguishable from method calls. */
>   	 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
Previous: Andrei RybakNext: Andrei Rybak
Message 15 of 19 in “userdiff: Java updates”
  1. 0/3 userdiff: Java updatesAndrei Rybak, Feb 3, 2023
  2. 1/3 userdiff: support Java type parametersAndrei Rybak, Feb 3, 2023
  3. 2/3 userdiff: support Java record typesAndrei Rybak, Feb 3, 2023
  4. 3/3 userdiff: support Java sealed classesAndrei Rybak, Feb 3, 2023
  5. Tassilo HornFeb 4, 2023
  6. 0/3 userdiff: Java updatesAndrei Rybak, Feb 4, 2023
  7. 1/3 userdiff: support Java type parametersAndrei Rybak, Feb 4, 2023
  8. 2/3 userdiff: support Java record typesAndrei Rybak, Feb 4, 2023
  9. 3/3 userdiff: support Java sealed classesAndrei Rybak, Feb 4, 2023
  10. Johannes SixtFeb 5, 2023
  11. Andrei RybakFeb 5, 2023
  12. Johannes SixtFeb 5, 2023
  13. 0/3 userdiff: Java updatesAndrei Rybak, Feb 7, 2023
  14. 1/3 userdiff: support Java type parametersAndrei Rybak, Feb 7, 2023
  15. Andrei RybakFeb 8, 2023
  16. 2/3 userdiff: support Java record typesAndrei Rybak, Feb 7, 2023
  17. 3/3 userdiff: support Java sealed classesAndrei Rybak, Feb 7, 2023
  18. Johannes SixtFeb 8, 2023
  19. Junio C HamanoFeb 8, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.