Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ let package = Package(
name: "gitdiff",
targets: ["gitdiff"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
.target(
name: "gitdiff"
Expand Down
37 changes: 21 additions & 16 deletions Sources/gitdiff/Views/DiffFileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@
// Created by Tornike Gomareli on 18.06.25.
//


import SwiftUI

/// View for rendering a single file's diff
struct DiffFileView: View {
let file: DiffFile

@Environment(\.diffConfiguration) private var configuration

var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack {
Image(systemName: "doc.text")
.foregroundColor(configuration.theme.fileHeaderText)

Text(file.displayName)
.font(.system(.headline, design: configuration.fontFamily))
.fontWeight(.bold)
.foregroundColor(configuration.theme.fileHeaderText)

Spacer()

if file.isBinary {
Text("Binary file")
.font(.caption)
Expand All @@ -39,7 +38,7 @@ struct DiffFileView: View {
}
.padding()
.background(configuration.theme.fileHeaderBackground)

if file.isBinary {
Text("Binary file not shown")
.font(.system(size: configuration.fontSize, design: configuration.fontFamily))
Expand All @@ -55,7 +54,7 @@ struct DiffFileView: View {
.padding(.vertical, 4)
.frame(maxWidth: .infinity, alignment: .leading)
.background(configuration.theme.headerBackground)

VStack(spacing: configuration.lineSpacing.value) {
ForEach(hunk.lines) { line in
DiffLineView(line: line)
Expand Down Expand Up @@ -86,24 +85,30 @@ struct DiffFileView: View {
lines: [
DiffLine(type: .context, content: "import SwiftUI", oldLineNumber: 1, newLineNumber: 1),
DiffLine(type: .context, content: "", oldLineNumber: 2, newLineNumber: 2),
DiffLine(type: .context, content: "struct ContentView: View {", oldLineNumber: 3, newLineNumber: 3),
DiffLine(type: .added, content: " let title = \"Hello World\"", oldLineNumber: nil, newLineNumber: 4),
DiffLine(type: .context, content: " var body: some View {", oldLineNumber: 4, newLineNumber: 5),
DiffLine(type: .removed, content: " Text(\"Hello, World!\")", oldLineNumber: 5, newLineNumber: nil),
DiffLine(
type: .context, content: "struct ContentView: View {", oldLineNumber: 3, newLineNumber: 3),
DiffLine(
type: .added, content: " let title = \"Hello World\"", oldLineNumber: nil,
newLineNumber: 4),
DiffLine(
type: .context, content: " var body: some View {", oldLineNumber: 4, newLineNumber: 5),
DiffLine(
type: .removed, content: " Text(\"Hello, World!\")", oldLineNumber: 5,
newLineNumber: nil),
DiffLine(type: .added, content: " Text(title)", oldLineNumber: nil, newLineNumber: 6),
DiffLine(type: .added, content: "}", oldLineNumber: nil, newLineNumber: 7)
DiffLine(type: .added, content: "}", oldLineNumber: nil, newLineNumber: 7),
]
)

let sampleFile = DiffFile(
oldPath: "example.swift",
newPath: "example.swift",
hunks: [sampleHunk],
isBinary: false,
isRenamed: false
)

DiffFileView(file: sampleFile)
.padding()
.background(Color(UIColor.systemBackground))
.background(Color(.systemBackground))
}
39 changes: 23 additions & 16 deletions Sources/gitdiff/Views/DiffLineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
// Created by Tornike Gomareli on 18.06.25.
//


import SwiftUI

/// View for rendering a single diff line with GitHub-style formatting
struct DiffLineView: View {
let line: DiffLine

@Environment(\.diffConfiguration) private var configuration

private var backgroundColor: Color {
switch line.type {
case .added:
Expand All @@ -26,7 +25,7 @@ struct DiffLineView: View {
return configuration.theme.headerBackground
}
}

private var foregroundColor: Color {
switch line.type {
case .added:
Expand All @@ -39,7 +38,7 @@ struct DiffLineView: View {
return configuration.theme.headerText
}
}

private var linePrefix: String {
switch line.type {
case .added:
Expand All @@ -50,7 +49,7 @@ struct DiffLineView: View {
return " "
}
}

var body: some View {
HStack(spacing: 0) {
if configuration.showLineNumbers {
Expand All @@ -60,23 +59,31 @@ struct DiffLineView: View {
.frame(width: 20, alignment: .trailing)
.padding(.horizontal, 4)
.background(configuration.theme.lineNumberBackground)

Text(line.newLineNumber.map(String.init) ?? "")
.font(.system(size: configuration.fontSize * 0.85, design: configuration.fontFamily))
.foregroundColor(configuration.theme.lineNumberText)
.frame(width: 20, alignment: .trailing)
.padding(.horizontal, 4)
.background(configuration.theme.lineNumberBackground)
}

HStack(alignment: .top, spacing: 0) {
Text(linePrefix)
.font(.system(size: configuration.fontSize, weight: configuration.fontWeight, design: configuration.fontFamily))
.font(
.system(
size: configuration.fontSize, weight: configuration.fontWeight,
design: configuration.fontFamily)
)
.foregroundColor(foregroundColor)
.padding(.leading, configuration.contentPadding.leading)

Text(line.content)
.font(.system(size: configuration.fontSize, weight: configuration.fontWeight, design: configuration.fontFamily))
.font(
.system(
size: configuration.fontSize, weight: configuration.fontWeight,
design: configuration.fontFamily)
)
.foregroundColor(foregroundColor)
.padding(.leading, 4)
.padding(.trailing, configuration.contentPadding.trailing)
Expand All @@ -98,7 +105,7 @@ struct DiffLineView: View {
newLineNumber: 3
)
)

DiffLineView(
line: DiffLine(
type: .added,
Expand All @@ -107,7 +114,7 @@ struct DiffLineView: View {
newLineNumber: 4
)
)

DiffLineView(
line: DiffLine(
type: .context,
Expand All @@ -116,7 +123,7 @@ struct DiffLineView: View {
newLineNumber: 5
)
)

DiffLineView(
line: DiffLine(
type: .removed,
Expand All @@ -125,7 +132,7 @@ struct DiffLineView: View {
newLineNumber: nil
)
)

DiffLineView(
line: DiffLine(
type: .added,
Expand All @@ -135,5 +142,5 @@ struct DiffLineView: View {
)
)
}
.background(Color(UIColor.systemBackground))
.background(Color(.systemBackground))
}
63 changes: 31 additions & 32 deletions Sources/gitdiff/Views/DiffRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Tornike Gomareli on 18.06.25.
//


import SwiftUI

/// A SwiftUI view that renders git diff output with syntax highlighting.
Expand Down Expand Up @@ -61,44 +60,44 @@ public struct DiffRenderer: View {
.padding()
}
}
.background(Color(UIColor.systemBackground))
.background(Color(.systemBackground))
}
}

#Preview {
let text = """
diff --git a/example.swift b/example.swift
index 1234567..abcdefg 100644
--- a/example.swift
+++ b/example.swift
@@ -1,5 +1,6 @@
import SwiftUI

struct ContentView: View {
+ let title = "Hello World"
var body: some View {
- Text("Hello, World!")
+ Text(title)
}
}
diff --git a/AppDelegate.swift b/AppDelegate.swift
index 2345678..bcdefgh 100644
--- a/AppDelegate.swift
+++ b/AppDelegate.swift
@@ -10,8 +10,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
// Insert code here to initialize your application
+ setupApplication()
diff --git a/example.swift b/example.swift
index 1234567..abcdefg 100644
--- a/example.swift
+++ b/example.swift
@@ -1,5 +1,6 @@


struct ContentView: View {
+ let title = "Hello World"
var body: some View {
- Text("Hello, World!")
+ Text(title)
}
}

- func applicationWillTerminate(_ notification: Notification) {
- // Insert code here to tear down your application
+ private func setupApplication() {
+ // Configure app settings
+ print("Application setup complete")
diff --git a/AppDelegate.swift b/AppDelegate.swift
index 2345678..bcdefgh 100644
--- a/AppDelegate.swift
+++ b/AppDelegate.swift
@@ -10,8 +10,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
// Insert code here to initialize your application
+ setupApplication()
}

- func applicationWillTerminate(_ notification: Notification) {
- // Insert code here to tear down your application
+ private func setupApplication() {
+ // Configure app settings
+ print("Application setup complete")
}
}
}
"""
"""
// Simple usage with default GitHub theme
DiffRenderer(diffText: text)

Expand Down
25 changes: 0 additions & 25 deletions Sources/gitdiff/gitdiff.docc/gitdiff.md

This file was deleted.

Loading