Skip to contentSkip to conversation

userdiff: add support for R programming language

PATCH Superseded Rodrigo Carvalho 4 participants 9 messages May 25, 2025 – Jun 3, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 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.

---
 userdiff.c | 4 ++++
 1 file changed, 4 insertions(+)
userdiff.c +4 -0
index da75625020..d1d31ea67e 100644
@@ -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].*)$",
 	 /* -- */
Signature · 1 line
2.43.0
#1 lore raw

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.

---
 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
t/t4018/r-indent new file +6 -0
new file mode 100644
index 0000000000..9df440f2a4
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+    c = mean(a, b)
+    d = c + 2
+    ChangeMe()
+    return (d)
+}
t/t4018/r-noindent new file +6 -0
new file mode 100644
index 0000000000..6d9b01ffe3
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+c = mean(a, b)
+d = c + 2
+ChangeMe()
+return (c)
+}
Signature · 1 line
2.43.0
#2 lore raw

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

Quoted 8 lines · Rodrigo Carvalho

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

---

t/t4018/r-indent line 6

Quoted 6 lines
t/t4018/r-indent +6 -0
@@ -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) {...}
#3 lore raw
In reply to Rodrigo Carvalho

Am 25.05.25 um 23:02 schrieb Rodrigo Carvalho:

Quoted 14 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.

---
 userdiff.c | 4 ++++
 1 file changed, 4 insertions(+)
userdiff.c +0 -0
index da75625020..d1d31ea67e 100644

userdiff.c PATTERNS("python", · line 320

Quoted 4 lines
userdiff.c +1 -0
@@ -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.

userdiff.c PATTERNS("python", · line 321

Quoted 1 line
userdiff.c +1 -0
+	"^[ \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.

userdiff.c PATTERNS("python", · line 323

Quoted 2 lines
userdiff.c +2 -0
+	/* -- */
+	"[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.

userdiff.c PATTERNS("python", · line 326

Quoted 3 lines
userdiff.c +0 -0
 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

#4 lore raw
Quoted 8 lines · Johannes Sixt

userdiff.c PATTERNS("python", · line 321

Quoted 1 line
userdiff.c +1 -0
+	"^[ \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 ;-).

Quoted 8 lines

userdiff.c PATTERNS("python", · line 323

Quoted 2 lines
userdiff.c +2 -0
+	/* -- */
+	"[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.

Quoted 3 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.

Thanks.

#5 lore raw
Quoted 7 lines
Quoted 5 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

Quoted 5 lines
Quoted 3 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

#6 lore raw

PATCH v2 1/1 userdiff: add support for R programming language

In reply to Rodrigo Carvalho

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.

---
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
t/t4018/r-indent new file +6 -0
new file mode 100644
index 0000000000..9df440f2a4
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+    c = mean(a, b)
+    d = c + 2
+    ChangeMe()
+    return (d)
+}
t/t4018/r-indent-nested new file +10 -0
new file mode 100644
index 0000000000..30412e6c79
@@ -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))
+}
t/t4018/r-noindent new file +6 -0
new file mode 100644
index 0000000000..6d9b01ffe3
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+c = mean(a, b)
+d = c + 2
+ChangeMe()
+return (c)
+}
userdiff.c +4 -0
index da75625020..64e5005682 100644
@@ -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].*)$",
 	 /* -- */
Signature · 1 line
2.43.0
#7 lore raw
Quoted 8 lines · Rodrigo Carvalho

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.

Quoted 6 lines

---
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.

#8 lore raw

Am 03.06.25 um 20:20 schrieb Junio C Hamano:

Quoted 17 lines
Quoted 8 lines · Rodrigo Carvalho

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.

Quoted 6 lines

---
Changes in v2:
- Add support for function defined by '='
- Replace word regex to '[^ \t']
- Add new test that handles with nested function

Quoted 7 lines

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