Skip to content

Conversation

@johnsoncodehk
Copy link
Member

@johnsoncodehk johnsoncodehk commented Dec 11, 2025

Note: The code implementation was written manually, but the tests and this PR summary were created with AI assistance.

Summary

This PR removes the dependency on fast-diff and replaces it with a custom lightweight character alignment algorithm for the range formatting feature in the VS Code extension.

Changes

  • Removed dependency: Removed fast-diff from package.json (reducing bundle size and dependencies)
  • New implementation: Implemented a bidirectional character-matching algorithm in rangeFormatting.ts that:
    • Scans forward from the start to find the beginning of the overlap region
    • Scans backward from the end to find the end of the overlap region
    • Handles whitespace-tolerant matching (spaces, tabs, newlines, carriage returns)
    • Treats non-whitespace characters as aligned in a 1:1 relationship

Algorithm Details

The new approach uses two while loops:

  1. Forward scan: Starting from the beginning, advances through both old and new text until reaching the overlap start position, handling whitespace differences along the way
  2. Backward scan: Starting from the end, advances backward through both texts until reaching the overlap end position

The algorithm considers:

  • Exact character matches → advance both indices
  • Both are whitespace → advance both (whitespace-tolerant)
  • One is whitespace → skip the whitespace character
  • Both are non-whitespace but different → advance both (1:1 replacement)

Known Limitations

This implementation is optimized for formatting operations (primarily whitespace changes) rather than arbitrary content transformations. It works well for:

  • Indentation changes
  • Whitespace normalization
  • Format-on-save scenarios

It has limitations with:

  • Significant non-whitespace content insertions/deletions where length changes dramatically
  • Complex Unicode character transformations

Benefits

  • Zero external dependencies for this feature
  • Smaller bundle size
  • Simpler codebase with transparent algorithm
  • Sufficient for formatting use cases which is the primary purpose of restrictFormattingEditsToRange

Testing

Added comprehensive test coverage including edge cases for whitespace handling, empty selections, boundary conditions, and documented known limitations.

@johnsoncodehk johnsoncodehk changed the title perf(vscode): replace fast-diff with whitespace check perf(vscode): replace fast-diff with custom character-by-character alignment algorithm Dec 11, 2025
@johnsoncodehk johnsoncodehk marked this pull request as ready for review December 11, 2025 13:59
@johnsoncodehk johnsoncodehk merged commit 704b30a into master Dec 11, 2025
13 checks passed
@johnsoncodehk johnsoncodehk deleted the remove-fast-diff branch December 11, 2025 14:00
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