/** * Unified Diff Utilities * * Extracts and creates unified diff format from Cursor's edit strategies. * Supports 3 strategies: applyPatch, strReplace, multiStrReplace. * * @module agents/cursor/normalizer/diff-utils */ import type { FileChange } from '../../types/agent-executor.js'; import type { CursorEditTool } from '../types/tools.js'; /** * Extract unified diff hunks from patch content. * * Parses unified diff format and splits into individual hunks. * Each hunk starts with @@ and contains a set of changes. * * @param patchContent - Unified diff patch content * @returns Array of hunk strings (including @@ headers) * * @example * ```typescript * const patch = `@@ -10,7 +10,7 @@ function example() { * const unchanged = 'same'; * -const old = 'value'; * +const new = 'value'; * }`; * * const hunks = extractUnifiedDiffHunks(patch); * // Returns: ['@@ -10,7 +10,7 @@ function example() {\n ...'] * ``` */ export declare function extractUnifiedDiffHunks(patchContent: string): string[]; /** * Create complete unified diff with file header and hunks. * * Combines file path header with multiple hunks into a complete * unified diff format suitable for UI display or patching tools. * * @param filePath - Relative file path * @param hunks - Array of hunk strings (from extractUnifiedDiffHunks) * @returns Complete unified diff with header * * @example * ```typescript * const hunks = ['@@ -10,3 +10,3 @@\n-old\n+new']; * const diff = concatenateDiffHunks('src/file.ts', hunks); * // Returns: * // --- a/src/file.ts * // +++ b/src/file.ts * // @@ -10,3 +10,3 @@ * // -old * // +new * ``` */ export declare function concatenateDiffHunks(filePath: string, hunks: string[]): string; /** * Create unified diff from string replacement. * * Converts a simple oldText → newText replacement into unified diff format. * Estimates line numbers based on newline counts. Does not include context lines. * * @param filePath - Relative file path * @param oldText - Text being replaced * @param newText - New text content * @returns Unified diff with minimal context * * @example * ```typescript * const diff = createUnifiedDiff( * 'src/file.ts', * 'const x = 1;', * 'const x = 2;' * ); * // Returns: * // --- a/src/file.ts * // +++ b/src/file.ts * // @@ -1,1 +1,1 @@ * // -const x = 1; * // +const x = 2; * ``` */ export declare function createUnifiedDiff(filePath: string, oldText: string, newText: string): string; /** * Create single unified diff hunk without file header. * * Used for multi-edit scenarios where multiple hunks are concatenated. * Creates a minimal hunk with @@ header and change lines. * * @param oldText - Text being replaced * @param newText - New text content * @returns Single hunk string (no file header) * * @example * ```typescript * const hunk = createUnifiedDiffHunk('old value', 'new value'); * // Returns: * // @@ -1,1 +1,1 @@ * // -old value * // +new value * ``` */ export declare function createUnifiedDiffHunk(oldText: string, newText: string): string; /** * Extract FileChange array from Cursor edit tool args. * * Routes to appropriate strategy based on args structure: * - applyPatch: Full unified diff from Cursor * - strReplace: Single string replacement * - multiStrReplace: Multiple replacements (multiple hunks) * * @param args - Edit tool arguments * @param filePath - File path being edited (from args.path) * @returns Array of FileChange objects with unified diffs * * @example * ```typescript * // Strategy 1: applyPatch * const changes = extractEditChanges({ * path: 'src/file.ts', * applyPatch: { patchContent: '@@...' } * }, 'src/file.ts'); * * // Strategy 2: strReplace * const changes = extractEditChanges({ * path: 'src/file.ts', * strReplace: { oldText: 'old', newText: 'new' } * }, 'src/file.ts'); * * // Strategy 3: multiStrReplace * const changes = extractEditChanges({ * path: 'src/file.ts', * multiStrReplace: { * edits: [ * { oldText: 'old1', newText: 'new1' }, * { oldText: 'old2', newText: 'new2' } * ] * } * }, 'src/file.ts'); * ``` */ export declare function extractEditChanges(args: CursorEditTool['editToolCall']['args'], filePath: string): FileChange[]; /** * Extract FileChange from edit tool result. * * Fallback strategy when args don't contain enough information. * Tries to extract diffString from result.success. * * @param result - Edit tool result * @param filePath - File path being edited * @returns FileChange with unified diff, or null if no diff found * * @example * ```typescript * const change = extractResultDiff( * { success: { diffString: '@@ -1,1 +1,1 @@\n-old\n+new' } }, * 'src/file.ts' * ); * ``` */ export declare function extractResultDiff(result: any, filePath: string): FileChange | null; /** * Validate unified diff format. * * Checks if string is a valid unified diff (has @@ markers and +/- lines). * * @param diff - Potential unified diff string * @returns True if valid unified diff format * * @example * ```typescript * isValidUnifiedDiff('@@ -1,1 +1,1 @@\n-old\n+new'); // true * isValidUnifiedDiff('not a diff'); // false * ``` */ export declare function isValidUnifiedDiff(diff: string): boolean; //# sourceMappingURL=diff-utils.d.ts.map