Beispiel #1
0
        /// <summary>
        /// Merges the additional line into this line.
        /// </summary>
        public void MergeWith(HighlightedLine additionalLine)
        {
            if (additionalLine == null)
            {
                return;
            }
#if DEBUG
            ValidateInvariants();
            additionalLine.ValidateInvariants();
#endif

            int         pos = 0;
            Stack <int> activeSectionEndOffsets = new Stack <int>();
            int         lineEndOffset           = this.DocumentLine.EndOffset;
            activeSectionEndOffsets.Push(lineEndOffset);
            foreach (HighlightedSection newSection in additionalLine.Sections)
            {
                int newSectionStart = newSection.Offset;
                // Track the existing sections using the stack, up to the point where
                // we need to insert the first part of the newSection
                while (pos < this.Sections.Count)
                {
                    HighlightedSection s = this.Sections[pos];
                    if (newSection.Offset < s.Offset)
                    {
                        break;
                    }
                    while (s.Offset > activeSectionEndOffsets.Peek())
                    {
                        activeSectionEndOffsets.Pop();
                    }
                    activeSectionEndOffsets.Push(s.Offset + s.Length);
                    pos++;
                }
                // Now insert the new section
                // Create a copy of the stack so that we can track the sections we traverse
                // during the insertion process:
                Stack <int> insertionStack = new Stack <int>(activeSectionEndOffsets.Reverse());
                // The stack enumerator reverses the order of the elements, so we call Reverse() to restore
                // the original order.
                int i;
                for (i = pos; i < this.Sections.Count; i++)
                {
                    HighlightedSection s = this.Sections[i];
                    if (newSection.Offset + newSection.Length <= s.Offset)
                    {
                        break;
                    }
                    // Insert a segment in front of s:
                    Insert(ref i, ref newSectionStart, s.Offset, newSection.Color, insertionStack);

                    while (s.Offset > insertionStack.Peek())
                    {
                        insertionStack.Pop();
                    }
                    insertionStack.Push(s.Offset + s.Length);
                }
                Insert(ref i, ref newSectionStart, newSection.Offset + newSection.Length, newSection.Color, insertionStack);
            }

#if DEBUG
            ValidateInvariants();
#endif
        }