Skip to content

Allow custom rendering of unified diff lines - #2871

Merged
jviotti merged 1 commit into
sourcemeta:mainfrom
HarshPopat23:feat/diff-line-writer
Sep 24, 2026
Merged

jviotti merged 1 commit into
sourcemeta:mainfrom
HarshPopat23:feat/diff-line-writer

Conversation

@HarshPopat23

@HarshPopat23 HarshPopat23 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add an optional line_writer callback to sourcemeta::core::Diff::FormatOptions allowing consumers to customize the rendering of individual unified-diff lines.

Background

This enables consumers (such as the JSON Schema CLI's fmt --check command) to style unified diff lines (e.g. coloring deleted lines red, inserted lines green, hunk headers cyan, etc.) while keeping the Core diff engine decoupled from terminal detection, ANSI escape sequences, and color policy.

Details

  • Introduces sourcemeta::core::Diff::FormatOptions::LineType enum class:
    • HeaderOriginal (prefix = "--- ", content = <original_label>)
    • HeaderModified (prefix = "+++ ", content = <modified_label>)
    • Hunk (prefix = "@@ ", content = "-<orig> +<mod> @@")
    • Context (prefix = " ", content = <line>)
    • Delete (prefix = "-", content = <line>)
    • Insert (prefix = "+", content = <line>)
    • NoNewline (prefix = "\\ ", content = "No newline at end of file")
  • Introduces LineWriter callback type: std::function<void(std::ostream &stream, LineType type, std::string_view prefix, std::string_view content)>.
  • Hunk headers are formatted as a single logical line (prefix = "@@ ", content = "-<orig> +<mod> @@").
  • Core retains ownership of the trailing \n.
  • The default path (when no callback is set) remains direct and allocation-free.
  • Appending line_writer to Diff::FormatOptions preserves aggregate initialization and designated initializer source compatibility for existing callers.
  • Adds comprehensive unit test coverage in test/diff/diff_line_myers_test.cc.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Re-trigger cubic

Signed-off-by: HarshPopat23 <musichk61@gmail.com>
@augmentcode

augmentcode Bot commented Sep 24, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: This PR adds per-line customization hooks for unified-diff rendering.

Changes:

  • Adds FormatOptions::LineType to identify headers, hunks, context, inserts, deletes, and missing-newline markers.
  • Adds an optional LineWriter callback to Diff::FormatOptions.
  • Routes unified-diff headers and content lines through the callback when configured.
  • Keeps ownership of each trailing newline in the core formatter.
  • Preserves the direct, allocation-free default rendering path when no callback is supplied.
  • Builds hunk metadata as callback content only when custom rendering is enabled.
  • Documents callback lifetime and line-body responsibilities in the public API.
  • Adds coverage for semantic types, labels, multiple hunks, missing newlines, stream formatting, and embedded NULs.

Technical Notes: The callback is appended to the options aggregate to retain existing source initialization patterns while allowing consumers to apply their own styling policy.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

@HarshPopat23

Copy link
Copy Markdown
Contributor Author

PTAL: @jviotti ,
Guide me what to add befor pr in jsonshema,
sourcemeta/jsonschema#878

@jviotti jviotti left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! I'll merge and let me upgrade it myself on the CLI in a little bit, as I'm managing other upgrades

@jviotti
jviotti merged commit f503133 into sourcemeta:main Sep 24, 2026
15 checks passed
@jviotti

jviotti commented Sep 24, 2026

Copy link
Copy Markdown
Member

See sourcemeta/jsonschema#881

@jviotti

jviotti commented Sep 24, 2026

Copy link
Copy Markdown
Member

@HarshPopat23 Upgrade done! Feel free to rebase your other PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants