diff --git a/core/nextEdit/diff/diff.ts b/core/nextEdit/diff/diff.ts index f0316d56d..0a06bd1eb 100644 --- a/core/nextEdit/diff/diff.ts +++ b/core/nextEdit/diff/diff.ts @@ -210,6 +210,9 @@ export function calculateFinalCursorPosition( oldEditRangeSlice: string, newEditRangeSlice: string, ) { + if (newEditRangeSlice === "") { + return currCursorPos; + } // How far away is the current line from the start of the editable region? const lineOffsetAtCursorPos = currCursorPos.line - editableRegionStartLine; @@ -274,81 +277,163 @@ export interface DiffGroup { type?: string; // Optional classification of the group } +/** + * Group diff lines into meaningful sections based on changes + * @param diffLines The diff lines to group + * @param offset Optional line offset to apply to the resulting line numbers + * @param maxGroupSize Optional maximum group size constraint (Mode 2) + * @returns Array of DiffGroup objects representing the changes + */ export function groupDiffLines( diffLines: DiffLine[], - maxGroupSize: number = 5, + offset: number = 0, + maxGroupSize?: number, ): DiffGroup[] { const groups: DiffGroup[] = []; + const changedAreas = findChangedAreas(diffLines); - // First pass: find areas where changes occur + for (const area of changedAreas) { + if (maxGroupSize === undefined) { + // Mode 1: Flexible group size. + groups.push( + processFlexibleSizeGroup(diffLines, area.start, area.end, offset), + ); + } else { + // Mode 2: Limited group size. + groups.push( + processLimitedSizeGroup( + diffLines, + area.start, + area.end, + maxGroupSize, + offset, + ), + ); + } + } + + return groups; +} + +/** + * Find areas of change in the diff lines. + */ +function findChangedAreas( + diffLines: DiffLine[], +): { start: number; end: number }[] { + const changedAreas: { start: number; end: number }[] = []; let changedAreaStart = -1; for (let i = 0; i < diffLines.length; i++) { if (diffLines[i].type !== "same" && changedAreaStart === -1) { changedAreaStart = i; } else if (diffLines[i].type === "same" && changedAreaStart !== -1) { - // We've found the end of a changed area, process it - processChangedArea(changedAreaStart, i - 1); + // We've found the end of a changed area. + changedAreas.push({ start: changedAreaStart, end: i - 1 }); changedAreaStart = -1; } } - // Handle the last changed area if it extends to the end + // Handle the last changed area if it extends to the end. if (changedAreaStart !== -1) { - processChangedArea(changedAreaStart, diffLines.length - 1); + changedAreas.push({ start: changedAreaStart, end: diffLines.length - 1 }); } - function processChangedArea(start: number, end: number) { - // Calculate result line numbers - let resultLineStart = 0; - - // Count how many result lines appear before the start - for (let i = 0; i < start; i++) { - if (diffLines[i].type !== "old") { - resultLineStart++; - } - } - - // Create a group from the change area - const changedLines = diffLines.slice(start, end + 1); - - // Count how many additional lines we can include - const changedResultLineCount = changedLines.filter( - (l) => l.type !== "old", - ).length; - const additionalLinesNeeded = maxGroupSize - changedResultLineCount; - - // Add additional context lines after the change - let additionalLines: DiffLine[] = []; - let additionalResultLineCount = 0; - - for ( - let i = end + 1; - i < diffLines.length && additionalResultLineCount < additionalLinesNeeded; - i++ - ) { - additionalLines.push(diffLines[i]); - if (diffLines[i].type !== "old") { - additionalResultLineCount++; - } - } - - // Calculate the ending result line number - let resultLineEnd = resultLineStart; - for (const line of [...changedLines, ...additionalLines]) { - if (line.type !== "old") { - resultLineEnd++; - } - } - resultLineEnd--; // Adjust because we incremented one too many times - - // Create the group - groups.push({ - startLine: resultLineStart, - endLine: resultLineEnd, - lines: [...changedLines, ...additionalLines], - }); - } - - return groups; + return changedAreas; +} + +/** + * Count the number of lines in the old content (excluding "new" lines). + */ +function countOldContentLines( + diffLines: DiffLine[], + startIdx: number, + endIdx: number, +): number { + let count = 0; + for (let i = startIdx; i <= endIdx; i++) { + if (diffLines[i].type !== "new") { + count++; + } + } + return count; +} + +/** + * Process a changed area with a limited group size. + */ +function processLimitedSizeGroup( + diffLines: DiffLine[], + start: number, + end: number, + maxGroupSize: number, + offset: number, +): DiffGroup { + // Calculate the starting line in old content. + let oldContentLineStart = countOldContentLines(diffLines, 0, start - 1); + + // Track how many lines we have left in our group size budget. + let remainingGroupSize = maxGroupSize; + let currentLine = start; + const lines: DiffLine[] = []; + + // Process lines until we hit our size limit or reach the end. + while (currentLine <= end && remainingGroupSize > 0) { + // Add current line to results if we haven't seen it yet. + if ( + lines.length === 0 || + (lines.length > 0 && + lines[lines.length - 1].line !== diffLines[currentLine].line) + ) { + lines.push(diffLines[currentLine]); + } + + if (diffLines[currentLine].type === "old") { + currentLine++; + } else if (diffLines[currentLine].type === "same") { + remainingGroupSize--; + currentLine++; + } else if (diffLines[currentLine].type === "new") { + remainingGroupSize--; + currentLine++; + } + } + + // Adjust for the last increment. + currentLine--; + + // Calculate the end line in old content. + let oldContentLineEnd = + oldContentLineStart + + countOldContentLines(diffLines, start, currentLine) - + 1; + + return { + startLine: oldContentLineStart + offset, + endLine: oldContentLineEnd + offset, + lines, + }; +} + +/** + * Process a changed area with flexible sizing. + */ +function processFlexibleSizeGroup( + diffLines: DiffLine[], + start: number, + end: number, + offset: number, +): DiffGroup { + // Calculate the starting line in old content. + let oldContentLineStart = countOldContentLines(diffLines, 0, start - 1); + + // Calculate the end line in old content. + let oldContentLineEnd = + oldContentLineStart + countOldContentLines(diffLines, start, end) - 1; + + return { + startLine: oldContentLineStart + offset, + endLine: oldContentLineEnd + offset, + lines: diffLines.slice(start, end + 1), + }; }