threads / patch / 63523

patch, 2 partsuserdiff: add support for R programming language

Subject: [PATCH 1/2] userdiff: add support for R programming language

## tl;dr

9 messages between May 25, 2025 and Jun 3, 2025. Diffs are folded; open one to read it.

replies: 8people: 4as markdown or json

Rodrigo Carvalho· May 25, 2025, 21:02 UTC · lore

The patch appends userdiff.c file in order to support R programming language function header. This will be useful for those who use Git for versioning .R files.

Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
---
 userdiff.c | 4 ++++
 1 file changed, 4 insertions(+)
Show changes to userdiff.c +4 −0
diff --git a/userdiff.c b/userdiff.c
index da75625020..d1d31ea67e 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -317,6 +317,10 @@ PATTERNS("python",
 	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
 	 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
 	 /* -- */
+PATTERNS("r",
+	"^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*<-[ \t]*function.*)$",
+	/* -- */
+	"[a-zA-Z_][a-zA-Z0-9_.]*"),
 PATTERNS("ruby",
 	 "^[ \t]*((class|module|def)[ \t].*)$",
 	 /* -- */
-- 
2.43.0
Rodrigo Carvalho· May 25, 2025, 21:02 UTC · re: Rodrigo Carvalho · lore

[PATCH 2/2] t4018: add two userdiff tests for R language

This patch adds two userdiff tests for R programming language files. These files define a simple function, with and without indentation.

Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
---
 t/t4018/r-indent   | 6 ++++++
 t/t4018/r-noindent | 6 ++++++
 2 files changed, 12 insertions(+)
 create mode 100644 t/t4018/r-indent
 create mode 100644 t/t4018/r-noindent
Show changes to 2 files +12 −0

t/t4018/r-indent, t/t4018/r-noindent

diff --git a/t/t4018/r-indent b/t/t4018/r-indent
new file mode 100644
index 0000000000..9df440f2a4
--- /dev/null
+++ b/t/t4018/r-indent
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+    c = mean(a, b)
+    d = c + 2
+    ChangeMe()
+    return (d)
+}
diff --git a/t/t4018/r-noindent b/t/t4018/r-noindent
new file mode 100644
index 0000000000..6d9b01ffe3
--- /dev/null
+++ b/t/t4018/r-noindent
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+c = mean(a, b)
+d = c + 2
+ChangeMe()
+return (c)
+}
-- 
2.43.0
Eric Sunshine· May 25, 2025, 22:42 UTC · re: Rodrigo Carvalho · lore

Re: [PATCH 2/2] t4018: add two userdiff tests for R language

On Sun, May 25, 2025 at 5:03 PM Rodrigo Carvalho <rodrigorsdc@gmail.com> wrote:
Show 14 quoted lines
> This patch adds two userdiff tests for R programming language
> files. These files define a simple function, with and without
> indentation.
>
> Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
> ---
> diff --git a/t/t4018/r-indent b/t/t4018/r-indent
> @@ -0,0 +1,6 @@
> +RIGHT <- function(a, b) {
> +    c = mean(a, b)
> +    d = c + 2
> +    ChangeMe()
> +    return (d)
> +}

Although nontraditional, do we care that some people define functions using `=` rather than `<-`?

    foo = function(x) {...}
Johannes Sixt· May 26, 2025, 05:57 UTC · re: Rodrigo Carvalho · lore

Re: [PATCH 1/2] userdiff: add support for R programming language

Am 25.05.25 um 23:02 schrieb Rodrigo Carvalho:
Show 18 quoted lines
> The patch appends userdiff.c file in order to support R programming
> language function header. This will be useful for those who use Git
> for versioning .R files.
> 
> Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
> ---
>  userdiff.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/userdiff.c b/userdiff.c
> index da75625020..d1d31ea67e 100644
> --- a/userdiff.c
> +++ b/userdiff.c
> @@ -317,6 +317,10 @@ PATTERNS("python",
>  	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
>  	 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
>  	 /* -- */
> +PATTERNS("r",

Rant mode: I am not a fan of the name "R". It is so un-searchable. It hijacks a single character for its own purpose.

I don't see a negative impact in this case, though.
> +	"^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*<-[ \t]*function.*)$",

I wonder how useful this is in practice. Unlike C or Java for example, code can live outside of functions in R scripts. If you have a script without any functions, there would not be any hunk headers. If you have a script with a mix of functions and code outside of functions, the code after a function would be attributed to the function. I'm not saying that this is bad, but just asking if this is part of the plan.

> +	/* -- */
> +	"[a-zA-Z_][a-zA-Z0-9_.]*"),

This singles out identifiers. Every single other characters would be its own word. I'd consider this a disimprovement. If you are not prepared to provide worddiff patterns, I recommend to use "[^ \t]+", which roughly amounts to the default behavior. It can be improved incrementally in later patches.

>  PATTERNS("ruby",
>  	 "^[ \t]*((class|module|def)[ \t].*)$",
>  	 /* -- */

Please squash the test cases into this patch. Don't forget to test an indented function, and while at it, test a function definition *nested* in a function definition: that documents what the expected outcome is.

-- Hannes
Junio C Hamano· May 27, 2025, 15:04 UTC · re: Johannes Sixt · lore

Re: [PATCH 1/2] userdiff: add support for R programming language

Johannes Sixt <j6t@kdbg.org> writes:
Show 8 quoted lines
>> +	"^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*<-[ \t]*function.*)$",
>
> I wonder how useful this is in practice. Unlike C or Java for example,
> code can live outside of functions in R scripts. If you have a script
> without any functions, there would not be any hunk headers. If you have
> a script with a mix of functions and code outside of functions, the code
> after a function would be attributed to the function. I'm not saying
> that this is bad, but just asking if this is part of the plan.
Isn't it the same as shell, perl, python, e-lisp and perhaps others?

If we can reliably detect that we are outside of any function and set it to an empty string that would be great ;-).

Show 8 quoted lines
>> +	/* -- */
>> +	"[a-zA-Z_][a-zA-Z0-9_.]*"),
>
> This singles out identifiers. Every single other characters would be its
> own word. I'd consider this a disimprovement. If you are not prepared to
> provide worddiff patterns, I recommend to use "[^ \t]+", which roughly
> amounts to the default behavior. It can be improved incrementally in
> later patches.
Good point.
> Please squash the test cases into this patch. Don't forget to test an
> indented function, and while at it, test a function definition *nested*
> in a function definition: that documents what the expected outcome is.
Again, good point.
Thanks.
Rodrigo Carvalho· May 29, 2025, 21:36 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] userdiff: add support for R programming language

Show 7 quoted lines
> > This singles out identifiers. Every single other characters would be its
> > own word. I'd consider this a disimprovement. If you are not prepared to
> > provide worddiff patterns, I recommend to use "[^ \t]+", which roughly
> > amounts to the default behavior. It can be improved incrementally in
> > later patches.
>
> Good point.
Thanks for the remark. I'll change to this approach in v2
Show 5 quoted lines
> > Please squash the test cases into this patch. Don't forget to test an
> > indented function, and while at it, test a function definition *nested*
> > in a function definition: that documents what the expected outcome is.
>
> Again, good point.

Sure, that is an important feature that a userdiff driver should address. Thanks again

Rodrigo Carvalho· May 29, 2025, 22:16 UTC · re: Rodrigo Carvalho · lore

[PATCH v2] userdiff: add support for R programming language

The patch appends userdiff.c file in order to support R programming language function header. This will be useful for those who use Git for versioning .R files.

Also, add three userdiff tests for R programming language files. These files define simple function and nested function, with and without indentation.

Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
---
Changes in v2:
- Add support for function defined by '='
- Replace word regex to '[^ \t']
- Add new test that handles with nested function
 t/t4018/r-indent        |  6 ++++++
 t/t4018/r-indent-nested | 10 ++++++++++
 t/t4018/r-noindent      |  6 ++++++
 userdiff.c              |  4 ++++
 4 files changed, 26 insertions(+)
 create mode 100644 t/t4018/r-indent
 create mode 100644 t/t4018/r-indent-nested
 create mode 100644 t/t4018/r-noindent
Show changes to 4 files +26 −0

t/t4018/r-indent, t/t4018/r-indent-nested, t/t4018/r-noindent, userdiff.c

diff --git a/t/t4018/r-indent b/t/t4018/r-indent
new file mode 100644
index 0000000000..9df440f2a4
--- /dev/null
+++ b/t/t4018/r-indent
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+    c = mean(a, b)
+    d = c + 2
+    ChangeMe()
+    return (d)
+}
diff --git a/t/t4018/r-indent-nested b/t/t4018/r-indent-nested
new file mode 100644
index 0000000000..30412e6c79
--- /dev/null
+++ b/t/t4018/r-indent-nested
@@ -0,0 +1,10 @@
+LEFT = function(a, b) {
+    c = mean(a, b)
+    RIGHT = function(d, e) {
+        f = var(d, e)
+        g = f + 1
+        ChangeMe()
+        return (g)
+    }
+    return (RIGHT(2, 3))
+}
diff --git a/t/t4018/r-noindent b/t/t4018/r-noindent
new file mode 100644
index 0000000000..6d9b01ffe3
--- /dev/null
+++ b/t/t4018/r-noindent
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+c = mean(a, b)
+d = c + 2
+ChangeMe()
+return (c)
+}
diff --git a/userdiff.c b/userdiff.c
index da75625020..64e5005682 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -317,6 +317,10 @@ PATTERNS("python",
 	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
 	 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
 	 /* -- */
+PATTERNS("r",
+	"^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*(<-|=)[ \t]*function.*)$",
+	/* -- */
+	"[^ \t]+"),
 PATTERNS("ruby",
 	 "^[ \t]*((class|module|def)[ \t].*)$",
 	 /* -- */
-- 
2.43.0
Junio C Hamano· Jun 3, 2025, 18:20 UTC · re: Rodrigo Carvalho · lore

Re: [PATCH v2] userdiff: add support for R programming language

Rodrigo Carvalho <rodrigorsdc@gmail.com> writes:
Show 14 quoted lines
> The patch appends userdiff.c file in order to support R programming
> language function header. This will be useful for those who use Git
> for versioning .R files.
>
> Also, add three userdiff tests for R programming language
> files. These files define simple function and nested function,
> with and without indentation.
>
> Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
> ---
> Changes in v2:
> - Add support for function defined by '='
> - Replace word regex to '[^ \t']
> - Add new test that handles with nested function
Sorry, but this fell of the radar.

It seems all the reviewer comments on the previous round have been addressed. If everybody is happy with this version, let's merge it down to 'next', for the next cycle.

Thanks.
Johannes Sixt· Jun 3, 2025, 21:15 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] userdiff: add support for R programming language

Am 03.06.25 um 20:20 schrieb Junio C Hamano:
Show 24 quoted lines
> Rodrigo Carvalho <rodrigorsdc@gmail.com> writes:
> 
>> The patch appends userdiff.c file in order to support R programming
>> language function header. This will be useful for those who use Git
>> for versioning .R files.
>>
>> Also, add three userdiff tests for R programming language
>> files. These files define simple function and nested function,
>> with and without indentation.
>>
>> Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
>> ---
>> Changes in v2:
>> - Add support for function defined by '='
>> - Replace word regex to '[^ \t']
>> - Add new test that handles with nested function
> 
> Sorry, but this fell of the radar.
> 
> It seems all the reviewer comments on the previous round have been
> addressed.  If everybody is happy with this version, let's merge it
> down to 'next', for the next cycle.
> 
> Thanks.
Sorry for being slow. This round looks good to me, too.
-- Hannes

← back to recent threads