// 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);
        }
示例#2
0
        // linenumber is one-based
        private static DiffContext.Line getContextLine(int linenumber, bool isRightSideContext, string text)
        {
            DiffContext.Line line = new DiffContext.Line
            {
                Text = text
            };

            DiffContext.Line.Side side = new DiffContext.Line.Side
            {
                Number = linenumber,

                // this 'maker' cannot distinguish between modified and unmodified lines
                State = DiffContext.Line.State.Changed
            };

            if (isRightSideContext)
            {
                line.Right = side;
            }
            else
            {
                line.Left = side;
            }

            return(line);
        }
        // leftLineNumber and rightLineNumber are one-based
        private static DiffContext.Line getLineContext(int?leftLineNumber, int?rightLineNumber,
                                                       string leftLine, string rightLine)
        {
            DiffContext.Line line = new DiffContext.Line();

            if (leftLineNumber.HasValue && rightLineNumber.HasValue)
            {
                Debug.Assert(leftLine == rightLine);
                line.Left  = getSide(leftLineNumber.Value, DiffContext.Line.State.Unchanged);
                line.Right = getSide(rightLineNumber.Value, DiffContext.Line.State.Unchanged);
                line.Text  = leftLine;
            }
            else if (leftLineNumber.HasValue)
            {
                line.Left = getSide(leftLineNumber.Value, DiffContext.Line.State.Changed);
                line.Text = leftLine;
            }
            else if (rightLineNumber.HasValue)
            {
                line.Right = getSide(rightLineNumber.Value, DiffContext.Line.State.Changed);
                line.Text  = rightLine;
            }
            else
            {
                Debug.Assert(false);
            }

            return(line);
        }
        private static string getCode(DiffContext.Line line)
        {
            if (line.Text.Length == 0)
            {
                return("<br");
            }

            // replace some special symbols such as '<' or '>'
            string encodedText = System.Net.WebUtility.HtmlEncode(line.Text);

            // replace spaces with &nbsp
            return(encodedText
                   .Replace("\t", "    ")    /* replace each TAB with four spaces */
                   .Replace(" ", "&nbsp;")); /* replace each SPACE with &nbsp; */
        }
        private string getDiffCellClass(DiffContext.Line line)
        {
            if (line.Left.HasValue && line.Right.HasValue)
            {
                return("unchanged");
            }
            else if (line.Left.HasValue)
            {
                return(line.Left.Value.State == DiffContext.Line.State.Unchanged ? "unchanged" : "removed");
            }
            else if (line.Right.HasValue)
            {
                return(line.Right.Value.State == DiffContext.Line.State.Unchanged ? "unchanged" : "added");
            }

            throw new ArgumentException(String.Format("Bad context line: {0}", line.ToString()));
        }
        private string getCode(DiffContext.Line line)
        {
            if (line.Text.Length == 0)
            {
                return("<br");
            }

            string trimmed       = line.Text.TrimStart();
            int    leadingSpaces = line.Text.Length - trimmed.Length;

            string spaces = string.Empty;

            for (int i = 0; i < leadingSpaces; ++i)
            {
                spaces += "&nbsp;";
            }

            return(spaces + System.Net.WebUtility.HtmlEncode(trimmed));
        }
示例#7
0
        private string getCode(DiffContext.Line line)
        {
            if (line.Text.Length == 0)
            {
                return("<br");
            }

            string trimmed       = line.Text.TrimStart();
            int    leadingSpaces = line.Text.Length - trimmed.Length;

            StringBuilder result = new StringBuilder();

            for (int i = 0; i < leadingSpaces; ++i)
            {
                result.Append("&nbsp;");
            }

            result.Append(System.Net.WebUtility.HtmlEncode(trimmed));
            return(result.ToString());
        }
 private string getRightLineNumber(DiffContext.Line line)
 {
     return(line.Right?.Number.ToString() ?? "");
 }
 private static string getLeftLineNumber(DiffContext.Line line)
 {
     return(line.Left?.Number.ToString() ?? "");
 }