MCPcopy Create free account
hub / github.com/Codeya-IDE/deepin-ide / merge

Method merge

src/plugins/git/utils/differ.cpp:1268–1336  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1266}
1267
1268QList<Diff> Differ::merge(const QList<Diff> &diffList)
1269{
1270 QString lastDelete;
1271 QString lastInsert;
1272 QList<Diff> newDiffList;
1273 for (int i = 0; i <= diffList.size(); i++) {
1274 Diff diff = i < diffList.size()
1275 ? diffList.at(i)
1276 : Diff(Diff::Equal); // dummy, ensure we process to the end
1277 // even when diffList doesn't end with equality
1278 if (diff.command == Diff::Delete) {
1279 lastDelete += diff.text;
1280 } else if (diff.command == Diff::Insert) {
1281 lastInsert += diff.text;
1282 } else { // Diff::Equal
1283 if (!(lastDelete.isEmpty() && lastInsert.isEmpty())) {
1284
1285 // common prefix
1286 const int prefixCount = commonPrefix(lastDelete, lastInsert);
1287 if (prefixCount) {
1288 const QString prefix = lastDelete.left(prefixCount);
1289 lastDelete = lastDelete.mid(prefixCount);
1290 lastInsert = lastInsert.mid(prefixCount);
1291
1292 if (!newDiffList.isEmpty()
1293 && newDiffList.last().command == Diff::Equal) {
1294 newDiffList.last().text += prefix;
1295 } else {
1296 newDiffList.append(Diff(Diff::Equal, prefix));
1297 }
1298 }
1299
1300 // common suffix
1301 const int suffixCount = commonSuffix(lastDelete, lastInsert);
1302 if (suffixCount) {
1303 const QString suffix = lastDelete.right(suffixCount);
1304 lastDelete = lastDelete.left(lastDelete.size() - suffixCount);
1305 lastInsert = lastInsert.left(lastInsert.size() - suffixCount);
1306
1307 diff.text.prepend(suffix);
1308 }
1309
1310 // append delete / insert / equal
1311 if (!lastDelete.isEmpty())
1312 newDiffList.append(Diff(Diff::Delete, lastDelete));
1313 if (!lastInsert.isEmpty())
1314 newDiffList.append(Diff(Diff::Insert, lastInsert));
1315 if (!diff.text.isEmpty())
1316 newDiffList.append(diff);
1317 lastDelete.clear();
1318 lastInsert.clear();
1319 } else { // join with last equal diff
1320 if (!newDiffList.isEmpty()
1321 && newDiffList.last().command == Diff::Equal) {
1322 newDiffList.last().text += diff.text;
1323 } else {
1324 if (!diff.text.isEmpty())
1325 newDiffList.append(diff);

Callers

nothing calls this directly

Calls 12

commonPrefixFunction · 0.85
commonSuffixFunction · 0.85
squashEqualitiesFunction · 0.85
mergeFunction · 0.85
atMethod · 0.80
leftMethod · 0.80
rightMethod · 0.80
DiffClass · 0.50
sizeMethod · 0.45
isEmptyMethod · 0.45
appendMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected