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";
|
import { logger } from "./logger";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type Action = {
|
type Action = {
|
||||||
action: "delete" | "insert" | "modify";
|
action: "delete" | "insert" | "modify";
|
||||||
content?: string;
|
content?: string;
|
||||||
@ -11,6 +9,22 @@ type Action = {
|
|||||||
new_content?: string;
|
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 {
|
export function applyCodeChanges(originalCode: string, actionsString: string): string {
|
||||||
const actions = JSON.parse(actionsString) as Array<Action>;
|
const actions = JSON.parse(actionsString) as Array<Action>;
|
||||||
|
|
||||||
@ -23,38 +37,26 @@ export function applyCodeChanges(originalCode: string, actionsString: string): s
|
|||||||
|
|
||||||
switch (action.action) {
|
switch (action.action) {
|
||||||
case 'delete':
|
case 'delete':
|
||||||
const deleteIndices = lines.reduce((indices, line, index) => {
|
// find the matching index
|
||||||
if (contentLines.includes(line.trim())) {
|
const matchingIndex = findMatchingIndex(lines, contentLines);
|
||||||
indices.push(index);
|
if (matchingIndex !== -1) {
|
||||||
}
|
lines.splice(matchingIndex, contentLines.length);
|
||||||
return indices;
|
|
||||||
}, [] as number[]);
|
|
||||||
for (const deleteIndex of deleteIndices.reverse()) {
|
|
||||||
lines.splice(deleteIndex, contentLines.length);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'insert':
|
case 'insert':
|
||||||
const insertIndices = lines.reduce((indices, line, index) => {
|
// find the matching index
|
||||||
if (insertAfterLines.includes(line.trim())) {
|
const matchingIndex2 = findMatchingIndex(lines, insertAfterLines);
|
||||||
indices.push(index);
|
if (matchingIndex2 !== -1) {
|
||||||
}
|
lines.splice(matchingIndex2 + 1, 0, ...contentLines);
|
||||||
return indices;
|
|
||||||
}, [] as number[]);
|
|
||||||
for (const insertIndex of insertIndices.reverse()) {
|
|
||||||
lines.splice(insertIndex + 1, 0, ...contentLines);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'modify':
|
case 'modify':
|
||||||
const modifyIndices = lines.reduce((indices, line, index) => {
|
// find the matching index
|
||||||
if (originalContentLines.includes(line.trim())) {
|
const matchingIndex3 = findMatchingIndex(lines, originalContentLines);
|
||||||
indices.push(index);
|
if (matchingIndex3 !== -1) {
|
||||||
}
|
lines.splice(matchingIndex3, originalContentLines.length, ...action.new_content!.split('\n'));
|
||||||
return indices;
|
|
||||||
}, [] as number[]);
|
|
||||||
for (const modifyIndex of modifyIndices) {
|
|
||||||
lines.splice(modifyIndex, originalContentLines.length, ...action.new_content!.split('\n'));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user