MCPcopy Create free account
hub / github.com/Palm1r/QodeAssist / applyDiffToContent

Method applyDiffToContent

context/ChangesManager.cpp:1540–1650  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1538}
1539
1540bool ChangesManager::applyDiffToContent(
1541 QString &content,
1542 const DiffInfo &diffInfo,
1543 bool reverse,
1544 QString *errorMsg)
1545{
1546 LOG_MESSAGE(QString("=== Applying %1 to content ===").arg(reverse ? "REVERSE diff" : "diff"));
1547
1548 auto setError = [errorMsg](const QString &msg) {
1549 if (errorMsg) *errorMsg = msg;
1550 };
1551
1552 if (diffInfo.useFallback) {
1553 LOG_MESSAGE(" Using fallback mode (direct content replacement)");
1554
1555 QString searchContent = reverse ? diffInfo.modifiedContent : diffInfo.originalContent;
1556 QString replaceContent = reverse ? diffInfo.originalContent : diffInfo.modifiedContent;
1557
1558 int matchPos = content.indexOf(searchContent);
1559 if (matchPos != -1) {
1560 content = content.left(matchPos)
1561 + replaceContent
1562 + content.mid(matchPos + searchContent.length());
1563 setError("Applied using fallback mode (direct replacement)");
1564 LOG_MESSAGE(QString(" ✓ Fallback: Direct replacement successful at position %1").arg(matchPos));
1565 return true;
1566 } else {
1567 setError("Fallback failed: Original content not found in file");
1568 LOG_MESSAGE(" ✗ Fallback: Content not found");
1569 return false;
1570 }
1571 }
1572
1573 if (diffInfo.hunks.isEmpty()) {
1574 LOG_MESSAGE(" No hunks to apply (content unchanged)");
1575 setError("No changes to apply");
1576 return true;
1577 }
1578
1579 QStringList fileLines = content.split('\n');
1580 LOG_MESSAGE(QString(" File has %1 lines, applying %2 hunk(s)")
1581 .arg(fileLines.size()).arg(diffInfo.hunks.size()));
1582
1583 QList<DiffHunk> hunksToApply = diffInfo.hunks;
1584
1585 std::sort(hunksToApply.begin(), hunksToApply.end(),
1586 [](const DiffHunk &a, const DiffHunk &b) {
1587 return a.oldStartLine > b.oldStartLine;
1588 });
1589
1590 LOG_MESSAGE(" Hunks sorted in descending order for application");
1591
1592 int appliedHunks = 0;
1593 int failedHunks = 0;
1594
1595 for (int hunkIdx = 0; hunkIdx < hunksToApply.size(); ++hunkIdx) {
1596 const DiffHunk &hunk = hunksToApply[hunkIdx];
1597

Callers

nothing calls this directly

Calls 2

sizeMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected