Ejemplo n.º 1
0
        // Line returns a specific line by given type and line number in a section.
        public DiffLine Line(DiffLineType typ, int line)
        {
            var      difference      = 0;
            var      addCount        = 0;
            var      delCount        = 0;
            DiffLine matchedDiffLine = null;

            foreach (var item in this.Lines)
            {
                switch (item.Type)
                {
                case DiffLineType.DiffLineAdd:
                    addCount++;
                    break;

                case DiffLineType.DiffLineDelete:
                    delCount++;
                    break;

                default:
                    if (matchedDiffLine != null)
                    {
                        if (addCount == delCount)
                        {
                            return(matchedDiffLine);
                        }
                        throw new Exception("diff line error");
                    }
                    difference = item.RightLine - item.LeftLine;
                    addCount   = 0;
                    delCount   = 0;
                    break;
                }

                switch (typ)
                {
                case DiffLineType.DiffLineDelete:
                    if (item.RightLine == 0 && item.LeftLine == line - difference)
                    {
                        matchedDiffLine = item;
                    }
                    break;

                case DiffLineType.DiffLineAdd:
                    if (item.LeftLine == 0 && item.RightLine == line + difference)
                    {
                        matchedDiffLine = item;
                    }
                    break;
                }
            }
            if (addCount == delCount)
            {
                return(matchedDiffLine);
            }
            throw new Exception("diff line error");
        }
Ejemplo n.º 2
0
        public DiffTextLine(string text, DiffLineType changeType)
        {
            Runs = new List <ITextRun>
            {
                new TextRun(text)
            };

            ChangeType = changeType;

            foreach (var run in Runs)
            {
                Length += run.Text.Length;
            }
        }
Ejemplo n.º 3
0
        public DiffLine(int left, string lefttext, int right, string righttext, DiffLineType type)
        {
            Type = DiffLineType.None;

            LeftLineNumber = left;
            LeftLineText = lefttext;
            RightLineNumber = right;
            RightLineText = righttext;

            if (type == DiffLineType.Change)
                Type = type;
            else if (left > 0 && right < 0)
                Type = DiffLineType.Delete;
            else if (left < 0 && right > 0)
                Type = DiffLineType.Add;
        }
Ejemplo n.º 4
0
        public static DiffTextModel GenerateUnifiedDiff(List <Diff> diffs)
        {
            // Generate the model
            DiffTextModel textModel = new();

            int beforeLineNo = 1;
            int afterLineNo  = 1;

            foreach (Diff d in diffs)
            {
                DiffLineType diffType = DiffLineType.Empty;
                switch (d.operation)
                {
                case Operation.DELETE:
                {
                    diffType = DiffLineType.Remove;
                }
                break;

                case Operation.EQUAL:
                {
                    diffType = DiffLineType.Unchanged;
                }
                break;

                case Operation.INSERT:
                {
                    diffType = DiffLineType.Insert;
                }
                break;
                }

                string[] splitLines = d.text.TrimEnd('\n').Split('\n');
                if (splitLines.Length > 0)
                {
                    for (int i = 0; i < splitLines.Length; i++)
                    {
                        bool useBeforeLineNo = true;
                        bool useAfterLineNo  = true;

                        switch (d.operation)
                        {
                        case Operation.DELETE:
                        {
                            useAfterLineNo = false;
                        }
                        break;

                        case Operation.INSERT:
                        {
                            useBeforeLineNo = false;
                        }
                        break;

                        case Operation.EQUAL:
                        default:
                            break;
                        }

                        DiffTextLine newLine = new DiffTextLine(splitLines[i], diffType)
                        {
                            BeforeLineNo = useBeforeLineNo ? beforeLineNo++ : -1,
                            LineNo       = useAfterLineNo ? afterLineNo++ : -1
                        };
                        textModel.InsertLine(newLine);
                    }
                }
            }

            return(textModel);
        }
 private DiffLine(string line, DiffLineType lineType)
 {
     _line = line;
     _lineType = lineType;
 }
Ejemplo n.º 6
0
 private DiffLine(string line, DiffLineType lineType)
 {
     _line     = line;
     _lineType = lineType;
 }