Ejemplo n.º 1
0
        internal LuaClassifier(ITextBuffer buffer,
                               IClassificationTypeRegistryService typeService)
        {
            _buffer = buffer;

            _grammar             = new LuaGrammar();
            _parser              = new Irony.Parsing.Parser(_grammar);
            _parser.Context.Mode = Irony.Parsing.ParseMode.VsLineScan;

            _luaTags = new Dictionary <Irony.Parsing.TokenType, ClassificationTag>();
            _luaTags[Irony.Parsing.TokenType.Text]        = BuildTag(typeService, PredefinedClassificationTypeNames.Character);
            _luaTags[Irony.Parsing.TokenType.Keyword]     = BuildTag(typeService, PredefinedClassificationTypeNames.Keyword);
            _luaTags[Irony.Parsing.TokenType.Identifier]  = BuildTag(typeService, PredefinedClassificationTypeNames.Identifier);
            _luaTags[Irony.Parsing.TokenType.String]      = BuildTag(typeService, PredefinedClassificationTypeNames.String);
            _luaTags[Irony.Parsing.TokenType.Literal]     = BuildTag(typeService, PredefinedClassificationTypeNames.Literal);
            _luaTags[Irony.Parsing.TokenType.Operator]    = BuildTag(typeService, PredefinedClassificationTypeNames.Operator);
            _luaTags[Irony.Parsing.TokenType.LineComment] = BuildTag(typeService, PredefinedClassificationTypeNames.Comment);
            _luaTags[Irony.Parsing.TokenType.Comment]     = BuildTag(typeService, PredefinedClassificationTypeNames.Comment);

            _commentTag = BuildTag(typeService, PredefinedClassificationTypeNames.Comment);
            _funcionTag = BuildTag(typeService, "function");
            _tableTag   = BuildTag(typeService, "table");

            InitializeLineStates(_buffer.CurrentSnapshot);
        }
Ejemplo n.º 2
0
        void ReParse()
        {
            ITextSnapshot newSnapshot = buffer.CurrentSnapshot;
            List <Region> newRegions  = new List <Region>();

            LuaLanguage.LuaGrammar grammar = new LuaLanguage.LuaGrammar();
            Irony.Parsing.Parser   parser  = new Irony.Parsing.Parser(grammar);
            var tree = parser.Parse(newSnapshot.GetText());

            if (tree.Root != null)
            {
                FindHiddenRegions(newSnapshot, tree.Root, ref newRegions);
            }


            //determine the changed span, and send a changed event with the new spans
            List <Span> oldSpans =
                new List <Span>(this.regions.Select(r => AsSnapshotSpan(r, this.snapshot)
                                                    .TranslateTo(newSnapshot, SpanTrackingMode.EdgeExclusive)
                                                    .Span));
            List <Span> newSpans =
                new List <Span>(newRegions.Select(r => AsSnapshotSpan(r, newSnapshot).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 = newSnapshot;
            this.regions  = newRegions;

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