| 399 | } |
| 400 | |
| 401 | KDevelop::VcsDiff VcsDiff::subDiff(const uint startLine, const uint endLine, DiffDirection dir) const |
| 402 | { |
| 403 | // Code adapted from cola/diffparse.py |
| 404 | enum LineType { |
| 405 | ADD = '+', |
| 406 | DEL = '-', |
| 407 | CTX = ' ', |
| 408 | NO_NEWLINE = '\\' |
| 409 | }; |
| 410 | |
| 411 | VcsDiff ret; |
| 412 | ret.setBaseDiff(baseDiff()); |
| 413 | ret.setDepth(depth()); |
| 414 | |
| 415 | |
| 416 | auto hunks = d->hunks; |
| 417 | QStringList lines; |
| 418 | for (const auto& hunk : hunks) { |
| 419 | // Skip hunks before the first line |
| 420 | if (hunk < startLine) |
| 421 | continue; |
| 422 | |
| 423 | // Skip hunks after the last line |
| 424 | if (hunk > endLine) |
| 425 | break; |
| 426 | |
| 427 | std::map<LineType, int> counts = { |
| 428 | {ADD, 0}, |
| 429 | {DEL, 0}, |
| 430 | {CTX, 0}, |
| 431 | {NO_NEWLINE, 0} |
| 432 | }; |
| 433 | QStringList filteredLines; |
| 434 | |
| 435 | // Will be set if the previous line in a hunk was |
| 436 | // skipped because it was not in the selected range |
| 437 | bool prevSkipped = false; |
| 438 | |
| 439 | uint lnIdx = hunk.headingLineIdx; |
| 440 | |
| 441 | // Store the number of skipped lines which start the hunk |
| 442 | // (i.e. lines before a first deletion (addition in case of reverse) |
| 443 | // so that we can adjust the start appropriately |
| 444 | int startOffset = 0; |
| 445 | const auto _lines = QStringList(hunk.lines.constBegin(), hunk.lines.constEnd()); |
| 446 | for(const auto& line: _lines) { |
| 447 | lnIdx++; |
| 448 | LineType tp = line.length() > 0 ? (LineType) line[0].toLatin1() : (LineType) 0; |
| 449 | QString content = line.mid(1); |
| 450 | |
| 451 | if (dir == Reverse) { |
| 452 | if (tp == ADD) tp = DEL; |
| 453 | else if (tp == DEL) tp = ADD; |
| 454 | } |
| 455 | |
| 456 | if (lnIdx < startLine || endLine < lnIdx) { |
| 457 | // skip additions (or deletions if reverse) that are not in range |
| 458 | if (tp == ADD) { |