Merge pull request #83 from covespace/fix_diff_instructions
Fix diff instructions
This commit is contained in:
commit
9aba48d09e
@ -1,8 +1,6 @@
|
||||
import { logger } from "./logger";
|
||||
|
||||
|
||||
|
||||
|
||||
type Action = {
|
||||
action: "delete" | "insert" | "modify";
|
||||
content?: string;
|
||||
@ -11,67 +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<Action>;
|
||||
const actions = JSON.parse(actionsString) as Array<Action>;
|
||||
|
||||
const lines = originalCode.split('\n');
|
||||
const lines = originalCode.split('\n');
|
||||
|
||||
for (const action of actions) {
|
||||
switch (action.action) {
|
||||
case 'delete':
|
||||
const deleteIndex = lines.findIndex(line => line.trim() === action.content);
|
||||
if (deleteIndex !== -1) {
|
||||
lines.splice(deleteIndex, 1);
|
||||
}
|
||||
break;
|
||||
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') || [];
|
||||
|
||||
case 'insert':
|
||||
const insertIndex = lines.findIndex(line => line.trim() === action.insert_after);
|
||||
if (insertIndex !== -1) {
|
||||
lines.splice(insertIndex + 1, 0, action.content!);
|
||||
}
|
||||
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 'modify':
|
||||
const modifyIndex = lines.findIndex(line => line.trim() === action.original_content);
|
||||
if (modifyIndex !== -1) {
|
||||
lines[modifyIndex] = lines[modifyIndex].replace(lines[modifyIndex].trim(), action.new_content!);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
case 'insert':
|
||||
// find the matching index
|
||||
const matchingIndex2 = findMatchingIndex(lines, insertAfterLines);
|
||||
if (matchingIndex2 !== -1) {
|
||||
lines.splice(matchingIndex2 + 1, 0, ...contentLines);
|
||||
}
|
||||
break;
|
||||
|
||||
return lines.join('\n');
|
||||
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');
|
||||
}
|
||||
|
||||
|
||||
export function isValidActionString(actionString: string): boolean {
|
||||
try {
|
||||
const actions = JSON.parse(actionString) as Array<Action>;
|
||||
try {
|
||||
const actions = JSON.parse(actionString) as Array<Action>;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user