// isRightSideContext is true when linenumber corresponds to the right side (sha2). // linenumber is one-based private DiffContext createDiffContext(int linenumber, bool isRightSideContext, FullContextDiff context, ContextDepth depth) { int startLineNumber = Math.Max(1, linenumber - depth.Up); int endLineNumber = linenumber + depth.Down; SparsedListIterator <string> itLeft = context.Left.Begin(); SparsedListIterator <string> itRight = context.Right.Begin(); if (isRightSideContext) { itRight = SparsedListUtils.FindNth(itRight, startLineNumber - 1); itLeft = SparsedListUtils.Advance(itLeft, itRight.Position); } else { itLeft = SparsedListUtils.FindNth(itLeft, startLineNumber - 1); itRight = SparsedListUtils.Advance(itRight, itLeft.Position); } DiffContext diffContext = new DiffContext { Lines = new List <DiffContext.Line>() }; int iContextLine = 0; while (true) { int?leftLineNumber = itLeft.LineNumber != null ? itLeft.LineNumber + 1 : null; int?rightLineNumber = itRight.LineNumber != null ? itRight.LineNumber + 1 : null; DiffContext.Line line = getLineContext(leftLineNumber, rightLineNumber, itLeft.Current, itRight.Current); diffContext.Lines.Add(line); if ((leftLineNumber.HasValue && !isRightSideContext && leftLineNumber == linenumber) || (rightLineNumber.HasValue && isRightSideContext && rightLineNumber == linenumber)) { // zero-based index of a selected line in DiffContext.Lines diffContext.SelectedIndex = iContextLine; } if ((leftLineNumber.HasValue && !isRightSideContext && leftLineNumber >= endLineNumber) || (rightLineNumber.HasValue && isRightSideContext && rightLineNumber >= endLineNumber)) { // we've just reached a line that should not be included in the context break; } if (!itLeft.Next() || !itRight.Next()) { // we've just reached the end break; } ++iContextLine; } return(diffContext); }
/// <summary> /// Advances passed iterator 'index' positions forward /// </summary> public static SparsedListIterator <T> Advance <T>(SparsedListIterator <T> iterator, int index) where T : class { int skipped = 0; while (true) { if (skipped == index) { break; } skipped++; if (!iterator.Next()) { break; } } return(iterator); }
/// <summary> /// Finds an N-th non-null item, where N is passed in 'index' argument /// </summary> public static SparsedListIterator <T> FindNth <T>(SparsedListIterator <T> iterator, int index) where T : class { int skipped = 0; while (true) { if (iterator.GetLineNumber().HasValue) { if (skipped == index) { break; } skipped++; } if (!iterator.Next()) { break; } } return(iterator); }