(edit: WorkspaceEdit)
| 198 | } |
| 199 | |
| 200 | async applyEdit(edit: WorkspaceEdit): Promise<boolean> { |
| 201 | // In CLI mode, we need to apply the edits to the actual files |
| 202 | try { |
| 203 | for (const [uri, edits] of edit.entries()) { |
| 204 | let filePath = uri.fsPath |
| 205 | |
| 206 | // On Windows, strip leading slash if present (e.g., /C:/path becomes C:/path) |
| 207 | if (process.platform === "win32" && filePath.startsWith("/")) { |
| 208 | filePath = filePath.slice(1) |
| 209 | } |
| 210 | |
| 211 | let content = "" |
| 212 | |
| 213 | // Read existing content if file exists |
| 214 | try { |
| 215 | content = fs.readFileSync(filePath, "utf-8") |
| 216 | } catch { |
| 217 | // File doesn't exist, start with empty content |
| 218 | } |
| 219 | |
| 220 | // Apply edits in reverse order to maintain correct positions |
| 221 | const sortedEdits = edits.sort((a, b) => { |
| 222 | const lineDiff = b.range.start.line - a.range.start.line |
| 223 | if (lineDiff !== 0) return lineDiff |
| 224 | return b.range.start.character - a.range.start.character |
| 225 | }) |
| 226 | |
| 227 | const lines = content.split("\n") |
| 228 | for (const textEdit of sortedEdits) { |
| 229 | const startLine = textEdit.range.start.line |
| 230 | const startChar = textEdit.range.start.character |
| 231 | const endLine = textEdit.range.end.line |
| 232 | const endChar = textEdit.range.end.character |
| 233 | |
| 234 | if (startLine === endLine) { |
| 235 | // Single line edit |
| 236 | const line = lines[startLine] || "" |
| 237 | lines[startLine] = line.substring(0, startChar) + textEdit.newText + line.substring(endChar) |
| 238 | } else { |
| 239 | // Multi-line edit |
| 240 | const firstLine = lines[startLine] || "" |
| 241 | const lastLine = lines[endLine] || "" |
| 242 | const newContent = |
| 243 | firstLine.substring(0, startChar) + textEdit.newText + lastLine.substring(endChar) |
| 244 | lines.splice(startLine, endLine - startLine + 1, newContent) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Write back to file |
| 249 | const newContent = lines.join("\n") |
| 250 | fs.writeFileSync(filePath, newContent, "utf-8") |
| 251 | |
| 252 | // Update the in-memory document object to reflect the new content |
| 253 | // This is critical for CLI mode where DiffViewProvider reads from the document object |
| 254 | const document = this.textDocuments.find((doc: TextDocument) => doc.uri.fsPath === filePath) |
| 255 | if (document) { |
| 256 | const newLines = newContent.split("\n") |
| 257 |
no test coverage detected