MCPcopy Create free account
hub / github.com/KDE/kate / mapDiffLine

Method mapDiffLine

apps/lib/diff/gitdiff.cpp:332–403  ·  view source on GitHub ↗

* Maps a line position in the diff to a corresponding line position in the destination file. * * @param line a 0-based line position in the diff * @param dest specifies the destination file to map to: * either SRC (the source file, '-') or TGT (the target file, '+') * @returns the 0-based line position in the destination file or -1 if no such position exists.

Source from the content-addressed store, hash-verified

330 * @returns the 0-based line position in the destination file or -1 if no such position exists.
331 */
332 int mapDiffLine(const uint line, const Dest dest) const
333 {
334 const QLatin1Char skipChar = (dest == SRC) ? QLatin1Char(TGT) : QLatin1Char(SRC);
335 for (const auto &h : hunks) {
336 if (h.containsDiffLine(line)) {
337 int hunkPos = h.diffLineToHunkLine(line);
338
339 // The line refers to the heading line
340 if (hunkPos < 0)
341 return -1;
342
343 // Any lines in the diff hunk which come before line and come from the opposite
344 // of dest should not be counted (they are not present in the dest)
345 int skipCount = 0;
346 for (int i = 0; i < hunkPos; i++) {
347 if (h.lines.at(i).startsWith(skipChar))
348 skipCount++;
349 }
350
351 // Any lines in the diff hunk which come from the second part (src)/ first part (tgt)
352 // of a conflict should not be counted either
353 bool inConflict = false; // This is set so that a line inside a conflict is recognized as a valid line
354 for (int i = 0; i < hunkPos; i++) {
355 if (CONFLICT_START_RE->match(h.lines.at(i)).hasMatch()) {
356 skipCount++; // skip the conflict marker line
357 if (dest == TGT) {
358 while ((++i) < hunkPos && !CONFLICT_MID_RE->match(h.lines.at(i)).hasMatch()) {
359 skipCount++;
360 }
361 } else {
362 inConflict = true;
363 }
364 }
365 if (CONFLICT_MID_RE->match(h.lines.at(i)).hasMatch()) {
366 skipCount++; // skip the conflict marker line
367 if (dest == SRC) {
368 while ((++i) < hunkPos && !CONFLICT_END_RE->match(h.lines.at(i)).hasMatch())
369 skipCount++;
370 } else {
371 inConflict = true;
372 }
373 }
374 if (CONFLICT_END_RE->match(h.lines.at(i)).hasMatch()) {
375 skipCount++; // skip the conflict marker line
376 inConflict = false;
377 }
378 }
379
380 auto ln = h.lines[hunkPos];
381
382 // This works around the fact that inConflict is set even if hunkPos
383 // ends up hitting a conflict marker
384 if (CONFLICT_RE->match(ln).hasMatch())
385 return -1;
386
387 if (ln.startsWith(QLatin1Char(dest)) || ln.startsWith(u' ') || ln.isEmpty() || inConflict) {
388 if (dest == SRC) {
389 // The -1 accounts for the fact that srcStart is 1-based

Callers 2

diffLineToSourceLineMethod · 0.80
diffLineToTargetLineMethod · 0.80

Calls 4

containsDiffLineMethod · 0.80
diffLineToHunkLineMethod · 0.80
matchMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected