userdiff: add built-in pattern for golang
This adds xfuncname and word_regex patterns for golang, a quite popular programming language. It also includes test cases for the xfuncname regex (t4018) and updated documentation. The xfuncname regex finds functions, structs and interfaces. Although the Go language prohibits the opening brace from being on its own line, the regex does not makes it mandatory, to be able to match `func` statements like this: func foo(bar int, baz int) { } This is covered by the test case t4018/golang-long-func. The word_regex pattern finds identifiers, integers, floats, complex numbers and operators, according to the go specification. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alban Gruin committed
Mar 1, 2018 at 12:19 UTC
1dbf0c0ad6cabfc35aa1896b4d94f4ee811ba803
8 files changed
+37
Documentation/gitattributes.txt
+2
@@ -714,6 +714,8 @@ patterns are available:
714
715
- `fountain` suitable for Fountain documents.
716
717
+- `golang` suitable for source code in the Go language.
718
+
719
- `html` suitable for HTML/XHTML documents.
720
721
- `java` suitable for source code in the Java language.
t/t4018-diff-funcname.sh
+1
@@ -33,6 +33,7 @@ diffpatterns="
33
css
34
fortran
35
fountain
36
+ golang
37
html
38
java
39
matlab
t/t4018/golang-complex-function
new
+8
@@ -0,0 +1,8 @@
1
+type Test struct {
2
+ a Type
3
+}
4
+
5
+func (t *Test) RIGHT(a Type) (Type, error) {
6
+ t.a = a
7
+ return ChangeMe, nil
8
+}
t/t4018/golang-func
new
+4
@@ -0,0 +1,4 @@
1
+func RIGHT() {
2
+ a := 5
3
+ b := ChangeMe
4
+}
t/t4018/golang-interface
new
+4
@@ -0,0 +1,4 @@
1
+type RIGHT interface {
2
+ a() Type
3
+ b() ChangeMe
4
+}
t/t4018/golang-long-func
new
+5
@@ -0,0 +1,5 @@
1
+func RIGHT(aVeryVeryVeryLongVariableName AVeryVeryVeryLongType,
2
+ anotherLongVariableName AnotherLongType) {
3
+ a := 5
4
+ b := ChangeMe
5
+}
t/t4018/golang-struct
new
+4
@@ -0,0 +1,4 @@
1
+type RIGHT struct {
2
+ a Type
3
+ b ChangeMe
4
+}
userdiff.c
+9
@@ -38,6 +38,15 @@ IPATTERN("fortran",
38
"|//|\\*\\*|::|[/<>=]="),
39
IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
40
"[^ \t-]+"),
41
+PATTERNS("golang",
42
+ /* Functions */
43
+ "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
44
+ /* Structs and interfaces */
45
+ "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
46
+ /* -- */
47
+ "[a-zA-Z_][a-zA-Z0-9_]*"
48
+ "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
49
+ "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
50
PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
51
"[^<>= \t]+"),
52
PATTERNS("java",