public static int GetOffsetFromNode(RedBlackTree <TreeNode> .RedBlackTreeNode node) { int offset = node.left != null ? node.left.value.totalLength : 0; while (node.parent != null) { if (node == node.parent.right) { if (node.parent.left != null && node.parent.left.value != null) { offset += node.parent.left.value.totalLength; } if (node.parent.value != null) { offset += node.parent.value.Length; } } node = node.parent; } return(offset); }
void TextRemove(int offset, int length) { if (length == 0 || (lines.Count == 1 && lines.Length == 0)) { return; } LineSegmentTree.TreeNode startNode = lines.GetNodeAtOffset(offset); int charsRemoved = startNode.EndOffset - offset; if (offset + length < startNode.EndOffset) { lines.ChangeLength(startNode, startNode.Length - length); return; } LineSegmentTree.TreeNode endNode = lines.GetNodeAtOffset(offset + length); if (endNode == null) { return; } int charsLeft = endNode.EndOffset - (offset + length); if (startNode == endNode) { lines.ChangeLength(startNode, startNode.Length - length); return; } RedBlackTree <LineSegmentTree.TreeNode> .RedBlackTreeIterator iter = startNode.Iter; iter.MoveNext(); LineSegment line; int cnt = 0; do { line = iter.Current; iter.MoveNext(); lines.RemoveLine(line); ++cnt; } while (line != endNode); lines.ChangeLength(startNode, startNode.Length - charsRemoved + charsLeft, endNode.DelimiterLength); }
public void Insert(int offset, int addBufferOffset, int addLength) { RedBlackTree <TreeNode> .RedBlackTreeNode node = GetTreeNodeAtOffset(offset); int oldNodeOffset = CalcOffset(node); int newLength = offset - oldNodeOffset; TreeNode splittedNode = node.value.SplitRight(newLength); ChangeLength(node, newLength); RedBlackTree <TreeNode> .RedBlackTreeNode newNode = InsertAfter(node, new DataTreeNode(addBufferOffset, addLength)); if (splittedNode.Length > 0) { InsertAfter(newNode, splittedNode); } if (newLength == 0) { RemoveNode(node); } }
static int GetNextOffset(Document document, int lineNumber) { int startLineNumber = lineNumber + 1; if (startLineNumber > document.Length) { startLineNumber = 0; } LineSegment startLine = document.GetLine(startLineNumber); RedBlackTree <LineSegmentTree.TreeNode> .RedBlackTreeIterator iter = startLine.Iter; do { LineSegment line = iter.Current; if (line.IsBookmarked) { return(line.Offset); } } while (iter.MoveNext()); return(-1); }
public int OffsetToLineNumber(int offset) { RedBlackTree <TreeNode> .RedBlackTreeNode node = GetTreeNodeAtOffset(offset); if (node == null) { return(-1); } int result = node.left != null ? node.left.value.count : 0;; while (node.parent != null) { if (node == node.parent.right) { if (node.parent.left != null) { result += node.parent.left.value.count; } result++; } node = node.parent; } return(result); }
void UpdateNode(RedBlackTree <TreeNode> .RedBlackTreeNode node) { if (node == null) { return; } int currentTotalLength = node.value.Length; if (node.left != null) { currentTotalLength += node.left.value.TotalLength; } if (node.right != null) { currentTotalLength += node.right.value.TotalLength; } if (currentTotalLength != node.value.TotalLength) { node.value.TotalLength = currentTotalLength; UpdateNode(node.parent); } }
static string GenerateRtf(Document doc, Mono.TextEditor.Highlighting.SyntaxMode mode, Mono.TextEditor.Highlighting.Style style, ITextEditorOptions options) { StringBuilder rtfText = new StringBuilder(); List <Gdk.Color> colorList = new List <Gdk.Color> (); ISegment selection = new Segment(0, doc.Length); LineSegment line = doc.GetLineByOffset(selection.Offset); LineSegment endLine = doc.GetLineByOffset(selection.EndOffset); RedBlackTree <LineSegmentTree.TreeNode> .RedBlackTreeIterator iter = line.Iter; bool isItalic = false; bool isBold = false; int curColor = -1; do { bool appendSpace = false; line = iter.Current; for (Chunk chunk = mode.GetChunks(doc, style, line, line.Offset, line.EditableLength); chunk != null; chunk = chunk.Next) { int start = System.Math.Max(selection.Offset, chunk.Offset); int end = System.Math.Min(chunk.EndOffset, selection.EndOffset); ChunkStyle chunkStyle = chunk.GetChunkStyle(style); if (start < end) { if (isBold != chunkStyle.Bold) { rtfText.Append(chunkStyle.Bold ? @"\b" : @"\b0"); isBold = chunkStyle.Bold; appendSpace = true; } if (isItalic != chunkStyle.Italic) { rtfText.Append(chunkStyle.Italic ? @"\i" : @"\i0"); isItalic = chunkStyle.Italic; appendSpace = true; } if (!colorList.Contains(chunkStyle.Color)) { colorList.Add(chunkStyle.Color); } int color = colorList.IndexOf(chunkStyle.Color); if (curColor != color) { curColor = color; rtfText.Append(@"\cf" + (curColor + 1)); appendSpace = true; } for (int i = start; i < end; i++) { char ch = chunk.GetCharAt(doc, i); switch (ch) { case '\\': rtfText.Append(@"\\"); break; case '{': rtfText.Append(@"\{"); break; case '}': rtfText.Append(@"\}"); break; case '\t': rtfText.Append(@"\tab"); appendSpace = true; break; default: if (appendSpace) { rtfText.Append(' '); appendSpace = false; } rtfText.Append(ch); break; } } } } if (line == endLine) { break; } rtfText.Append(@"\par"); rtfText.AppendLine(); } while (iter.MoveNext()); // color table StringBuilder colorTable = new StringBuilder(); colorTable.Append(@"{\colortbl ;"); for (int i = 0; i < colorList.Count; i++) { Gdk.Color color = colorList[i]; colorTable.Append(@"\red"); colorTable.Append(color.Red / 256); colorTable.Append(@"\green"); colorTable.Append(color.Green / 256); colorTable.Append(@"\blue"); colorTable.Append(color.Blue / 256); colorTable.Append(";"); } colorTable.Append("}"); StringBuilder rtf = new StringBuilder(); rtf.Append(@"{\rtf1\ansi\deff0\adeflang1025"); // font table rtf.Append(@"{\fonttbl"); rtf.Append(@"{\f0\fnil\fprq1\fcharset128 " + options.Font.Family + ";}"); rtf.Append("}"); rtf.Append(colorTable.ToString()); rtf.Append(@"\viewkind4\uc1\pard"); rtf.Append(@"\f0"); try { string fontName = options.Font.ToString(); double fontSize = Double.Parse(fontName.Substring(fontName.LastIndexOf(' ') + 1), System.Globalization.CultureInfo.InvariantCulture) * 2; rtf.Append(@"\fs"); rtf.Append(fontSize); } catch(Exception) { }; rtf.Append(@"\cf1"); rtf.Append(rtfText.ToString()); rtf.Append("}"); // System.Console.WriteLine(rtf); return(rtf.ToString()); }
void ChangeLength(RedBlackTree <TreeNode> .RedBlackTreeNode node, int newLength) { node.value.Length = newLength; UpdateNode(node); }
public TreeNode GetNodeAtOffset(int offset) { RedBlackTree <TreeNode> .RedBlackTreeNode node = GetTreeNodeAtOffset(offset); return(node != null ? node.value : null); }