continue-custom/core/nextEdit/diff/diff.ts
2025-06-26 16:10:08 -07:00

114 lines
3.2 KiB
TypeScript

import { DiffLine } from "../..";
/**
* Given a diff of two editable regions, get the offset position at the last new line inside the editable region.
* @param diffLines Result of myersDiff.
* @param lineContentAtCursorPos Content of the line at cursor position.
* @param lineOffsetAtCursorPos Offset of the line at cursor position compared to the start of the editable region.
* @returns Offset position at last new line inside the editable region.
*/
export function getOffsetPositionAtLastNewLine(
diffLines: DiffLine[],
lineContentAtCursorPos: string,
lineOffsetAtCursorPos: number,
): {
line: number;
character: number;
} {
let lastNewLineContent = "";
let lineOffset = -1;
let currentResultLine = 0;
let hasChanges = false;
// Build the string while tracking line numbers in the result
diffLines.reduce((acc, curr, i) => {
// Add the current line to our result
acc += curr.line;
// Add newline if not the last line
if (i < diffLines.length - 1) {
acc += "\n";
}
// If this is a "new" or "same" line, it will be part of the result
if (curr.type === "new" || curr.type === "same") {
if (curr.type === "new") {
// If it's a new line, update our tracking
lastNewLineContent = curr.line;
lineOffset = currentResultLine;
hasChanges = true;
}
// Increment our position in the result
currentResultLine++;
}
return acc;
}, "");
// If nothing has changed, return the original position
if (!hasChanges) {
lineOffset = lineOffsetAtCursorPos;
lastNewLineContent = lineContentAtCursorPos;
}
// Calculate the character position for the end of the last relevant line
const endOfCharPos = lastNewLineContent.length;
return {
line: lineOffset,
character: endOfCharPos,
};
}
export function getRenderableDiffWithGutterAnnotations(
diffLines: DiffLine[],
lineContentAtCursorPos: string,
lineOffsetAtCursorPos: number,
): {
offset: {
line: number;
character: number;
};
} {
let lastNewLineContent = "";
let lineOffset = -1;
let currentResultLine = 0;
let hasChanges = false;
// Build the string while tracking line numbers in the result
diffLines.reduce((acc, curr, i) => {
// Add the current line to our result
acc += curr.line;
// Add newline if not the last line
if (i < diffLines.length - 1) {
acc += "\n";
}
// If this is a "new" or "same" line, it will be part of the result
if (curr.type === "new" || curr.type === "same") {
if (curr.type === "new") {
// If it's a new line, update our tracking
lastNewLineContent = curr.line;
lineOffset = currentResultLine;
hasChanges = true;
}
// Increment our position in the result
currentResultLine++;
}
return acc;
}, "");
// If nothing has changed, return the original position
if (!hasChanges) {
lineOffset = lineOffsetAtCursorPos;
lastNewLineContent = lineContentAtCursorPos;
}
// Calculate the character position for the end of the last relevant line
const endOfCharPos = lastNewLineContent.length;
return {
offset: {
line: lineOffset,
character: endOfCharPos,
},
};
}