-
Notifications
You must be signed in to change notification settings - Fork 7
Add DiffParsing protocol for custom diff formats #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // | ||
| // DiffParsing.swift | ||
| // gitdiff | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Strategy for turning a diff text into the renderer's domain model. | ||
| /// | ||
| /// `DiffRenderer` reads the active parser from the environment so callers | ||
| /// can plug in formats other than standard unified diff — annotated diffs, | ||
| /// pre-tokenised server output, JSON patches, anything that can produce a | ||
| /// `[DiffFile]`. Inject via the ``SwiftUI/View/diffParser(_:)`` modifier. | ||
| /// | ||
| /// ```swift | ||
| /// DiffRenderer(diffText: rawDiff) | ||
| /// .diffParser(MyAnnotatedDiffParser(filePath: "foo.swift")) | ||
| /// ``` | ||
| public protocol DiffParsing: Sendable { | ||
| /// Parse the given diff text into the renderer's domain model. | ||
| /// | ||
| /// - Parameter diffText: Raw input in whatever format this parser understands. | ||
| /// - Returns: One `DiffFile` per file represented in the input. Returning | ||
| /// an empty array causes `DiffRenderer` to show the "no content" state. | ||
| /// - Throws: Re-thrown by the renderer's `.task` — usually | ||
| /// `CancellationError` if the view disappears mid-parse, but custom | ||
| /// parsers may surface format errors here. | ||
| func parse(_ diffText: String) async throws -> [DiffFile] | ||
| } | ||
|
|
||
| /// Default parser — accepts standard unified-diff text (the output of | ||
| /// `git diff`, `diff -u`, `Diff.createTwoFilesPatch` from jsdiff, etc.). | ||
| public struct UnifiedDiffParser: DiffParsing { | ||
| public init() {} | ||
|
|
||
| public func parse(_ diffText: String) async throws -> [DiffFile] { | ||
| try await DiffParser.parse(diffText) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import Testing | ||
|
|
||
| @testable import gitdiff | ||
|
|
||
| /// Contract tests for the `DiffParsing` extension point: custom parsers can | ||
| /// construct the library's domain model directly without relying on the | ||
| /// built-in unified-diff parser. | ||
| struct DiffParsingTests { | ||
|
|
||
| // MARK: - UnifiedDiffParser | ||
|
|
||
| @Test | ||
| func unifiedDiffParserMatchesLegacyStaticParser() async throws { | ||
| let diff = """ | ||
| diff --git a/foo.txt b/foo.txt | ||
| index 1111111..2222222 100644 | ||
| --- a/foo.txt | ||
| +++ b/foo.txt | ||
| @@ -1,2 +1,3 @@ | ||
| line1 | ||
| -line2 | ||
| +line2 changed | ||
| +line3 | ||
| """ | ||
|
|
||
| let legacy = try await DiffParser.parse(diff) | ||
| let viaProtocol = try await UnifiedDiffParser().parse(diff) | ||
|
|
||
| #expect(legacy.count == viaProtocol.count) | ||
| for (a, b) in zip(legacy, viaProtocol) { | ||
| #expect(a.oldPath == b.oldPath) | ||
| #expect(a.newPath == b.newPath) | ||
| #expect(a.hunks.count == b.hunks.count) | ||
| for (h1, h2) in zip(a.hunks, b.hunks) { | ||
| #expect(h1.lines.count == h2.lines.count) | ||
| for (l1, l2) in zip(h1.lines, h2.lines) { | ||
| #expect(l1.type == l2.type) | ||
| #expect(l1.content == l2.content) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Custom parser path | ||
|
|
||
| @Test | ||
| func customParserCanBuildDiffFilesDirectly() async throws { | ||
| /// A throwaway parser that ignores its input and produces a single | ||
| /// hand-built `DiffFile`. This exercises the public initialisers on | ||
| /// `DiffFile` / `DiffHunk` / `DiffLine` and the protocol contract. | ||
| struct FixtureParser: DiffParsing { | ||
| let filePath: String | ||
| func parse(_ diffText: String) async throws -> [DiffFile] { | ||
| [ | ||
| DiffFile( | ||
| oldPath: filePath, | ||
| newPath: filePath, | ||
| hunks: [ | ||
| DiffHunk( | ||
| oldStart: 1, | ||
| oldCount: 1, | ||
| newStart: 1, | ||
| newCount: 1, | ||
| header: "fixture", | ||
| lines: [ | ||
| DiffLine(type: .removed, content: "old", oldLineNumber: 1, newLineNumber: nil), | ||
| DiffLine(type: .added, content: "new", oldLineNumber: nil, newLineNumber: 1), | ||
| ] | ||
| ) | ||
| ] | ||
| ) | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| let files = try await FixtureParser(filePath: "x.swift").parse("anything") | ||
| #expect(files.count == 1) | ||
| #expect(files[0].oldPath == "x.swift") | ||
| #expect(files[0].hunks[0].lines[0].type == .removed) | ||
| #expect(files[0].hunks[0].lines[1].type == .added) | ||
| } | ||
|
|
||
| @Test | ||
| func defaultEnvironmentParserIsUnifiedDiffParser() { | ||
| /// Confirms the environment default that `DiffRenderer` reads. | ||
| let key = DiffParserKey.defaultValue | ||
| #expect(key is UnifiedDiffParser) | ||
| } | ||
|
|
||
| @Test | ||
| func parserCanReturnEmptyArrayToTriggerNoContentState() async throws { | ||
| struct EmptyParser: DiffParsing { | ||
| func parse(_ diffText: String) async throws -> [DiffFile] { [] } | ||
| } | ||
| let result = try await EmptyParser().parse("ignored") | ||
| #expect(result.isEmpty) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-parse when parser changes, not only when
diffTextchanges.Line 70 keys the task only by
diffText, so changing.diffParser(...)at runtime won’t trigger a new parse and can show stale output.Suggested adjustment
📝 Committable suggestion
🤖 Prompt for AI Agents