public void LoadDiff(Text leftText, Text rightText, EditList editList) { _out = new StringBuilder(); Format(editList, leftText, rightText); _editor.Text = _out.ToString(); _editor.Refresh(); _out = null; }
private void WriteLine(StringBuilder @out, Text text, int cur) { text.WriteLine(@out, cur); @out.Append('\n'); }
private void WriteLine(StringBuilder @out, List<TextMarker> markers, Text text, int line, Edit.Type editType) { int start = @out.Length; WriteLine(@out, text, line); int end = @out.Length; Color color; switch (editType) { case Edit.Type.INSERT: color = DiffColor.Added.LightColor; break; case Edit.Type.DELETE: color = DiffColor.Removed.LightColor; break; case Edit.Type.REPLACE: color = DiffColor.Changed.LightColor; break; default: throw new ArgumentOutOfRangeException("editType"); } markers.Add(new TextMarker( start, end - start, TextMarkerType.SolidBlock | TextMarkerType.ExtendToBorder, color )); }
private void WriteContextLine(Text text, int line, StringBuilder leftOut, StringBuilder rightOut) { WriteLine(leftOut, text, line); WriteLine(rightOut, text, line); }
private void Format(EditList edits, Text a, Text b, List<TextMarker> leftMarkers, List<TextMarker> rightMarkers, StringBuilder leftOut, StringBuilder rightOut) { int curA = 0; int offset = 0; foreach (var edit in edits) { for (int i = curA; i < edit.GetBeginA(); i++) { WriteContextLine(a, i, leftOut, rightOut); offset++; } _markers.Add(new DiffMarker( GetMarkerType(edit.GetType()), offset, Math.Max(edit.GetLengthA(), edit.GetLengthB()), edit.GetLengthA(), edit.GetLengthB() )); for (curA = edit.GetBeginA(); curA < edit.GetEndA(); curA++) { WriteLine(leftOut, leftMarkers, a, curA, edit.GetType()); } for (int curB = edit.GetBeginB(); curB < edit.GetEndB(); curB++) { WriteLine(rightOut, rightMarkers, b, curB, edit.GetType()); offset++; } for (int i = edit.GetLengthB() - edit.GetLengthA(); i > 0; i--) { WriteEmptyLine(leftOut, leftMarkers); } for (int i = edit.GetLengthA() - edit.GetLengthB(); i > 0; i--) { WriteEmptyLine(rightOut, rightMarkers); offset++; } } for (; curA < a.Size(); curA++) { WriteContextLine(a, curA, leftOut, rightOut); offset++; } }
public void LoadDiff(Text leftText, Text rightText, EditList editList) { var leftOut = new StringBuilder(); var rightOut = new StringBuilder(); var leftMarkers = new List<TextMarker>(); var rightMarkers = new List<TextMarker>(); _markers = new List<IDiffMarker>(); Format(editList, leftText, rightText, leftMarkers, rightMarkers, leftOut, rightOut); _leftEditor.Document.MarkerStrategy.RemoveAll(p => true); _leftEditor.Text = leftOut.ToString(); leftMarkers.ForEach(p => _leftEditor.Document.MarkerStrategy.AddMarker(p)); _leftEditor.Refresh(); _rightEditor.Document.MarkerStrategy.RemoveAll(p => true); _rightEditor.Text = rightOut.ToString(); rightMarkers.ForEach(p => _rightEditor.Document.MarkerStrategy.AddMarker(p)); _rightEditor.Refresh(); // This is the algorithm ICSharpTextEditor uses to determine the // full size of the scroll bar. _visibleLines = _rightEditor.Document.GetVisibleLine(_rightEditor.Document.TotalNumberOfLines - 1) + 1 + _rightEditor.ActiveTextAreaControl.TextArea.TextView.VisibleLineCount * 2 / 3; _markerMap.SetMarkers(_markers, _visibleLines); if (!_readOnly) _editor.SetMarkers(_markers, _visibleLines); UpdateVisibleLines(); }
void _sideBySideViewer_RightUpdated(object sender, EventArgs e) { _rightText = new Text(_sideBySideViewer.GetRightText()); BuildEditList(); OnRightUpdated(EventArgs.Empty); }
void _sideBySideViewer_LeftUpdated(object sender, EventArgs e) { _leftText = new Text(_sideBySideViewer.GetLeftText()); BuildEditList(); OnLeftUpdated(EventArgs.Empty); }
private Stream BuildStream(Text text, TextFileType fileType) { var preamble = fileType.Encoding.GetPreamble(); var stream = new MemoryStream(text.Content.Length + preamble.Length); var data = fileType.Encoding.GetBytes(text.Content); stream.Write(preamble, 0, preamble.Length); stream.Write(data, 0, data.Length); stream.Position = 0; return stream; }
public void LoadStreams(IStream leftStream, FileType leftFileType, byte[] leftData, IStream rightStream, FileType rightFileType, byte[] rightData) { _unifiedViewer.SelectDetails(leftStream, leftFileType, rightStream, rightFileType); _sideBySideViewer.SelectDetails(leftStream, leftFileType, rightStream, rightFileType); _leftData = leftData; _leftFileType = (TextFileType)leftFileType; _rightData = rightData; _rightFileType = (TextFileType)rightFileType; int leftBomSize = _leftFileType != null ? _leftFileType.Encoding.GetPreamble().Length : 0; int rightBomSize = _rightFileType != null ? _rightFileType.Encoding.GetPreamble().Length : 0; _leftText = new Text( _leftData == null ? String.Empty : _leftFileType.Encoding.GetString(_leftData, leftBomSize, _leftData.Length - leftBomSize) ); _rightText = new Text( _rightData == null ? String.Empty : _rightFileType.Encoding.GetString(_rightData, rightBomSize, _rightData.Length - rightBomSize) ); LoadDiff(); }
private void WriteRemovedLine(Text text, int line) { WriteLine('-', text, line); }
private void WriteLine(char prefix, Text text, int cur) { _out.Append(prefix); text.WriteLine(_out, cur); _out.Append('\n'); }
private void WriteContextLine(Text text, int line) { WriteLine(' ', text, line); }
private void WriteAddedLine(Text text, int line) { WriteLine('+', text, line); }
private bool IsEndOfLineMissing(Text text, int line) { return line + 1 == text.Size() && text.IsMissingNewlineAtEnd(); }
private void Format(EditList edits, Text a, Text b) { for (int curIdx = 0; curIdx < edits.Count; ) { Edit curEdit = edits[curIdx]; int endIdx = FindCombinedEnd(edits, curIdx); Edit endEdit = edits[endIdx]; int aCur = Math.Max(0, curEdit.GetBeginA() - Context); int bCur = Math.Max(0, curEdit.GetBeginB() - Context); int aEnd = Math.Min(a.Size(), endEdit.GetEndA() + Context); int bEnd = Math.Min(b.Size(), endEdit.GetEndB() + Context); WriteHunkHeader(aCur, aEnd, bCur, bEnd); while (aCur < aEnd || bCur < bEnd) { if (aCur < curEdit.GetBeginA() || endIdx + 1 < curIdx) { WriteContextLine(a, aCur); if (IsEndOfLineMissing(a, aCur)) { _out.Append(NoNewLine); } aCur++; bCur++; } else { if (aCur < curEdit.GetEndA()) { WriteRemovedLine(a, aCur); if (IsEndOfLineMissing(a, aCur)) { _out.Append(NoNewLine); } aCur++; } else { if (bCur < curEdit.GetEndB()) { WriteAddedLine(b, bCur); if (IsEndOfLineMissing(b, bCur)) { _out.Append(NoNewLine); } bCur++; } } } if (End(curEdit, aCur, bCur) && ++curIdx < edits.Count) { curEdit = edits[curIdx]; } } } }