| 91 | } |
| 92 | |
| 93 | async open(relPath: string): Promise<void> { |
| 94 | this.relPath = relPath |
| 95 | const fileExists = this.editType === "modify" |
| 96 | const absolutePath = path.resolve(this.cwd, relPath) |
| 97 | this.isEditing = true |
| 98 | |
| 99 | // Capture the current scroll position before we close the tab so we can |
| 100 | // restore it after saving/reverting. |
| 101 | const existingEditor = vscode.window.visibleTextEditors.find( |
| 102 | (e) => e.document.uri.scheme === "file" && arePathsEqual(e.document.uri.fsPath, absolutePath), |
| 103 | ) |
| 104 | this.preEditScrollLine = existingEditor?.visibleRanges?.[0]?.start.line |
| 105 | |
| 106 | // If the file is already open, ensure it's not dirty before getting its |
| 107 | // contents. |
| 108 | if (fileExists) { |
| 109 | const existingDocument = vscode.workspace.textDocuments.find( |
| 110 | (doc) => doc.uri.scheme === "file" && arePathsEqual(doc.uri.fsPath, absolutePath), |
| 111 | ) |
| 112 | |
| 113 | if (existingDocument && existingDocument.isDirty) { |
| 114 | await existingDocument.save() |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Get diagnostics before editing the file, we'll compare to diagnostics |
| 119 | // after editing to see if cline needs to fix anything. |
| 120 | this.preDiagnostics = vscode.languages.getDiagnostics() |
| 121 | |
| 122 | if (fileExists) { |
| 123 | this.originalContent = await fs.readFile(absolutePath, "utf-8") |
| 124 | } else { |
| 125 | this.originalContent = "" |
| 126 | } |
| 127 | |
| 128 | // For new files, create any necessary directories and keep track of new |
| 129 | // directories to delete if the user denies the operation. |
| 130 | this.createdDirs = await createDirectoriesForFile(absolutePath) |
| 131 | |
| 132 | // Make sure the file exists before we open it. |
| 133 | if (!fileExists) { |
| 134 | await fs.writeFile(absolutePath, "") |
| 135 | } |
| 136 | |
| 137 | // If the file was already open, close it (must happen after showing the |
| 138 | // diff view since if it's the only tab the column will close). |
| 139 | this.documentWasOpen = false |
| 140 | this.documentWasPinned = false |
| 141 | |
| 142 | // Close the tab if it's open (it's already saved above). |
| 143 | const tabs = vscode.window.tabGroups.all |
| 144 | .map((tg) => tg.tabs) |
| 145 | .flat() |
| 146 | .filter( |
| 147 | (tab) => |
| 148 | tab.input instanceof vscode.TabInputText && |
| 149 | tab.input.uri.scheme === "file" && |
| 150 | arePathsEqual(tab.input.uri.fsPath, absolutePath), |