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

Method mapDiffLine

kdevplatform/vcs/vcsdiff.cpp:314–382  ·  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 a @ref VcsDiff::SourceLocation whose path is the (relative to diff root) *

Source from the content-addressed store, hash-verified

312 * destination file or {"", -1} if no such position exists.
313 */
314 VcsDiff::SourceLocation mapDiffLine ( const uint line, const Dest dest ) const
315 {
316 const QLatin1Char skipChar = (dest == SRC) ? QLatin1Char(TGT) : QLatin1Char(SRC);
317 for (const auto& h : hunks) {
318 if (h.containsDiffLine(line)) {
319 int hunkPos = h.diffLineToHunkLine(line);
320
321 // The line refers to the heading line
322 if (hunkPos < 0)
323 return {};
324
325 // Any lines in the diff hunk which come before line and come from the opposite
326 // of dest should not be counted (they are not present in the dest)
327 int skipCount = 0;
328 for(int i=0; i<hunkPos; i++) {
329 if (h.lines.at(i).startsWith(skipChar))
330 skipCount++;
331 }
332
333 // Any lines in the diff hunk which come from the second part (src)/ first part (tgt)
334 // of a conflict should not be counted either
335 bool inConflict = false; // This is set so that a line inside a conflict is recognized as a valid line
336 for(int i=0; i<hunkPos; i++) {
337 if (CONFLICT_START_RE->match(h.lines.at(i)).hasMatch()) {
338 skipCount++; // skip the conflict marker line
339 if (dest == TGT) {
340 while ((++i) < hunkPos && !CONFLICT_MID_RE->match(h.lines.at(i)).hasMatch()) {
341 skipCount++;
342 }
343 } else {
344 inConflict = true;
345 }
346 }
347 if (CONFLICT_MID_RE->match(h.lines.at(i)).hasMatch()) {
348 skipCount++; // skip the conflict marker line
349 if (dest == SRC) {
350 while ((++i) < hunkPos && !CONFLICT_END_RE->match(h.lines.at(i)).hasMatch())
351 skipCount++;
352 } else {
353 inConflict = true;
354 }
355 }
356 if (CONFLICT_END_RE->match(h.lines.at(i)).hasMatch()) {
357 skipCount++; // skip the conflict marker line
358 inConflict = false;
359 }
360 }
361
362 auto ln = h.lines[hunkPos];
363
364 // This works around the fact that inConflict is set even if hunkPos
365 // ends up hitting a conflict marker
366 if (CONFLICT_RE->match(ln).hasMatch())
367 return {};
368
369 if (ln.startsWith(QLatin1Char(dest)) || ln.startsWith(QLatin1Char(' ')) || ln.isEmpty() || inConflict) {
370 if (dest == SRC)
371 // The -1 accounts for the fact that srcStart is 1-based

Callers 2

diffLineToSourceMethod · 0.80
diffLineToTargetMethod · 0.80

Calls 6

containsDiffLineMethod · 0.80
diffLineToHunkLineMethod · 0.80
hasMatchMethod · 0.80
atMethod · 0.45
matchMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected