summaryrefslogtreecommitdiff
path: root/extension/src/diffs.ts
blob: 4c077a250323f662e2b737cc509672ee286aa8c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import * as os from "os";
import * as path from "path";
import * as fs from "fs";
import * as vscode from "vscode";
import { extensionContext, ideProtocolClient } from "./activation/activate";
import { getMetaKeyLabel } from "./util/util";
import { devDataPath } from "./activation/environmentSetup";
import { uriFromFilePath } from "./util/vscode";

interface DiffInfo {
  originalFilepath: string;
  newFilepath: string;
  editor?: vscode.TextEditor;
  step_index: number;
  range: vscode.Range;
}

async function readFile(path: string): Promise<string> {
  return await vscode.workspace.fs
    .readFile(uriFromFilePath(path))
    .then((bytes) => new TextDecoder().decode(bytes));
}

async function writeFile(uri: vscode.Uri, contents: string) {
  await vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(contents));
}

// THIS IS LOCAL
export const DIFF_DIRECTORY = path
  .join(os.homedir(), ".continue", "diffs")
  .replace(/^C:/, "c:");

class DiffManager {
  // Create a temporary file in the global .continue directory which displays the updated version
  // Doing this because virtual files are read-only
  private diffs: Map<string, DiffInfo> = new Map();

  diffAtNewFilepath(newFilepath: string): DiffInfo | undefined {
    return this.diffs.get(newFilepath);
  }

  private async setupDirectory() {
    // Make sure the diff directory exists
    if (!fs.existsSync(DIFF_DIRECTORY)) {
      fs.mkdirSync(DIFF_DIRECTORY, {
        recursive: true,
      });
    }
  }

  constructor() {
    this.setupDirectory();

    // Listen for file closes, and if it's a diff file, clean up
    vscode.workspace.onDidCloseTextDocument((document) => {
      const newFilepath = document.uri.fsPath;
      const diffInfo = this.diffs.get(newFilepath);
      if (diffInfo) {
        this.cleanUpDiff(diffInfo, false);
      }
    });
  }

  private escapeFilepath(filepath: string): string {
    return filepath
      .replace(/\//g, "$forwardslash$")
      .replace(/\\/g, "$backslash$");
  }

  private remoteTmpDir: string = "/tmp/continue";
  private getNewFilepath(originalFilepath: string): string {
    if (vscode.env.remoteName) {
      // If we're in a remote, use the remote's temp directory
      // Doing this because there's no easy way to find the home directory,
      // and there aren't write permissions to the root directory
      // and writing these to local causes separate issues
      // because the vscode.diff command will always try to read from remote
      vscode.workspace.fs.createDirectory(uriFromFilePath(this.remoteTmpDir));
      return path.join(
        this.remoteTmpDir,
        this.escapeFilepath(originalFilepath)
      );
    }
    return path.join(DIFF_DIRECTORY, this.escapeFilepath(originalFilepath));
  }

  private async openDiffEditor(
    originalFilepath: string,
    newFilepath: string
  ): Promise<vscode.TextEditor | undefined> {
    // If the file doesn't yet exist or the basename is a single digit number (vscode terminal), don't open the diff editor
    try {
      await vscode.workspace.fs.stat(uriFromFilePath(newFilepath));
    } catch (e) {
      console.log("File doesn't exist, not opening diff editor", e);
      return undefined;
    }
    if (path.basename(originalFilepath).match(/^\d$/)) {
      return undefined;
    }

    const rightUri = uriFromFilePath(newFilepath);
    const leftUri = uriFromFilePath(originalFilepath);
    const title = "Continue Diff";
    console.log(
      "Opening diff window with ",
      leftUri,
      rightUri,
      title,
      newFilepath,
      originalFilepath
    );
    vscode.commands.executeCommand("vscode.diff", leftUri, rightUri, title);

    const editor = vscode.window.activeTextEditor;
    if (!editor) {
      return;
    }

    // Change the vscode setting to allow codeLens in diff editor
    vscode.workspace
      .getConfiguration("diffEditor", editor.document.uri)
      .update("codeLens", true, vscode.ConfigurationTarget.Global);

    if (
      extensionContext?.globalState.get<boolean>(
        "continue.showDiffInfoMessage"
      ) !== false
    ) {
      vscode.window
        .showInformationMessage(
          `Accept (${getMetaKeyLabel()}⇧↩) or reject (${getMetaKeyLabel()}⇧⌫) at the top of the file.`,
          "Got it",
          "Don't show again"
        )
        .then((selection) => {
          if (selection === "Don't show again") {
            // Get the global state
            extensionContext?.globalState.update(
              "continue.showDiffInfoMessage",
              false
            );
          }
        });
    }

    return editor;
  }

  private _findFirstDifferentLine(contentA: string, contentB: string): number {
    const linesA = contentA.split("\n");
    const linesB = contentB.split("\n");
    for (let i = 0; i < linesA.length && i < linesB.length; i++) {
      if (linesA[i] !== linesB[i]) {
        return i;
      }
    }
    return 0;
  }

  async writeDiff(
    originalFilepath: string,
    newContent: string,
    step_index: number
  ): Promise<string> {
    await this.setupDirectory();

    // Create or update existing diff
    const newFilepath = this.getNewFilepath(originalFilepath);
    await writeFile(uriFromFilePath(newFilepath), newContent);

    // Open the diff editor if this is a new diff
    if (!this.diffs.has(newFilepath)) {
      // Figure out the first line that is different
      const oldContent = await readFile(originalFilepath);
      const line = this._findFirstDifferentLine(oldContent, newContent);

      const diffInfo: DiffInfo = {
        originalFilepath,
        newFilepath,
        step_index,
        range: new vscode.Range(line, 0, line + 1, 0),
      };
      this.diffs.set(newFilepath, diffInfo);
    }

    // Open the editor if it hasn't been opened yet
    const diffInfo = this.diffs.get(newFilepath);
    if (diffInfo && !diffInfo?.editor) {
      diffInfo.editor = await this.openDiffEditor(
        originalFilepath,
        newFilepath
      );
      this.diffs.set(newFilepath, diffInfo);
    }

    vscode.commands.executeCommand(
      "workbench.action.files.revert",
      uriFromFilePath(newFilepath)
    );

    return newFilepath;
  }

  cleanUpDiff(diffInfo: DiffInfo, hideEditor: boolean = true) {
    // Close the editor, remove the record, delete the file
    if (hideEditor && diffInfo.editor) {
      try {
        vscode.window.showTextDocument(diffInfo.editor.document);
        vscode.commands.executeCommand("workbench.action.closeActiveEditor");
      } catch {}
    }
    this.diffs.delete(diffInfo.newFilepath);
    vscode.workspace.fs.delete(uriFromFilePath(diffInfo.newFilepath));
  }

  private inferNewFilepath() {
    const activeEditorPath =
      vscode.window.activeTextEditor?.document.uri.fsPath;
    if (activeEditorPath && path.dirname(activeEditorPath) === DIFF_DIRECTORY) {
      return activeEditorPath;
    }
    const visibleEditors = vscode.window.visibleTextEditors.map(
      (editor) => editor.document.uri.fsPath
    );
    for (const editorPath of visibleEditors) {
      if (path.dirname(editorPath) === DIFF_DIRECTORY) {
        for (const otherEditorPath of visibleEditors) {
          if (
            path.dirname(otherEditorPath) !== DIFF_DIRECTORY &&
            this.getNewFilepath(otherEditorPath) === editorPath
          ) {
            return editorPath;
          }
        }
      }
    }

    if (this.diffs.size === 1) {
      return Array.from(this.diffs.keys())[0];
    }
    return undefined;
  }

  async acceptDiff(newFilepath?: string) {
    // When coming from a keyboard shortcut, we have to infer the newFilepath from visible text editors
    if (!newFilepath) {
      newFilepath = this.inferNewFilepath();
    }
    if (!newFilepath) {
      console.log("No newFilepath provided to accept the diff");
      return;
    }
    // Get the diff info, copy new file to original, then delete from record and close the corresponding editor
    const diffInfo = this.diffs.get(newFilepath);
    if (!diffInfo) {
      console.log("No corresponding diffInfo found for newFilepath");
      return;
    }

    // Save the right-side file, then copy over to original
    vscode.workspace.textDocuments
      .find((doc) => doc.uri.fsPath === newFilepath)
      ?.save()
      .then(async () => {
        await writeFile(
          uriFromFilePath(diffInfo.originalFilepath),
          await readFile(diffInfo.newFilepath)
        );
        this.cleanUpDiff(diffInfo);
      });

    await recordAcceptReject(true, diffInfo);
  }

  async rejectDiff(newFilepath?: string) {
    // If no newFilepath is provided and there is only one in the dictionary, use that
    if (!newFilepath) {
      newFilepath = this.inferNewFilepath();
    }
    if (!newFilepath) {
      console.log(
        "No newFilepath provided to reject the diff, diffs.size was",
        this.diffs.size
      );
      return;
    }
    const diffInfo = this.diffs.get(newFilepath);
    if (!diffInfo) {
      console.log("No corresponding diffInfo found for newFilepath");
      return;
    }

    // Stop the step at step_index in case it is still streaming
    ideProtocolClient.deleteAtIndex(diffInfo.step_index);

    vscode.workspace.textDocuments
      .find((doc) => doc.uri.fsPath === newFilepath)
      ?.save()
      .then(() => {
        this.cleanUpDiff(diffInfo);
      });

    await recordAcceptReject(false, diffInfo);
  }
}

export const diffManager = new DiffManager();

async function recordAcceptReject(accepted: boolean, diffInfo: DiffInfo) {
  const devDataDir = devDataPath();
  const suggestionsPath = path.join(devDataDir, "suggestions.json");

  // Initialize suggestions list
  let suggestions = [];

  // Check if suggestions.json exists
  try {
    const rawData = await readFile(suggestionsPath);
    suggestions = JSON.parse(rawData);
  } catch {}

  // Add the new suggestion to the list
  suggestions.push({
    accepted,
    timestamp: Date.now(),
    suggestion: diffInfo.originalFilepath,
  });

  // Send the suggestion to the server
  // ideProtocolClient.sendAcceptRejectSuggestion(accepted);

  // Write the updated suggestions back to the file
  await writeFile(
    vscode.Uri.file(suggestionsPath),
    JSON.stringify(suggestions, null, 4)
  );
}

export async function acceptDiffCommand(newFilepath?: string) {
  await diffManager.acceptDiff(newFilepath);
  ideProtocolClient.sendAcceptRejectDiff(true);
}

export async function rejectDiffCommand(newFilepath?: string) {
  await diffManager.rejectDiff(newFilepath);
  ideProtocolClient.sendAcceptRejectDiff(false);
}