internal void Rebuild() { DocumentLine line = _lineTree.GetLineByNumber(1); SimpleSegment seg = DocumentLineSeeker.NextLineDelimiter(_doc, 0); var lines = new List <DocumentLine>(); Int32 lastDelimeterEnd = 0; while (seg != SimpleSegment.Invalid) { line._exactLength = seg.StartOffset + seg.Length - lastDelimeterEnd; line._delimiterLength = seg.Length; lastDelimeterEnd = seg.StartOffset + seg.Length; lines.Add(line); #if DEBUG line = new DocumentLine(_doc); #else line = new DocumentLine(); #endif seg = DocumentLineSeeker.NextLineDelimiter(_doc, lastDelimeterEnd); } // 最后一行也要处理 line._exactLength = _doc.Length - lastDelimeterEnd; lines.Add(line); _lineTree.RebuildTree(lines); }
internal void Insert(Int32 offset, String text, DocumentUpdate update) { DocumentLine line = _lineTree.GetLineByOffset(offset); // 不允许在\r和\n之间插入文本 SimpleSegment seg = DocumentLineSeeker.NextLineDelimiter(text, 0); if (seg == SimpleSegment.Invalid) { update.LineNumberNeedUpdate = line.LineNumber; // 插入的文本没有新的行,直接加入当前行 SetLineLength(line, line._exactLength + text.Length); return; } update.LineNumberNeedUpdate = line.LineNumber; update.NewStartLineNumber = update.LineNumberNeedUpdate + 1; // 和前置行合并 Int32 lineOffset = line.StartOffset; Int32 lineBreakOffset = offset + seg.EndOffset; Int32 lengthAfterInsertion = lineOffset + line._exactLength - offset; Int32 delimiterLengthAfterInsertion = line._delimiterLength; line._delimiterLength = seg.Length; SetLineLength(line, lineBreakOffset - lineOffset); Int32 lastDelimeterEnd = seg.EndOffset; seg = DocumentLineSeeker.NextLineDelimiter(text, lastDelimeterEnd); // 中间行处理 while (seg != SimpleSegment.Invalid) { #if DEBUG var newLine = new DocumentLine(_doc); #else var newLine = new DocumentLine(); #endif ++update.NewLineCount; newLine._delimiterLength = seg.Length; newLine._exactLength = seg.EndOffset - lastDelimeterEnd; _lineTree.InsertLineAfter(line, newLine); line = newLine; lastDelimeterEnd = seg.EndOffset; seg = DocumentLineSeeker.NextLineDelimiter(text, lastDelimeterEnd); } // 和后置行合并 #if DEBUG var afterLine = new DocumentLine(_doc); #else var afterLine = new DocumentLine(); #endif ++update.NewLineCount; afterLine._delimiterLength = delimiterLengthAfterInsertion; afterLine._exactLength = lengthAfterInsertion + (text.Length - lastDelimeterEnd); _lineTree.InsertLineAfter(line, afterLine); }