Răsfoiți Sursa

Difference data is reflected in UnifiedMergeView

Shun Miyazawa 1 an în urmă
părinte
comite
ca49172728

+ 64 - 3
apps/app/src/features/openai/client/services/editor-assistant.ts

@@ -40,8 +40,62 @@ type DetectedDiff = Array<{
   id: string,
 }>
 
+const insertTextAtLine = (ytext, lineNumber: number, textToInsert: string): void => {
+  // Get the entire text content
+  const content = ytext.toString();
+
+  // Split by newlines to get all lines
+  const lines = content.split('\n');
+
+  // Calculate the index position for insertion
+  let insertPosition = 0;
+
+  // Sum the length of all lines before the target line (plus newline characters)
+  for (let i = 0; i < lineNumber && i < lines.length; i++) {
+    insertPosition += lines[i].length + 1; // +1 for the newline character
+  }
+
+  // Insert the text at the calculated position
+  ytext.insert(insertPosition, textToInsert);
+};
+
+
+const getLineInfo = (ytext, lineNumber: number): { text: string, startIndex: number, endIndex: number } | null => {
+  // Get the entire text content
+  const content = ytext.toString();
+
+  // Split by newlines to get all lines
+  const lines = content.split('\n');
+
+  // Check if the requested line exists
+  if (lineNumber < 0 || lineNumber >= lines.length) {
+    return null; // Line doesn't exist
+  }
+
+  // Get the text of the specified line
+  const text = lines[lineNumber];
+
+  // Calculate the start index of the line
+  let startIndex = 0;
+  for (let i = 0; i < lineNumber; i++) {
+    startIndex += lines[i].length + 1; // +1 for the newline character
+  }
+
+  // Calculate the end index of the line (exclusive)
+  const endIndex = startIndex + text.length;
+
+  // Return comprehensive line information
+  return {
+    text,
+    startIndex,
+    endIndex,
+  };
+};
+
+
 export const useEditorAssistant = (): {postMessage: PostMessage, processMessage: ProcessMessage, accept: () => void, reject: () => void } => {
-  const positionRef = useRef<number>(0);
+  // const positionRef = useRef<number>(0);
+  const lineRef = useRef<number>(0);
 
   const [detectedDiff, setDetectedDiff] = useState<DetectedDiff>();
 
@@ -121,7 +175,13 @@ export const useEditorAssistant = (): {postMessage: PostMessage, processMessage:
       ydocs.secondaryDoc.transact(() => {
         pendingDetectedDiff.forEach((detectedDiff) => {
           if (isReplaceDiff(detectedDiff.data)) {
-            // TODO: https://redmine.weseek.co.jp/issues/164330
+            const lineInfo = getLineInfo(ytext, lineRef.current);
+            if (lineInfo != null && lineInfo.text !== detectedDiff.data.diff.replace) {
+              ytext.delete(lineInfo.startIndex, lineInfo.text.length);
+              insertTextAtLine(ytext, lineRef.current, detectedDiff.data.diff.replace);
+            }
+
+            lineRef.current += 1;
           }
           // if (isInsertDiff(detectedDiff.data)) {
           //   ytext.insert(positionRef.current, detectedDiff.data.diff.insert);
@@ -149,7 +209,8 @@ export const useEditorAssistant = (): {postMessage: PostMessage, processMessage:
       // Set detectedDiff to undefined after applying all detectedDiff to secondaryDoc
       if (detectedDiff?.filter(detectedDiff => detectedDiff.applied === false).length === 0) {
         setDetectedDiff(undefined);
-        positionRef.current = 0;
+        lineRef.current = 0;
+        // positionRef.current = 0;
       }
     }
   }, [codeMirrorEditor, detectedDiff, ydocs?.secondaryDoc]);