Exemple #1
0
        public void Remove(int offset, int length)
        {
            if (length <= 0 || this.Length == 0)
            {
                return;
            }

            RedBlackTree <TreeNode> .RedBlackTreeNode startNode = GetTreeNodeAtOffset(offset);
            RedBlackTree <TreeNode> .RedBlackTreeNode endNode   = GetTreeNodeAtOffset(offset + length);

            int newLength = offset - CalcOffset(startNode);

            if (startNode == endNode)
            {
                TreeNode splittedNode = startNode.value.SplitRight(newLength + length);
                ChangeLength(startNode, newLength);

                if (splittedNode.Length > 0)
                {
                    InsertAfter(startNode, splittedNode);
                }
                return;
            }
            int endSegmentLength = offset + length - CalcOffset(endNode);

            RedBlackTree <TreeNode> .RedBlackTreeIterator iter = new RedBlackTree <TreeNode> .RedBlackTreeIterator(startNode);

            RedBlackTree <TreeNode> .RedBlackTreeNode node;
            do
            {
                node = iter.CurrentNode;
                iter.MoveNext();
                if (node == null)
                {
                    break;
                }
                if (node == startNode)
                {
                    // has no right side, otherwise it would be startNode == endNode
                    length -= node.value.Length;
                    ChangeLength(node, newLength);
                }
                else if (node == endNode)
                {
                    // has no left side, otherwise it would be startNode == endNode
                    TreeNode rightSide = node.value.SplitRight(endSegmentLength);
                    if (rightSide.Length > 0)
                    {
                        InsertAfter(node, rightSide);
                    }
                    RemoveNode(node);
                }
                else                     // nodes in between
                {
                    length -= node.value.Length;
                    RemoveNode(node);
                }
            } while (node != endNode);
        }
        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);
        }
Exemple #3
0
        public string GetTextAt(int offset, int count)
        {
            if (count <= 0 || this.Length == 0)
            {
                return("");
            }
            char[] result = new char[count];

            RedBlackTree <TreeNode> .RedBlackTreeNode startNode = GetTreeNodeAtOffset(offset);
            RedBlackTree <TreeNode> .RedBlackTreeNode endNode   = GetTreeNodeAtOffset(offset + count);

            int nodeOffset;

            if (startNode == endNode)
            {
                nodeOffset = CalcOffset(startNode);
                startNode.value.CopyChars(this, nodeOffset, offset, count, result, 0);
            }
            else
            {
                RedBlackTree <TreeNode> .RedBlackTreeIterator iter = new RedBlackTree <TreeNode> .RedBlackTreeIterator(startNode);

                RedBlackTree <TreeNode> .RedBlackTreeNode node;
                int curOffset = 0;
                do
                {
                    node       = iter.CurrentNode;
                    nodeOffset = CalcOffset(node);
                    iter.MoveNext();
                    if (node == null)
                    {
                        break;
                    }
                    int partLength;
                    if (node == startNode)
                    {
                        partLength = nodeOffset + startNode.value.Length - offset;
                    }
                    else if (node == endNode)
                    {
                        partLength = offset + count - nodeOffset;
                    }
                    else
                    {
                        partLength = node.value.Length;
                    }
                    node.value.CopyChars(this, nodeOffset, offset + curOffset, partLength, result, curOffset);
                    curOffset += partLength;
                } while (node != endNode);
            }
            return(new string (result));
        }
        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);
        }
            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());
            }