Refactor applyCodeChanges and add findMatchingIndex
- Added findMatchingIndex function to find matching index in two lists. - Refactored applyCodeChanges to use findMatchingIndex. - Replaced reduce with findMatchingIndex for better readability.
This commit is contained in:
parent
ed96ab8fc4
commit
f8fe40890b
@ -1,8 +1,6 @@
|
||||
import { logger } from "./logger";
|
||||
|
||||
|
||||
|
||||
|
||||
type Action = {
|
||||
action: "delete" | "insert" | "modify";
|
||||
content?: string;
|
||||
@ -11,6 +9,22 @@ type Action = {
|
||||
new_content?: string;
|
||||
};
|
||||
|
||||
function findMatchingIndex(list1: string[], list2: string[]): number {
|
||||
for (let i = 0; i <= list1.length - list2.length; i++) {
|
||||
let isMatch = true;
|
||||
for (let j = 0; j < list2.length; j++) {
|
||||
if (list1[i + j].trim() !== list2[j].trim()) {
|
||||
isMatch = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isMatch) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
export function applyCodeChanges(originalCode: string, actionsString: string): string {
|
||||
const actions = JSON.parse(actionsString) as Array<Action>;
|
||||
|
||||
@ -23,38 +37,26 @@ export function applyCodeChanges(originalCode: string, actionsString: string): s
|
||||
|
||||
switch (action.action) {
|
||||
case 'delete':
|
||||
const deleteIndices = lines.reduce((indices, line, index) => {
|
||||
if (contentLines.includes(line.trim())) {
|
||||
indices.push(index);
|
||||
}
|
||||
return indices;
|
||||
}, [] as number[]);
|
||||
for (const deleteIndex of deleteIndices.reverse()) {
|
||||
lines.splice(deleteIndex, contentLines.length);
|
||||
// find the matching index
|
||||
const matchingIndex = findMatchingIndex(lines, contentLines);
|
||||
if (matchingIndex !== -1) {
|
||||
lines.splice(matchingIndex, contentLines.length);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'insert':
|
||||
const insertIndices = lines.reduce((indices, line, index) => {
|
||||
if (insertAfterLines.includes(line.trim())) {
|
||||
indices.push(index);
|
||||
}
|
||||
return indices;
|
||||
}, [] as number[]);
|
||||
for (const insertIndex of insertIndices.reverse()) {
|
||||
lines.splice(insertIndex + 1, 0, ...contentLines);
|
||||
// find the matching index
|
||||
const matchingIndex2 = findMatchingIndex(lines, insertAfterLines);
|
||||
if (matchingIndex2 !== -1) {
|
||||
lines.splice(matchingIndex2 + 1, 0, ...contentLines);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'modify':
|
||||
const modifyIndices = lines.reduce((indices, line, index) => {
|
||||
if (originalContentLines.includes(line.trim())) {
|
||||
indices.push(index);
|
||||
}
|
||||
return indices;
|
||||
}, [] as number[]);
|
||||
for (const modifyIndex of modifyIndices) {
|
||||
lines.splice(modifyIndex, originalContentLines.length, ...action.new_content!.split('\n'));
|
||||
// find the matching index
|
||||
const matchingIndex3 = findMatchingIndex(lines, originalContentLines);
|
||||
if (matchingIndex3 !== -1) {
|
||||
lines.splice(matchingIndex3, originalContentLines.length, ...action.new_content!.split('\n'));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user