Esempio n. 1
0
        /// <summary>
        /// parses buffer
        /// </summary>
        /// <param name="parser"></param>
        /// <param name="parent">parent region or null</param>
        /// <returns>a region with its children or null</returns>
        public static TextRegion ParseBuffer(SnapshotParser parser, TextRegion parent)
        {
            for (; !parser.AtEnd(); parser.MoveNext())
            {
                TextRegion r = TextRegion.TryCreateRegion(parser);

                if (r != null)
                {
                    parser.MoveNext();
                    //found the start of the region
                    if (!r.Complete)
                    {
                        //searching for child regions
                        while (TextRegion.ParseBuffer(parser, r) != null)
                        {
                            ;
                        }
                        //found everything
                        r.ExtendStartPoint();
                    }
                    //adding to children or merging with last child
                    r.Parent = parent;
                    parent.Children.Add(r);
                    return(r);
                }
                //found parent's end - terminating parsing
                if (parent.TryComplete(parser))
                {
                    parser.MoveNext();
                    return(null);
                }
            }
            return(null);
        }
Esempio n. 2
0
        //Add a method that parses the buffer. The example given here is for illustration only.
        //It synchronously parses the buffer into nested outlining regions.
        private void Outline()
        {
            ITextSnapshot  snapshot   = Buffer.CurrentSnapshot;
            TextRegion     regionTree = new TextRegion();
            SnapshotParser parser     = new SnapshotParser(snapshot, Classifier);

            //parsing snapshot
            while (TextRegion.ParseBuffer(parser, regionTree) != null)
            {
                ;
            }

            List <TextRegion> newRegions = GetRegionList(regionTree);

            List <Span> oldSpans = Regions.ConvertAll(r => r.AsSnapshotSpan().TranslateTo(snapshot, SpanTrackingMode.EdgeExclusive).Span);
            List <Span> newSpans = newRegions.ConvertAll(r => r.AsSnapshotSpan().Span);

            NormalizedSpanCollection oldSpanCollection = new NormalizedSpanCollection(oldSpans);
            NormalizedSpanCollection newSpanCollection = new NormalizedSpanCollection(newSpans);

            //the changed regions are regions that appear in one set or the other, but not both.
            NormalizedSpanCollection removed = NormalizedSpanCollection.Difference(oldSpanCollection, newSpanCollection);

            int changeStart = int.MaxValue;
            int changeEnd   = -1;

            if (removed.Count > 0)
            {
                changeStart = removed[0].Start;
                changeEnd   = removed[removed.Count - 1].End;
            }

            if (newSpans.Count > 0)
            {
                changeStart = Math.Min(changeStart, newSpans[0].Start);
                changeEnd   = Math.Max(changeEnd, newSpans[newSpans.Count - 1].End);
            }

            this.Snapshot = snapshot;
            this.Regions  = newRegions;

            if (changeStart <= changeEnd && this.TagsChanged != null)
            {
                this.TagsChanged(this, new SnapshotSpanEventArgs(
                                     new SnapshotSpan(this.Snapshot, Span.FromBounds(changeStart, changeEnd))));
            }
        }