From 50adabaef68917a65b123dbf6026c410b8c5a006 Mon Sep 17 00:00:00 2001
From: Nate Sesti <sestinj@gmail.com>
Date: Tue, 11 Jul 2023 00:51:42 -0700
Subject: don't allow /edit in diff editor, save diff editor

---
 continuedev/src/continuedev/steps/main.py | 10 +++++++++-
 extension/src/diffs.ts                    | 17 ++++++++++++-----
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/continuedev/src/continuedev/steps/main.py b/continuedev/src/continuedev/steps/main.py
index 4f543022..2a8cd250 100644
--- a/continuedev/src/continuedev/steps/main.py
+++ b/continuedev/src/continuedev/steps/main.py
@@ -10,7 +10,7 @@ from ..models.filesystem import RangeInFile, RangeInFileWithContents
 from ..core.observation import Observation, TextObservation, TracebackObservation
 from ..libs.llm.prompt_utils import MarkdownStyleEncoderDecoder
 from textwrap import dedent
-from ..core.main import Step
+from ..core.main import ContinueCustomException, Step
 from ..core.sdk import ContinueSDK, Models
 from ..core.observation import Observation
 import subprocess
@@ -251,6 +251,9 @@ class EditHighlightedCodeStep(Step):
             highlighted_code = await sdk.ide.getHighlightedCode()
             if highlighted_code is not None:
                 for rif in highlighted_code:
+                    if os.path.dirname(rif.filepath) == os.path.expanduser(os.path.join("~", ".continue", "diffs")):
+                        raise ContinueCustomException(
+                            message="Please accept or reject the change before making another edit in this file.", title="Accept/Reject First")
                     if rif.range.start == rif.range.end:
                         range_in_files.append(
                             RangeInFileWithContents.from_range_in_file(rif, ""))
@@ -278,6 +281,11 @@ class EditHighlightedCodeStep(Step):
             filepath=x.filepath, range=x.range
         ), range_in_files))
 
+        for range_in_file in range_in_files:
+            if os.path.dirname(range_in_file.filepath) == os.path.expanduser(os.path.join("~", ".continue", "diffs")):
+                self.description = "Please accept or reject the change before making another edit in this file."
+                return
+
         await sdk.run_step(DefaultModelEditCodeStep(user_input=self.user_input, range_in_files=range_in_files))
 
 
diff --git a/extension/src/diffs.ts b/extension/src/diffs.ts
index b9ef8384..1dc292e1 100644
--- a/extension/src/diffs.ts
+++ b/extension/src/diffs.ts
@@ -132,11 +132,18 @@ class DiffManager {
       console.log("No corresponding diffInfo found for newFilepath");
       return;
     }
-    fs.writeFileSync(
-      diffInfo.originalFilepath,
-      fs.readFileSync(diffInfo.newFilepath)
-    );
-    this.cleanUpDiff(diffInfo);
+
+    // Save the right-side file, then copy over to original
+    vscode.workspace.textDocuments
+      .find((doc) => doc.uri.fsPath === newFilepath)
+      ?.save()
+      .then(() => {
+        fs.writeFileSync(
+          diffInfo.originalFilepath,
+          fs.readFileSync(diffInfo.newFilepath)
+        );
+        this.cleanUpDiff(diffInfo);
+      });
   }
 
   rejectDiff(newFilepath?: string) {
-- 
cgit v1.2.3-70-g09d2