Esempio n. 1
0
 public static TreePath Deserialize(BufferedTextReader sr, int nextToken)
 {
     if (nextToken != '"')
     {
         throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
     }
     sr.InitBuffer();
     nextToken = sr.FillUntil('"');
     sr.Read();
     return(TreePath.Create(sr.BufferToString()));
 }
Esempio n. 2
0
        protected override IUndoableEdit RemoveUpdate(int offset, int length)
        {
            var indexForOffset = rootNode.NodeIndexForOffset(offset);

            if (indexForOffset < 0)
            {
                throw new ArgumentException();
            }

            var indexForEndOffset = rootNode.NodeIndexForOffset(offset + length);

            if (indexForEndOffset < 0)
            {
                if (offset + length == rootNode.EndOffset)
                {
                    indexForEndOffset = rootNode.Count - 1;
                }
                if (indexForEndOffset < 0)
                {
                    throw new ArgumentException();
                }
            }

            if (indexForEndOffset == indexForOffset)
            {
                // simple case. Nothing happens.
                return(new ElementEdit(this, TreePath.Create(Root), TreePath.Create(Root), indexForEndOffset, EmptyNodes, EmptyNodes));
            }

            removedNodes.Clear();
            addedNodes.Clear();

            for (var line = indexForOffset; line <= indexForEndOffset; line += 1)
            {
                removedNodes.Add(rootNode[line]);
            }

            var start = Content.CreatePosition(rootNode[indexForOffset].Offset, Bias.Forward);
            var end   = Content.CreatePosition(rootNode[indexForEndOffset].EndOffset, Bias.Backward);

            addedNodes.Add(new ImmutableLeafTextNode(this, start, end));

            var replacedNodes = addedNodes.ToArray();
            var oldRoot       = Root;

            ReplaceRoot(rootNode.Replace(indexForOffset, removedNodes.Count, replacedNodes));
            return(new ElementEdit(this, TreePath.Create(oldRoot), TreePath.Create(Root), indexForOffset, replacedNodes, removedNodes.ToArray()));
        }
Esempio n. 3
0
        protected override IUndoableEdit InsertUpdate(int offset, int length)
        {
            if (length <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            var splitPending = false;

            removedNodes.Clear();
            addedNodes.Clear();

            ITextNode     removeCandidate;
            ITextPosition startOffset;
            ITextPosition endOffset;

            int start, end;
            var insertIdx = FindRemovedParagraphs(offset, out start, out end);

            if (insertIdx != -1)
            {
                startOffset = Content.CreatePosition(start, Bias.Forward);
                endOffset   = Content.CreatePosition(end, Bias.Backward);
            }
            else if (rootNode.Count > 0)
            {
                removeCandidate = rootNode[rootNode.Count - 1];
                insertIdx       = rootNode.Count - 1;
                startOffset     = Content.CreatePosition(removeCandidate.Offset, Bias.Forward);
                endOffset       = Content.CreatePosition(removeCandidate.EndOffset, Bias.Backward);
            }
            else
            {
                startOffset     = Content.CreatePosition(offset, Bias.Forward);
                endOffset       = Content.CreatePosition(offset + length, Bias.Backward);
                removeCandidate = null;
                insertIdx       = 0;
            }

            var it = new BreakIterator <LineBreakType>(Content, rules.IsLineBreak, Math.Max(0, offset - 1), offset + length);

            if (offset > 0)
            {
                it.MoveNext();
                splitPending = it.Current != LineBreakType.None;
            }

            var cursor = offset;

            while (it.MoveNext())
            {
                var c = it.Current;
                if (c != LineBreakType.Continuation)
                {
                    if (splitPending)
                    {
                        addedNodes.Add(new ImmutableLeafTextNode(this, startOffset, Content.CreatePosition(cursor, Bias.Backward)));
                        startOffset  = Content.CreatePosition(cursor, Bias.Forward);
                        offset       = cursor;
                        splitPending = false;
                    }

                    if (c == LineBreakType.LineBreak)
                    {
                        splitPending = true;
                    }
                }
                cursor += 1;
            }

            if (cursor != offset)
            {
                if (splitPending)
                {
                    addedNodes.Add(new ImmutableLeafTextNode(this, startOffset, Content.CreatePosition(cursor, Bias.Backward)));
                    addedNodes.Add(new ImmutableLeafTextNode(this, Content.CreatePosition(cursor, Bias.Forward), endOffset));
                }
                else
                {
                    addedNodes.Add(new ImmutableLeafTextNode(this, startOffset, endOffset));
                }
            }

            var addedNodesArray = addedNodes.ToArray();
            var oldRoot         = Root;
            var newRoot         = rootNode.Replace(insertIdx, removedNodes.Count, addedNodesArray);

            ReplaceRoot(newRoot);
            return(new ElementEdit(this, TreePath.Create(oldRoot), TreePath.Create(Root), insertIdx, addedNodesArray, removedNodes.ToArray()));
        }