Exemplo n.º 1
0
        public static TextBufferStateTracker GetTrackerForTextBuffer(ITextBuffer buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            TextBufferStateTracker tracker;

            buffer.Properties.TryGetProperty(typeof(TextBufferStateTracker), out tracker);
            if (tracker != null)
            {
                return(tracker);
            }

            tracker = new TextBufferStateTracker(buffer);
            buffer.Properties.AddProperty(typeof(TextBufferStateTracker), tracker);
            return(tracker);
        }
Exemplo n.º 2
0
        public IList <ClassificationSpan> GetClassificationSpans(SnapshotSpan span)         // from IClassifier
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            _snapshot = span.Snapshot;

            var appSettings = ProbeEnvironment.CurrentAppSettings;
            var fileName    = VsTextUtil.TryGetDocumentFileName(span.Snapshot.TextBuffer);

            var tracker   = TextBufferStateTracker.GetTrackerForTextBuffer(span.Snapshot.TextBuffer);
            var spans     = new List <ClassificationSpan>();
            var state     = tracker.GetStateForPosition(span.Start.Position, span.Snapshot, fileName, appSettings);
            var tokenInfo = new ProbeClassifierScanner.TokenInfo();

            var fileStore = CodeModel.FileStore.GetOrCreateForTextBuffer(span.Snapshot.TextBuffer);

            if (fileStore == null)
            {
                return(new List <ClassificationSpan>());
            }

            var model = fileStore.GetMostRecentModel(appSettings, fileName, span.Snapshot, "GetClassificationSpans");

            _scanner.SetSource(span.GetText(), span.Start.Position, span.Snapshot, model);

            var disableDeadCode = ProbeToolsPackage.Instance.EditorOptions.DisableDeadCode;

            DisabledSectionTracker disabledSectionTracker = null;

            if (disableDeadCode)
            {
                disabledSectionTracker = new DisabledSectionTracker(model.DisabledSections);
                if (disabledSectionTracker.SetOffset(_scanner.PositionOffset))
                {
                    state |= State.Disabled;
                }
                else
                {
                    state &= ~State.Disabled;
                }
            }
            else
            {
                state &= ~State.Disabled;
            }

            while (_scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))
            {
                var classificationType = GetClassificationType(tokenInfo.Type);
                if (classificationType != null)
                {
                    spans.Add(new ClassificationSpan(new SnapshotSpan(_snapshot, new Span(span.Start.Position + tokenInfo.StartIndex, tokenInfo.Length)), classificationType));
                }

                if (disableDeadCode)
                {
                    if (disabledSectionTracker.Advance(_scanner.PositionOffset + _scanner.Position))
                    {
                        state |= State.Disabled;
                    }
                    else
                    {
                        state &= ~State.Disabled;
                    }
                }
                else
                {
                    state &= ~State.Disabled;
                }
            }

            return(spans);
        }