From f8fe40890b42782995f90a5f816b3e18045cc819 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Thu, 18 May 2023 21:43:05 +0800 Subject: [PATCH] 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. --- src/util/appyDiff.ts | 138 ++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/src/util/appyDiff.ts b/src/util/appyDiff.ts index ac0beb2..070cf99 100644 --- a/src/util/appyDiff.ts +++ b/src/util/appyDiff.ts @@ -1,8 +1,6 @@ import { logger } from "./logger"; - - type Action = { action: "delete" | "insert" | "modify"; content?: string; @@ -11,86 +9,90 @@ 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; + const actions = JSON.parse(actionsString) as Array; - const lines = originalCode.split('\n'); + const lines = originalCode.split('\n'); - for (const action of actions) { - const contentLines = action.content?.split('\n') || []; - const insertAfterLines = action.insert_after?.split('\n') || []; - const originalContentLines = action.original_content?.split('\n') || []; + for (const action of actions) { + const contentLines = action.content?.split('\n') || []; + const insertAfterLines = action.insert_after?.split('\n') || []; + const originalContentLines = action.original_content?.split('\n') || []; - 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); - } - break; + switch (action.action) { + case 'delete': + // 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); - } - break; + case 'insert': + // 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')); - } - break; - } - } + case 'modify': + // find the matching index + const matchingIndex3 = findMatchingIndex(lines, originalContentLines); + if (matchingIndex3 !== -1) { + lines.splice(matchingIndex3, originalContentLines.length, ...action.new_content!.split('\n')); + } + break; + } + } - return lines.join('\n'); + return lines.join('\n'); } export function isValidActionString(actionString: string): boolean { - try { - const actions = JSON.parse(actionString) as Array; + try { + const actions = JSON.parse(actionString) as Array; - for (const action of actions) { - if (!["delete", "insert", "modify"].includes(action.action)) { - return false; - } + for (const action of actions) { + if (!["delete", "insert", "modify"].includes(action.action)) { + return false; + } - if (action.action === "delete" && !action.content) { - logger.channel()?.error(`Invalid action string: ${action}`); - return false; - } + if (action.action === "delete" && !action.content) { + logger.channel()?.error(`Invalid action string: ${action}`); + return false; + } - if (action.action === "insert" && (!action.content || !action.insert_after)) { - logger.channel()?.error(`Invalid action string: ${action}`); - return false; - } + if (action.action === "insert" && (!action.content || !action.insert_after)) { + logger.channel()?.error(`Invalid action string: ${action}`); + return false; + } - if (action.action === "modify" && (!action.original_content || !action.new_content)) { - logger.channel()?.error(`Invalid action string: ${action}`); - return false; - } - } + if (action.action === "modify" && (!action.original_content || !action.new_content)) { + logger.channel()?.error(`Invalid action string: ${action}`); + return false; + } + } - return true; - } catch (error) { - return false; - } + return true; + } catch (error) { + return false; + } } \ No newline at end of file