/* * HashSet<DocumentLine> deletedLines = new HashSet<DocumentLine>(); * readonly HashSet<DocumentLine> changedLines = new HashSet<DocumentLine>(); * HashSet<DocumentLine> deletedOrChangedLines = new HashSet<DocumentLine>(); * * /// <summary> * /// Gets the list of lines deleted since the last RetrieveChangedLines() call. * /// The returned list is unsorted. * /// </summary> * public ICollection<DocumentLine> RetrieveDeletedLines() * { * var r = deletedLines; * deletedLines = new HashSet<DocumentLine>(); * return r; * } * * /// <summary> * /// Gets the list of lines changed since the last RetrieveChangedLines() call. * /// The returned list is sorted by line number and does not contain deleted lines. * /// </summary> * public List<DocumentLine> RetrieveChangedLines() * { * var list = (from line in changedLines * where !line.IsDeleted * let number = line.LineNumber * orderby number * select line).ToList(); * changedLines.Clear(); * return list; * } * * /// <summary> * /// Gets the list of lines changed since the last RetrieveDeletedOrChangedLines() call. * /// The returned list is not sorted. * /// </summary> * public ICollection<DocumentLine> RetrieveDeletedOrChangedLines() * { * var r = deletedOrChangedLines; * deletedOrChangedLines = new HashSet<DocumentLine>(); * return r; * } */ #endregion #region Rebuild public void Rebuild() { // keep the first document line DocumentLine ls = documentLineTree.GetByNumber(1); SimpleSegment ds = NewLineFinder.NextNewLine(document, 0); List <DocumentLine> lines = new List <DocumentLine>(); int lastDelimiterEnd = 0; while (ds != SimpleSegment.Invalid) { ls.TotalLength = ds.Offset + ds.Length - lastDelimiterEnd; ls.DelimiterLength = ds.Length; lastDelimiterEnd = ds.Offset + ds.Length; lines.Add(ls); ls = new DocumentLine(document); ds = NewLineFinder.NextNewLine(document, lastDelimiterEnd); } ls.ResetLine(); ls.TotalLength = document.TextLength - lastDelimiterEnd; lines.Add(ls); documentLineTree.RebuildTree(lines); foreach (ILineTracker lineTracker in lineTrackers) { lineTracker.RebuildDocument(); } }
public void Insert(int offset, string text) { DocumentLine line = documentLineTree.GetByOffset(offset); int lineOffset = line.Offset; Debug.Assert(offset <= lineOffset + line.TotalLength); if (offset > lineOffset + line.Length) { Debug.Assert(line.DelimiterLength == 2); // we are inserting in the middle of a delimiter // shorten line SetLineLength(line, line.TotalLength - 1); // add new line line = InsertLineAfter(line, 1); line = SetLineLength(line, 1); } SimpleSegment ds = NewLineFinder.NextNewLine(text, 0); if (ds == SimpleSegment.Invalid) { // no newline is being inserted, all text is inserted in a single line //line.InsertedLinePart(offset - line.Offset, text.Length); SetLineLength(line, line.TotalLength + text.Length); return; } //DocumentLine firstLine = line; //firstLine.InsertedLinePart(offset - firstLine.Offset, ds.Offset); int lastDelimiterEnd = 0; while (ds != SimpleSegment.Invalid) { // split line segment at line delimiter int lineBreakOffset = offset + ds.Offset + ds.Length; lineOffset = line.Offset; int lengthAfterInsertionPos = lineOffset + line.TotalLength - (offset + lastDelimiterEnd); line = SetLineLength(line, lineBreakOffset - lineOffset); DocumentLine newLine = InsertLineAfter(line, lengthAfterInsertionPos); newLine = SetLineLength(newLine, lengthAfterInsertionPos); line = newLine; lastDelimiterEnd = ds.Offset + ds.Length; ds = NewLineFinder.NextNewLine(text, lastDelimiterEnd); } //firstLine.SplitTo(line); // insert rest after last delimiter if (lastDelimiterEnd != text.Length) { //line.InsertedLinePart(0, text.Length - lastDelimiterEnd); SetLineLength(line, line.TotalLength + text.Length - lastDelimiterEnd); } }