| 296 | } |
| 297 | |
| 298 | bool ChangesManager::performFileEdit( |
| 299 | const QString &filePath, const QString &oldContent, const QString &newContent, QString *errorMsg) |
| 300 | { |
| 301 | auto setError = [errorMsg](const QString &msg) { |
| 302 | if (errorMsg) *errorMsg = msg; |
| 303 | }; |
| 304 | |
| 305 | auto editors = Core::EditorManager::visibleEditors(); |
| 306 | for (auto *editor : editors) { |
| 307 | if (!editor || !editor->document()) { |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | QString editorPath = editor->document()->filePath().toFSPathString(); |
| 312 | if (editorPath == filePath) { |
| 313 | QByteArray contentBytes = editor->document()->contents(); |
| 314 | QString currentContent = QString::fromUtf8(contentBytes); |
| 315 | |
| 316 | if (oldContent.isEmpty()) { |
| 317 | if (auto *textEditor |
| 318 | = qobject_cast<TextEditor::TextDocument *>(editor->document())) { |
| 319 | QTextDocument *doc = textEditor->document(); |
| 320 | |
| 321 | QTextCursor cursor(doc); |
| 322 | cursor.beginEditBlock(); |
| 323 | cursor.movePosition(QTextCursor::End); |
| 324 | cursor.insertText(newContent); |
| 325 | cursor.endEditBlock(); |
| 326 | |
| 327 | LOG_MESSAGE(QString("Appended to open editor: %1").arg(filePath)); |
| 328 | setError("Applied successfully (appended to end of file)"); |
| 329 | return true; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | int matchPos = currentContent.indexOf(oldContent); |
| 334 | if (matchPos != -1) { |
| 335 | if (auto *textEditor |
| 336 | = qobject_cast<TextEditor::TextDocument *>(editor->document())) { |
| 337 | QTextDocument *doc = textEditor->document(); |
| 338 | |
| 339 | QTextCursor cursor(doc); |
| 340 | cursor.beginEditBlock(); |
| 341 | cursor.setPosition(matchPos); |
| 342 | cursor.setPosition(matchPos + oldContent.length(), QTextCursor::KeepAnchor); |
| 343 | cursor.removeSelectedText(); |
| 344 | cursor.insertText(newContent); |
| 345 | cursor.endEditBlock(); |
| 346 | |
| 347 | LOG_MESSAGE(QString("Updated open editor (exact match): %1").arg(filePath)); |
| 348 | setError("Applied successfully (exact match)"); |
| 349 | return true; |
| 350 | } |
| 351 | } else { |
| 352 | double similarity = 0.0; |
| 353 | QString matchedContent = findBestMatch(currentContent, oldContent, 0.82, &similarity); |
| 354 | if (!matchedContent.isEmpty()) { |
| 355 | matchPos = currentContent.indexOf(matchedContent); |
nothing calls this directly
no test coverage detected