Skip to content

Commit b51d465

Browse files
shkulsgitster
authored andcommitted
userdiff: add support for Swift
Add a built-in userdiff driver for the Swift programming language so that diff hunk headers and word diffs work out of the box for ".swift" files. The funcname pattern is built for Swift's own declaration grammar: an optional run of attributes ("@objc", "@available(iOS 13, *)", ...), followed by an optional run of lowercase modifiers ("public", "static", "final", ...), followed by a declaration keyword (func, class, struct, enum, protocol, extension, actor, init, deinit, subscript). The keyword is followed by a boundary that allows whitespace, "(" (init/subscript), "?" or "!" (failable init), or "<" (generics), while still acting as a word boundary so e.g. "initialize(" does not match. The word regex recognizes Swift identifiers, hexadecimal, octal, binary, integer and floating-point literals, and the language's operators. Signed-off-by: Shlok Kulshreshtha <diy2903@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 41365c2 commit b51d465

15 files changed

Lines changed: 86 additions & 0 deletions

Documentation/gitattributes.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,8 @@ patterns are available:
914914
- `scheme` suitable for source code in most Lisp dialects,
915915
including Scheme, Emacs Lisp, Common Lisp, and Clojure.
916916

917+
- `swift` suitable for source code in the Swift language.
918+
917919
- `tex` suitable for source code for LaTeX documents.
918920

919921

t/t4018/swift-actor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
actor RIGHT {
2+
let a = 1
3+
// a comment
4+
let b = ChangeMe
5+
}

t/t4018/swift-attribute-with-args

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct View {
2+
@available(iOS 13, *) public func RIGHT() {
3+
let a = 1
4+
// a comment
5+
print(ChangeMe)
6+
}
7+
}

t/t4018/swift-class

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RIGHT {
2+
let a = 1
3+
// a comment
4+
let b = ChangeMe
5+
}

t/t4018/swift-enum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum RIGHT {
2+
case first
3+
// a comment
4+
case ChangeMe
5+
}

t/t4018/swift-extension

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extension RIGHT {
2+
static let a = 1
3+
// a comment
4+
static let b = ChangeMe
5+
}

t/t4018/swift-failable-init

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Bar {
2+
init?(RIGHT: Int) {
3+
let x = 0
4+
// a comment
5+
print(ChangeMe)
6+
}
7+
}

t/t4018/swift-func

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func RIGHT(x: Int) -> Int {
2+
let y = x
3+
// a comment
4+
return ChangeMe
5+
}

t/t4018/swift-generic-subscript

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct Container {
2+
subscript<RIGHT>(index: Int) -> Int {
3+
let a = 0
4+
// a comment
5+
return ChangeMe
6+
}
7+
}

t/t4018/swift-init

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Foo {
2+
init(RIGHT: Int) {
3+
let x = 0
4+
// a comment
5+
print(ChangeMe)
6+
}
7+
}

0 commit comments

Comments
 (0)