Skip to content

Conversation

BurhanCantCode
Copy link
Collaborator

Add Granular Diff Controls with Algorithmic Diff Detection

Overview

Replace naive line-by-line diff comparison with proper Myers algorithm and add granular block-level accept/reject controls for better code review workflow.

Problem

  • Massive diffs for small changes: Adding single line <p>Hi Burhan!</p> created 20+ line diffs showing entire sections as removed/added
  • All-or-nothing apply: Users could only accept or reject entire diffs, no granular control over specific chunks
  • Poor diff detection: Manual line comparison created false diff blocks instead of detecting actual changes

Solution

Proper Diff Algorithm

Replaced naive diff with industry-standard Myers algorithm:

// Before: Naive line comparison
const originalLines = originalCode.split('\n')
const mergedLines = mergedCode.split('\n')
// Manual comparison → massive false diffs

// After: Myers algorithm
const changes = diffLines(originalCode, mergedCode, { ignoreWhitespace: false })
// Proper change detection → minimal accurate diffs

Granular Block Controls

  • Individual chunk accept/reject: Users can accept/reject specific diff blocks instead of all-or-nothing
  • Floating widgets: Numbered buttons (1, 2, 3...) positioned next to each diff chunk in Monaco editor
  • Auto-save: Automatically saves file when accepting/rejecting individual blocks
  • Smart cleanup: Removes highlights and widgets after processing blocks
  • Real-time rebuilding: Editor content rebuilds based on accepted/rejected changes

Technical Implementation

Core Diff Processing

  • Uses diffLines from 'diff' npm library (Myers algorithm)
  • Creates DiffBlock objects for each change chunk
  • Tracks LineChange objects within each block
  • Maintains GranularDiffState for editor model

Visual Controls

  • Monaco editor addContentWidget API for floating buttons
  • CSS-styled widgets matching design system
  • Event handlers for accept/reject actions
  • Dynamic positioning next to diff chunks

State Management

  • Block-level tracking with unique IDs
  • Real-time content rebuilding via rebuildEditorFromBlockState
  • Integration with existing save system
  • Proper cleanup of widgets and decorations

Files Changed

  • components/editor/index.tsx - Main diff algorithm and granular controls
  • Added import: import { diffLines } from 'diff'
  • Enhanced handleApplyCode function with proper diff detection
  • Added handleBlockAction, addBlockControlWidgets, rebuildEditorFromBlockState

Result

  • Minimal diffs: Single line additions now show as single green lines
  • Granular control: Users can accept/reject individual chunks
  • Better UX: Auto-save, visual feedback, and clean widget management
  • Industry standard: Uses same diff algorithm as Git, VS Code, GitHub

Copy link

vercel bot commented Jun 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 11, 2025 8:01pm

@jamesmurdza
Copy link
Owner

Nice!

  1. Applying changes shouldn't save the file automatically. It should only save when the user saves it.
  2. The toast notifications are not necessary.

Applying changes don't save the file automatically
removed toast notifications
@BurhanCantCode
Copy link
Collaborator Author

Nice!

  1. Applying changes shouldn't save the file automatically. It should only save when the user saves it.
  2. The toast notifications are not necessary.

done!

@jamesmurdza
Copy link
Owner

Applying the first change removes all the red/green highlighting:

Untitled.mov

@akhileshrangani4
Copy link
Collaborator

looks great so far @BurhanCantCode , couple of more bugs:

  • when you cancel an inline change, it duplicates the line below it.
  • if you cancel all the changes one by one and then re-apply again, it duplicates again.
  • I also feel that there should be some kind of UI feedback on the code snippet so that a user knows that this snippet with was applied to the code at some point.

To recreate this bug:

  • apply a change, and then cancel the changes one by one.
  • cancel all the changes one by one and then re-apply.

image

@akhileshrangani4
Copy link
Collaborator

akhileshrangani4 commented Jun 6, 2025

one more bug:

  • after applying the changes the in-line diff widgets stays in the editor.
  • and the apply button applied the changes to other open file as well.

image

@BurhanCantCode
Copy link
Collaborator Author

thanks for the feedback @jamesmurdza and @akhileshrangani4, ill get to it the earliest i can

…ment and improve file handling

- Fix AI chat showing accept/reject buttons for all files instead of per-file
- Add per-file diff state tracking with fileDiffStates map
- Prevent memory leaks by adding event listener cleanup to block widgets
- Add file versioning to prevent race conditions during rapid file switching
- Fix file redirect logic with proper intended file scoping
- Add async delay for file switching to ensure content loads before code application
… - Successfully resolved merge conflicts between enhancing_codediffs and main branch. Preserved core enhancements: per-file diff state tracking, memory leak prevention, race condition protection, proper file scoping. Fixed integration issues: removed non-existent imports, replaced apiClient with socket calls, fixed saveFile parameters (content to body), added projectId from main. Result: all enhanced functionality intact, compatible with main branch, no TypeScript errors, consistent socket API usage.
…ackage-lock.json

- Added new dependencies: @clerk/backend, @hono/zod-validator, @scalar/hono-api-reference, @t3-oss/env-nextjs, @types/pg, drizzle-orm, drizzle-zod, hono, hono-openapi, pg, zod-openapi.
- Updated existing dependencies: @clerk/shared, @clerk/types, zod to newer versions.
- Removed outdated dependencies and ensured compatibility with the latest versions.
@jamesmurdza jamesmurdza force-pushed the main branch 3 times, most recently from f5acb81 to df7abea Compare July 26, 2025 13:02
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.

3 participants